Change the creature in this java program to a different one: // Creature.java Au
ID: 3794629 • Letter: C
Question
Change the creature in this java program to a different one:
// 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("happyFace.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 <= creatureX + creature.getIconWidth())
{
if (y >= creatureY && y <= creatureY + creature.getIconHeight())
{
catchCount++;
return true;
}
else
return false;
}
else
return false;
}
//-----------------------------------------------------------------
// 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);
}
}
TheCreaturePanel.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();
}
}
}
}
//********************************************************************
// 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);
}
}
Explanation / Answer
// Creature.java
//No need to make more changes just look at the pointInMe() method
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("happyface.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 <= creatureX + creature.getIconWidth())
{
if (y >= creatureY && y <= creatureY + creature.getIconHeight())
{
catchCount++;
return true;
}
else
return false;
}
else
return false;
}
//-----------------------------------------------------------------
// 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);
}
}
Output :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.