Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java prog (use JavaFx, not Swing) Design and implement an application that plays

ID: 3678486 • Letter: J

Question

Java prog (use JavaFx, not Swing)

Design and implement an application that plays the game Catch-the-Creature. Use an image or icon to represent the creature. Have the creature appear at random locations, then disappear and reappear somewhere else. The goal for the player is to "catch" the creature by pressing the mouse button while the mouse pointer is on the creature image. Create a separate class to represent the creature, and include in it a method that determines if the location of the mouse click corresponds to the current location of the creature. Display a count of the number of times the creature is caught.

Explanation / Answer

Main Code:

import javax.swing.*;

public class Catch_The_Creature
{

public static void main(String[] args)
{
JFrame frame = new JFrame("Catch the Creature");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Creature panel = new Creature();

JOptionPane.showMessageDialog(frame, "Catch Pikachu!");

frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);

}

}

Creature Code:

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

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


public class Creature extends JPanel
{
private final int WIDTH = 400, HEIGHT = 300;
private final int DELAY=20, IMAGE_SIZE = 60;

private ImageIcon image;
private int pikachuX, pikachuY;
private int x, y;
private int catchCount=0;
private static Random generator = new Random();
private Timer time;
private ActionListener updater;
private JLabel countLabel;

public Creature()
{
image = new ImageIcon("image/pikachu.jpg");
time = new Timer(DELAY, updater);

addMouseListener ((MouseListener) new MouseClickedListener());
setBackground (Color.green);


setPreferredSize(new Dimension(1900,1000));
time.start();

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote