Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.WindowConstants;
- import java.awt.Dimension;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Point;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseEvent;
- import java.awt.Font;
- import java.awt.FontMetrics;
- public class Creative_Drawing {
- private JFrame frame;
- public Creative_Drawing() {
- frame = new JFrame("A Nice Place");
- frame.setSize(500, 500);
- frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- frame.setPreferredSize(frame.getSize());
- frame.add(new DrawFace(frame.getSize()));
- frame.pack();
- frame.setVisible(true);
- }
- public static void main(String... argv) {
- new Creative_Drawing();
- }
- public static class DrawFace extends JPanel implements MouseListener {
- /*
- * Declare Class Variables Here
- */
- int x = 20;
- int y = 20;
- boolean change= true;
- public DrawFace(Dimension dimension) {
- setSize(dimension);
- setPreferredSize(dimension);
- addMouseListener(this);
- }
- @Override
- public void paintComponent(Graphics g) {
- Graphics2D g2 = (Graphics2D)g;//g2 is the graphics object that we need to use
- //to draw things to the screen
- Dimension d = getSize();
- //create a background
- if (change==true)
- {
- Color skyblue = new Color(135,206,250);
- g2.setColor(skyblue);
- g2.fillRect(0, 0, d.width, d.height);
- /**/
- Color grey = new Color (169,169,169);
- Color land = new Color (34,139,34);
- Color brown = new Color (139,69,19);
- Color cloud = new Color (255,250,250);
- //Building:
- g2.setColor(grey);
- g2.fillRect(150,100, 200, 400);
- g2.setColor(Color.white);
- //Windows:
- g2.fillRect(175,150 ,50 , 50);
- g2.fillRect(275,150,50,50);
- g2.fillRect(175,250,50,50);
- g2.fillRect(275,250,50,50);
- //Sun:
- g2.setColor(Color.yellow);
- g2.fillOval(-50,-50,200,200);
- //land
- g2.setColor(land);
- g2.fillRect(0,500,600,100);
- //door
- g2.setColor(brown);
- g2.fillRect(225,400,50,100);
- //cloud;
- g2.setColor(cloud);
- g2.fillOval(300,-20,100,100);
- g2.fillOval(350,0,100,100);
- g2.fillOval(400,-30,100,100);
- g2.fillOval(370,-10,100,100);
- }
- else {
- Color skydarkblue = new Color(25,25,112);
- g2.setColor(skydarkblue);
- g2.fillRect(0, 0, d.width, d.height);
- /**/
- Color grey = new Color (169,169,169);
- Color land = new Color (34,139,34);
- Color brown = new Color (139,69,19);
- Color cloud = new Color (255,250,250);
- Color gold = new Color(255,215,0);
- Color moon = new Color(255,248,220);
- Color darkgrey = new Color(96,96,96);
- //Moon:
- g2.setColor(moon);
- g2.fillOval(-50,-50,200,200);
- g2.setColor(skydarkblue);
- g2.fillOval(30,30,150,150);
- //Building
- g2.setColor(grey);
- g2.fillRect(150,100, 200, 400);
- //Windows:
- g2.setColor(gold);
- g2.fillRect(175,150 ,50 , 50);
- g2.fillRect(275,150,50,50);
- g2.fillRect(175,250,50,50);
- g2.fillRect(275,250,50,50);
- //land
- g2.setColor(land);
- g2.fillRect(0,500,600,100);
- //door
- g2.setColor(brown);
- g2.fillRect(225,400,50,100);
- //cloud;
- g2.setColor(darkgrey);
- g2.fillOval(300,-20,100,100);
- g2.fillOval(350,0,100,100);
- g2.fillOval(400,-30,100,100);
- g2.fillOval(370,-10,100,100);
- //Stars
- g2.setColor(Color.white);
- g2.fillOval(100,200,2,2);
- g2.fillOval(60,130,2,2);
- g2.fillOval(300,200,2,2);
- g2.fillOval(200,10,2,2);
- g2.fillOval(100,200,2,2);
- g2.fillOval(400,400,2,2);
- g2.fillOval(230,50,2,2);
- g2.fillOval(500,200,2,2);
- g2.fillOval(430,190,2,2);
- g2.fillOval(580,300,2,2);
- g2.fillOval(230,50,2,2);
- g2.fillOval(10,50,2,2);
- g2.fillOval(200,400,2,2);
- g2.fillOval(10,470,2,2);
- g2.fillOval(70,370,2,2);
- }
- //display Text
- g2.setColor(Color.red);
- g2.setFont (new Font("TimesRoman", Font.PLAIN, 20));
- g2.drawString("The Highest Building in the world" , x,y);//text to display, x and y coordinates
- }
- public void mousePressed(MouseEvent e) {
- x = e.getX();
- y = e.getY();
- change = false;
- repaint();//updates the paint method
- }
- public void mouseReleased(MouseEvent e) {
- change = true;
- repaint();
- }
- public void mouseEntered(MouseEvent e) {
- }
- public void mouseExited(MouseEvent e) {
- }
- //DO NOT USE THE METHOD BELOW
- public void mouseClicked(MouseEvent e) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement