Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // General information about this program
- /**
- * 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.
- *
- * @ Tara Nadella
- * @ 6/30/2019
- */
- import java.util.*;
- // Setting everything up...importing all the tools needed to make the drawing and the interactive 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 DrawingApp { // The class header
- private JFrame frame;
- public DrawingApp() { // The main method
- frame = new JFrame("Mine Sweeper");
- frame.setSize(600, 400);
- frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- frame.setPreferredSize(frame.getSize());
- frame.add(new DrawPupAndBone(frame.getSize()));
- frame.pack();
- frame.setVisible(true);
- }
- public static void main(String... argv) {
- new DrawingApp();
- }
- public static class DrawPupAndBone extends JPanel implements MouseListener {
- /*
- * Declaring class variables
- */
- int x = 20;
- int y = 20;
- boolean boneBite = false;
- public DrawPupAndBone(Dimension dimension) {
- setSize(dimension);
- setPreferredSize(dimension);
- addMouseListener(this);
- }
- @Override
- // Drawing the shapes and text
- 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();
- // Creating the background
- g2.setColor(Color.white);
- g2.fillRect(0, 0, d.width, d.height);
- /**/
- // Creating new Colours
- Color brown = new Color(50,100,0); // A combination of R, B, and G that creates a brown colour
- Color tan = new Color(101,50,0); // A combination of R, B, and G that creates a tan colour
- Color silver = new Color (100, 100, 100); // A combination of R, B, and G that creates a silver colour
- // Drawing the Bone
- g2.setColor(brown); // Setting the color
- g2.fillOval(100, 100, 150, 150); // Setting the coordinates and size
- g2.setColor(brown); // Setting the color
- g2.fillOval(450, 100, 150, 150); // Setting the coordinates and size
- g2.setColor(brown); // Setting the color
- g2.fillOval(100, 200, 150, 150); // Setting the coordinates and size
- g2.setColor(brown); // Setting the color
- g2.fillOval(450, 200, 150, 150); // Setting the coordinates and size
- g2.setColor(brown); // Setting the color
- g2.fillRect(150, 162, 400, 113); // Setting the coordinates and size
- // Setting up a condition that will cause a bite mark to appear on the screen if "boneBite" is true.
- if(boneBite==true){
- //Bite
- g2.setColor(Color.white); // Setting the color
- g2.fillOval(550, 140, 75, 75); // Setting the coordinates and size
- g2.setColor(Color.white); // Setting the color
- g2.fillOval(520, 185, 75, 75); // Setting the coordinates and size
- g2.setColor(Color.white); // Setting the color
- g2.fillOval(550, 220, 75, 75); // Setting the coordinates and size
- }
- // Drawing the face
- g2.setColor(silver); // Setting the color
- g2.fillRect(650, 100, 250, 250); // Setting the coordinates and size
- g2.setColor(Color.white); // Setting the color
- g2.fillOval(625, 270, 300, 350); // Setting the coordinates and size
- g2.setColor(silver); // Setting the color
- g2.fillRect(650, 340, 250, 10); // Setting the coordinates and size
- // Drawing the eyes
- g2.setColor(Color.white); // Setting the color
- g2.fillOval(700, 145, 50, 70); // Setting the coordinates and size
- g2.setColor(Color.white); // Setting the color
- g2.fillOval(800, 145, 50, 70); // Setting the coordinates and size
- g2.setColor(Color.black); // Setting the color
- g2.fillOval(705, 160, 30, 30); // Setting the coordinates and size
- g2.setColor(Color.black); // Setting the color
- g2.fillOval(805, 160, 30, 30); // Setting the coordinates and size
- // Drawing the smile
- g2.setColor(Color.black); // Setting the color
- g2.fillOval(745, 275, 60, 60); // Setting the coordinates and size
- g2.setColor(Color.white); // Setting the color
- g2.fillOval(745, 270, 60, 60); // Setting the coordinates and size
- // Drawing the nose
- g2.setColor(Color.black); // Setting the color
- g2.fillRect(765, 280, 20, 20); // Setting the coordinates and size
- g2.setColor(Color.black); // Setting the color
- g2.fillOval(765, 285, 20, 20); // Setting the coordinates and size
- g2.setColor(Color.black); // Setting the color
- g2.fillRect(772, 285, 5, 50); // Setting the coordinates and size
- // Drawing the ears
- g2.setColor(tan); // Setting the color
- g2.fillOval(630, 100, 50, 200); // Setting the coordinates and size
- g2.setColor(tan); // Setting the color
- g2.fillOval(870, 100, 50, 200); // Setting the coordinates and size
- // Drawing the body
- g2.setColor(silver); // Setting the color
- g2.fillRect(755, 345, 450, 200); // Setting the coordinates and size
- // Drawing the legs
- g2.setColor(tan); // Setting the color
- g2.fillRect(810, 545, 45, 200); // Setting the coordinates and size
- g2.setColor(tan); // Setting the color
- g2.fillRect(900, 545, 45, 200); // Setting the coordinates and size
- g2.setColor(tan); // Setting the color
- g2.fillRect(1010, 545, 45, 200); // Setting the coordinates and size
- g2.setColor(tan); // Setting the color
- g2.fillRect(1100, 545, 45, 200); // Setting the coordinates and size
- // Drawing the feet
- g2.setColor(Color.black); // Setting the color
- g2.fillOval(785, 730, 70, 30); // Setting the coordinates and size
- g2.setColor(Color.black); // Setting the color
- g2.fillOval(875, 730, 70, 30); // Setting the coordinates and size
- g2.setColor(Color.black); // Setting the color
- g2.fillOval(985, 730, 70, 30); // Setting the coordinates and size
- g2.setColor(Color.black); // Setting the color
- g2.fillOval(1075, 730, 70, 30); // Setting the coordinates and size
- // Displaying the text "Hungry Puppy!"
- g2.setColor(Color.red); // Setting the color
- g2.setFont (new Font("TimesRoman", Font.PLAIN, 20)); // Setting the font and size of the text
- g2.drawString("Hungry Puppy!" , x,y); //Setting the text to be displayed and it's position
- }
- public void mousePressed(MouseEvent e) {
- x = e.getX();
- y = e.getY(); // Getting the text to follow the mouse
- boneBite = true; // Yes bite mark appears when mouse is held down
- repaint();// Updating the paint method
- }
- public void mouseReleased(MouseEvent e) {
- boneBite = false; // No bite mark appears when mouse click is released
- repaint(); // Updating the paint method
- }
- public void mouseEntered(MouseEvent e) {
- }
- public void mouseExited(MouseEvent e) {
- }
- public void mouseClicked(MouseEvent e) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement