Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. import javax.swing.JFrame;
  2. import javax.swing.JPanel;
  3. import javax.swing.WindowConstants;
  4. import java.awt.Dimension;
  5. import java.awt.Color;
  6. import java.awt.Graphics;
  7. import java.awt.Graphics2D;
  8. import java.awt.Point;
  9. import java.awt.event.MouseListener;
  10. import java.awt.event.MouseEvent;
  11. import java.awt.Font;
  12. import java.awt.FontMetrics;
  13.  
  14. public class Creative_Drawing {
  15. private JFrame frame;
  16.  
  17. public Creative_Drawing() {
  18. frame = new JFrame("A Nice Place");
  19. frame.setSize(500, 500);
  20. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  21. frame.setPreferredSize(frame.getSize());
  22. frame.add(new DrawFace(frame.getSize()));
  23. frame.pack();
  24. frame.setVisible(true);
  25. }
  26.  
  27. public static void main(String... argv) {
  28. new Creative_Drawing();
  29. }
  30.  
  31. public static class DrawFace extends JPanel implements MouseListener {
  32. /*
  33. * Declare Class Variables Here
  34. */
  35. int x = 20;
  36. int y = 20;
  37. boolean change= true;
  38. public DrawFace(Dimension dimension) {
  39. setSize(dimension);
  40. setPreferredSize(dimension);
  41. addMouseListener(this);
  42.  
  43. }
  44.  
  45. @Override
  46. public void paintComponent(Graphics g) {
  47. Graphics2D g2 = (Graphics2D)g;//g2 is the graphics object that we need to use
  48. //to draw things to the screen
  49. Dimension d = getSize();
  50. //create a background
  51.  
  52. if (change==true)
  53. {
  54. Color skyblue = new Color(135,206,250);
  55. g2.setColor(skyblue);
  56. g2.fillRect(0, 0, d.width, d.height);
  57. /**/
  58.  
  59. Color grey = new Color (169,169,169);
  60. Color land = new Color (34,139,34);
  61. Color brown = new Color (139,69,19);
  62. Color cloud = new Color (255,250,250);
  63. //Building:
  64. g2.setColor(grey);
  65. g2.fillRect(150,100, 200, 400);
  66. g2.setColor(Color.white);
  67. //Windows:
  68. g2.fillRect(175,150 ,50 , 50);
  69. g2.fillRect(275,150,50,50);
  70. g2.fillRect(175,250,50,50);
  71. g2.fillRect(275,250,50,50);
  72. //Sun:
  73. g2.setColor(Color.yellow);
  74. g2.fillOval(-50,-50,200,200);
  75. //land
  76. g2.setColor(land);
  77. g2.fillRect(0,500,600,100);
  78. //door
  79. g2.setColor(brown);
  80. g2.fillRect(225,400,50,100);
  81. //cloud;
  82. g2.setColor(cloud);
  83. g2.fillOval(300,-20,100,100);
  84. g2.fillOval(350,0,100,100);
  85. g2.fillOval(400,-30,100,100);
  86. g2.fillOval(370,-10,100,100);
  87. }
  88. else {
  89.  
  90. Color skydarkblue = new Color(25,25,112);
  91. g2.setColor(skydarkblue);
  92. g2.fillRect(0, 0, d.width, d.height);
  93. /**/
  94.  
  95. Color grey = new Color (169,169,169);
  96. Color land = new Color (34,139,34);
  97. Color brown = new Color (139,69,19);
  98. Color cloud = new Color (255,250,250);
  99. Color gold = new Color(255,215,0);
  100. Color moon = new Color(255,248,220);
  101. Color darkgrey = new Color(96,96,96);
  102. //Moon:
  103. g2.setColor(moon);
  104. g2.fillOval(-50,-50,200,200);
  105. g2.setColor(skydarkblue);
  106. g2.fillOval(30,30,150,150);
  107.  
  108. //Building
  109. g2.setColor(grey);
  110. g2.fillRect(150,100, 200, 400);
  111.  
  112. //Windows:
  113. g2.setColor(gold);
  114. g2.fillRect(175,150 ,50 , 50);
  115. g2.fillRect(275,150,50,50);
  116. g2.fillRect(175,250,50,50);
  117. g2.fillRect(275,250,50,50);
  118.  
  119. //land
  120. g2.setColor(land);
  121. g2.fillRect(0,500,600,100);
  122. //door
  123. g2.setColor(brown);
  124. g2.fillRect(225,400,50,100);
  125. //cloud;
  126. g2.setColor(darkgrey);
  127. g2.fillOval(300,-20,100,100);
  128. g2.fillOval(350,0,100,100);
  129. g2.fillOval(400,-30,100,100);
  130. g2.fillOval(370,-10,100,100);
  131. //Stars
  132. g2.setColor(Color.white);
  133. g2.fillOval(100,200,2,2);
  134. g2.fillOval(60,130,2,2);
  135. g2.fillOval(300,200,2,2);
  136. g2.fillOval(200,10,2,2);
  137. g2.fillOval(100,200,2,2);
  138. g2.fillOval(400,400,2,2);
  139. g2.fillOval(230,50,2,2);
  140. g2.fillOval(500,200,2,2);
  141. g2.fillOval(430,190,2,2);
  142. g2.fillOval(580,300,2,2);
  143. g2.fillOval(230,50,2,2);
  144. g2.fillOval(10,50,2,2);
  145. g2.fillOval(200,400,2,2);
  146. g2.fillOval(10,470,2,2);
  147. g2.fillOval(70,370,2,2);
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154. }
  155. //display Text
  156. g2.setColor(Color.red);
  157. g2.setFont (new Font("TimesRoman", Font.PLAIN, 20));
  158. g2.drawString("The Highest Building in the world" , x,y);//text to display, x and y coordinates
  159.  
  160.  
  161.  
  162. }
  163.  
  164.  
  165. public void mousePressed(MouseEvent e) {
  166. x = e.getX();
  167. y = e.getY();
  168.  
  169. change = false;
  170. repaint();//updates the paint method
  171. }
  172.  
  173. public void mouseReleased(MouseEvent e) {
  174. change = true;
  175. repaint();
  176. }
  177.  
  178. public void mouseEntered(MouseEvent e) {
  179.  
  180. }
  181.  
  182. public void mouseExited(MouseEvent e) {
  183. }
  184. //DO NOT USE THE METHOD BELOW
  185. public void mouseClicked(MouseEvent e) {
  186.  
  187. }
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement