Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.37 KB | None | 0 0
  1. // General information about this program
  2. /**
  3. * This program draws a dog, and as long as the mouse is held down a bite mark appears in a bone and where the mouse cicks the screen, the text "Hungry Puppy" appears.
  4. *
  5. * @ Tara Nadella
  6. * @ 6/30/2019
  7. */
  8.  
  9. import java.util.*;
  10.  
  11. // Setting everything up...importing all the tools needed to make the drawing and the interactive features
  12. import javax.swing.JFrame;
  13. import javax.swing.JPanel;
  14. import javax.swing.WindowConstants;
  15. import java.awt.Dimension;
  16. import java.awt.Color;
  17. import java.awt.Graphics;
  18. import java.awt.Graphics2D;
  19. import java.awt.Point;
  20. import java.awt.event.MouseListener;
  21. import java.awt.event.MouseEvent;
  22. import java.awt.Font;
  23. import java.awt.FontMetrics;
  24.  
  25. public class DrawingApp { // The class header
  26. private JFrame frame;
  27. public DrawingApp() { // The main method
  28. frame = new JFrame("Mine Sweeper");
  29. frame.setSize(600, 400);
  30. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  31. frame.setPreferredSize(frame.getSize());
  32. frame.add(new DrawPupAndBone(frame.getSize()));
  33. frame.pack();
  34. frame.setVisible(true);
  35. }
  36. public static void main(String... argv) {
  37. new DrawingApp();
  38. }
  39. public static class DrawPupAndBone extends JPanel implements MouseListener {
  40. /*
  41. * Declaring class variables
  42. */
  43. int x = 20;
  44. int y = 20;
  45. boolean boneBite = false;
  46. public DrawPupAndBone(Dimension dimension) {
  47. setSize(dimension);
  48. setPreferredSize(dimension);
  49. addMouseListener(this);
  50. }
  51. @Override
  52. // Drawing the shapes and text
  53. public void paintComponent(Graphics g) {
  54. Graphics2D g2 = (Graphics2D)g;// g2 is the graphics object that we need to use
  55.  
  56. // To draw things to the screen
  57. Dimension d = getSize();
  58.  
  59. // Creating the background
  60. g2.setColor(Color.white);
  61. g2.fillRect(0, 0, d.width, d.height);
  62. /**/
  63.  
  64. // Creating new Colours
  65. Color brown = new Color(50,100,0); // A combination of R, B, and G that creates a brown colour
  66. Color tan = new Color(101,50,0); // A combination of R, B, and G that creates a tan colour
  67. Color silver = new Color (100, 100, 100); // A combination of R, B, and G that creates a silver colour
  68.  
  69. // Drawing the Bone
  70. g2.setColor(brown); // Setting the color
  71. g2.fillOval(100, 100, 150, 150); // Setting the coordinates and size
  72.  
  73. g2.setColor(brown); // Setting the color
  74. g2.fillOval(450, 100, 150, 150); // Setting the coordinates and size
  75.  
  76. g2.setColor(brown); // Setting the color
  77. g2.fillOval(100, 200, 150, 150); // Setting the coordinates and size
  78.  
  79. g2.setColor(brown); // Setting the color
  80. g2.fillOval(450, 200, 150, 150); // Setting the coordinates and size
  81.  
  82. g2.setColor(brown); // Setting the color
  83. g2.fillRect(150, 162, 400, 113); // Setting the coordinates and size
  84.  
  85. // Setting up a condition that will cause a bite mark to appear on the screen if "boneBite" is true.
  86. if(boneBite==true){
  87. //Bite
  88. g2.setColor(Color.white); // Setting the color
  89. g2.fillOval(550, 140, 75, 75); // Setting the coordinates and size
  90.  
  91. g2.setColor(Color.white); // Setting the color
  92. g2.fillOval(520, 185, 75, 75); // Setting the coordinates and size
  93.  
  94. g2.setColor(Color.white); // Setting the color
  95. g2.fillOval(550, 220, 75, 75); // Setting the coordinates and size
  96. }
  97.  
  98. // Drawing the face
  99. g2.setColor(silver); // Setting the color
  100. g2.fillRect(650, 100, 250, 250); // Setting the coordinates and size
  101.  
  102. g2.setColor(Color.white); // Setting the color
  103. g2.fillOval(625, 270, 300, 350); // Setting the coordinates and size
  104.  
  105. g2.setColor(silver); // Setting the color
  106. g2.fillRect(650, 340, 250, 10); // Setting the coordinates and size
  107.  
  108. // Drawing the eyes
  109. g2.setColor(Color.white); // Setting the color
  110. g2.fillOval(700, 145, 50, 70); // Setting the coordinates and size
  111.  
  112. g2.setColor(Color.white); // Setting the color
  113. g2.fillOval(800, 145, 50, 70); // Setting the coordinates and size
  114.  
  115. g2.setColor(Color.black); // Setting the color
  116. g2.fillOval(705, 160, 30, 30); // Setting the coordinates and size
  117.  
  118. g2.setColor(Color.black); // Setting the color
  119. g2.fillOval(805, 160, 30, 30); // Setting the coordinates and size
  120.  
  121. // Drawing the smile
  122. g2.setColor(Color.black); // Setting the color
  123. g2.fillOval(745, 275, 60, 60); // Setting the coordinates and size
  124.  
  125. g2.setColor(Color.white); // Setting the color
  126. g2.fillOval(745, 270, 60, 60); // Setting the coordinates and size
  127.  
  128. // Drawing the nose
  129. g2.setColor(Color.black); // Setting the color
  130. g2.fillRect(765, 280, 20, 20); // Setting the coordinates and size
  131.  
  132. g2.setColor(Color.black); // Setting the color
  133. g2.fillOval(765, 285, 20, 20); // Setting the coordinates and size
  134.  
  135. g2.setColor(Color.black); // Setting the color
  136. g2.fillRect(772, 285, 5, 50); // Setting the coordinates and size
  137.  
  138. // Drawing the ears
  139. g2.setColor(tan); // Setting the color
  140. g2.fillOval(630, 100, 50, 200); // Setting the coordinates and size
  141.  
  142. g2.setColor(tan); // Setting the color
  143. g2.fillOval(870, 100, 50, 200); // Setting the coordinates and size
  144.  
  145. // Drawing the body
  146. g2.setColor(silver); // Setting the color
  147. g2.fillRect(755, 345, 450, 200); // Setting the coordinates and size
  148.  
  149. // Drawing the legs
  150. g2.setColor(tan); // Setting the color
  151. g2.fillRect(810, 545, 45, 200); // Setting the coordinates and size
  152.  
  153. g2.setColor(tan); // Setting the color
  154. g2.fillRect(900, 545, 45, 200); // Setting the coordinates and size
  155.  
  156. g2.setColor(tan); // Setting the color
  157. g2.fillRect(1010, 545, 45, 200); // Setting the coordinates and size
  158.  
  159. g2.setColor(tan); // Setting the color
  160. g2.fillRect(1100, 545, 45, 200); // Setting the coordinates and size
  161.  
  162. // Drawing the feet
  163. g2.setColor(Color.black); // Setting the color
  164. g2.fillOval(785, 730, 70, 30); // Setting the coordinates and size
  165.  
  166. g2.setColor(Color.black); // Setting the color
  167. g2.fillOval(875, 730, 70, 30); // Setting the coordinates and size
  168.  
  169. g2.setColor(Color.black); // Setting the color
  170. g2.fillOval(985, 730, 70, 30); // Setting the coordinates and size
  171.  
  172. g2.setColor(Color.black); // Setting the color
  173. g2.fillOval(1075, 730, 70, 30); // Setting the coordinates and size
  174.  
  175. // Displaying the text "Hungry Puppy!"
  176. g2.setColor(Color.red); // Setting the color
  177. g2.setFont (new Font("TimesRoman", Font.PLAIN, 20)); // Setting the font and size of the text
  178. g2.drawString("Hungry Puppy!" , x,y); //Setting the text to be displayed and it's position
  179. }
  180. public void mousePressed(MouseEvent e) {
  181. x = e.getX();
  182. y = e.getY(); // Getting the text to follow the mouse
  183.  
  184. boneBite = true; // Yes bite mark appears when mouse is held down
  185.  
  186. repaint();// Updating the paint method
  187. }
  188. public void mouseReleased(MouseEvent e) {
  189. boneBite = false; // No bite mark appears when mouse click is released
  190. repaint(); // Updating the paint method
  191. }
  192. public void mouseEntered(MouseEvent e) {
  193. }
  194. public void mouseExited(MouseEvent e) {
  195. }
  196. public void mouseClicked(MouseEvent e) {
  197. }
  198. }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement