Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How do you include creature.gif and happyFace.gif? //******************************************************************** // CatchTheCreature.java Author: Lewis/Loftus // // Solution to Programming Project 9.12 //******************************************************************** import javax.swing.JFrame; public class CatchTheCreature

How do you include "creature.gif" and "happyFace.gif"?

//******************************************************************** // CatchTheCreature.java Author: Lewis/Loftus // // Solution to Programming Project 9.12 //********************************************************************

import javax.swing.JFrame;

public class CatchTheCreature { //----------------------------------------------------------------- // Displays the main frame of the program. //----------------------------------------------------------------- public static void main(String[] args) { JFrame frame = new JFrame("Catch The Creature"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new CatchTheCreaturePanel()); frame.pack(); frame.setVisible(true); } }

//******************************************************************** // CatchTheCreaturePanel.java Author: Lewis/Loftus // // Solution to Programming Project 9.12 //********************************************************************

import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Random;

public class CatchTheCreaturePanel extends JPanel { private Creature creature; private Random gen; private Timer animate;

private final int INITIAL_X = 20, INITIAL_Y = 100; private final int MOVE_ODDS = 4; // 1 in 4 chance the creature will move private final int DELAY = 500;

//----------------------------------------------------------------- // Sets up the panel. //----------------------------------------------------------------- public CatchTheCreaturePanel() { creature = new Creature(INITIAL_X, INITIAL_Y); gen = new Random();

addMouseListener(new Catcher());

setBackground(Color.white); setPreferredSize(new Dimension(500, 500));

animate = new Timer(DELAY, new RandomMover()); animate.start(); }

//----------------------------------------------------------------- // Draws the creature and the number of catches //----------------------------------------------------------------- public void paintComponent(Graphics page) { super.paintComponent(page); creature.draw(this, page); String catchString = "Catches: " + creature.getCatchCount(); page.drawString(catchString, 10, 20); String missString = "Misses: " + creature.getMissCount(); page.drawString(missString, 10, 40); }

//***************************************************************** // Inner class that listens for mouse presses. If the mouse is // pressed inside the creature, the move the creature and // repaint the screen. //***************************************************************** private class Catcher extends MouseAdapter { public void mouseClicked(MouseEvent event) { if (creature.pointInMe(event.getX(), event.getY())) creature.move(getSize()); repaint (); } }

//***************************************************************** // Inner class that listens for the timer to move the creature and // repaint the screen during the animation. Moves the create based // on MOVE_ODDS (may not move every time the timer fires). //***************************************************************** private class RandomMover implements ActionListener { public void actionPerformed(ActionEvent event) { int shouldMove = gen.nextInt(MOVE_ODDS); if (shouldMove == 0) { creature.move(getSize()); repaint(); } } } }

//******************************************************************** // Creature.java Author: Lewis/Loftus // // Solution to Programming Project 9.12 //********************************************************************

import javax.swing.*; import java.awt.*; import java.util.Random;

public class Creature { private int creatureX, creatureY, clickCount, catchCount; private ImageIcon creature; private Random gen;

//----------------------------------------------------------------- // Creates the creature. //----------------------------------------------------------------- public Creature(int initialX, int initialY) { creature = new ImageIcon("creature.gif"); creatureX = initialX; creatureY = initialY; clickCount = catchCount = 0; gen = new Random(); }

//----------------------------------------------------------------- // Moves the creature to a random location within the play area. //----------------------------------------------------------------- public void move(Dimension area) { creatureX = gen.nextInt(area.width - creature.getIconWidth()); creatureY = gen.nextInt(area.height - creature.getIconHeight()); }

//----------------------------------------------------------------- // Returns true if point (x , y) is in the creature and increments // the catch count, else returns false. //----------------------------------------------------------------- public boolean pointInMe(int x, int y) { clickCount++; if (x >= creatureX && x = creatureY && y

//----------------------------------------------------------------- // Returns the number of catches. //----------------------------------------------------------------- public int getCatchCount() { return catchCount; }

//----------------------------------------------------------------- // Returns the number of misses. //----------------------------------------------------------------- public int getMissCount() { return clickCount - catchCount; }

//----------------------------------------------------------------- // Draws the creature on the specified component. //----------------------------------------------------------------- public void draw(Component panel, Graphics page) { creature.paintIcon(panel, page, creatureX, creatureY); } }

image text in transcribed creature.gif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Upgrading Oracle Databases Oracle Database New Features

Authors: Charles Kim, Gary Gordhamer, Sean Scott

1st Edition

B0BL12WFP6, 979-8359657501

More Books

Students also viewed these Databases questions

Question

What do you want from your leader?

Answered: 1 week ago

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago