Question
1. The Reset button does nothing currently in the program. Implement additional code so that when the Reset button is clicked, the game level is
1.The Reset button does nothing currently in the program. Implement additional code so that when the Reset button is clicked, the game level is reset back to the initial level, and the number of hits and misses is reset to zero.
2.In the current program, the Duke does not move when the mouse is dragged while the button is being held down. Modify the program so that the Duke would move as the mouse is dragged also, in the same way as when the mouse is moved.
3.Currently, when the Duke is caught, a different Duke image with a waving hand is shown. There is no change to the image for a miss. Modify the program so that when a miss occurs, a new Duke image with hands down is displayed. Once the Duke moves to a new location, the image should return to the original as done currently in the program.
Duke Links
Duke image -https://imgur.com/T9DrSm5 Duke Miss image - https://imgur.com/mluS410 Duke Wave image - https://imgur.com/nKAjqzR
Code
import java.awt.*; import java.awt.event.*;
public class CatchDuke extends Frame implements ItemListener {
static final int MAX_LEVEL = 5; static final int INITIAL_LEVEL = 1;
// Labels and controls Label hitLabel; Label missLabel; Button reset; Choice levelChoice;
// the canvas DukeCanvas canvas;
/** * Constructor */ public CatchDuke() { setTitle("Catch the Duke"); setLayout(new BorderLayout());
// create display and control Panel topPanel = new Panel(new GridLayout(2, 1)); add(topPanel, BorderLayout.NORTH); Panel labelPanel = new Panel(new FlowLayout(FlowLayout.LEFT)); topPanel.add(labelPanel); Label label = new Label("Number of Hits:"); labelPanel.add(label); hitLabel = new Label(" 0"); labelPanel.add(hitLabel); label = new Label("Number of Misses:"); labelPanel.add(label); missLabel = new Label(" 0"); labelPanel.add(missLabel);
Panel controlPanel = new Panel(new FlowLayout(FlowLayout.LEFT)); topPanel.add(controlPanel); label = new Label("Select Level:"); controlPanel.add(label); levelChoice = new Choice(); for(int i=1; i<=MAX_LEVEL; i++) { levelChoice.add(" "+i); } controlPanel.add(levelChoice); levelChoice.select(INITIAL_LEVEL-1); levelChoice.addItemListener(this); reset = new Button("Reset"); controlPanel.add(reset);
// create the canvas canvas = new DukeCanvas(); add(canvas, BorderLayout.CENTER); canvas.setLevel(levelChoice.getSelectedIndex()); } // end of constructor
/** * Implementing ItemListener */ public void itemStateChanged(ItemEvent event) { canvas.setLevel(levelChoice.getSelectedIndex()); }
/** * the DukeCanvas class */ class DukeCanvas extends Canvas implements MouseListener, MouseMotionListener {
// duke images Image duke, dukeWave; // current position of duke int dukeX, dukeY; // size of duke int dukeWidth, dukeHeight; // number of hits and misses int hits = 0; int misses = 0; boolean hit = false; // level of difficulty int level = 0; public void setLevel(int lv) { level = lv; }
/** * Constructor */ public DukeCanvas() { setBackground(Color.white); // load images Toolkit toolkit = Toolkit.getDefaultToolkit(); duke = toolkit.getImage("duke.gif"); dukeWave = toolkit.getImage("dukeWave.gif");
addMouseListener(this); addMouseMotionListener(this); } // end of constructor
/** * painting the component */ public void paint(Graphics g) {
// get canvas width and height int canvasWidth = getSize().width-2; int canvasHeight = getSize().height-2; // draw a border g.drawRect(0, 0, getSize().width, getSize().height);
if(hit) { // display hit image g.drawImage(dukeWave, dukeX, dukeY, this); } else { // get image width and height dukeWidth = duke.getWidth(this); dukeHeight = duke.getHeight(this); // calculate the width and height of the display area int dWidth = canvasWidth - dukeWidth; int dHeight = canvasHeight - dukeHeight;
// generate a new position for the duke and draw it dukeX = (int)(Math.random()*1000) % dWidth; dukeY = (int)(Math.random()*1000) % dHeight; g.drawImage(duke, dukeX, dukeY, this); } }
/** * Implementing MouseListener */ public void mouseClicked(MouseEvent event) { if(hit) { // No successive hits allowed hit = false; repaint(); return; } int x = event.getX(); int y = event.getY(); if((dukeX < x && x < dukeX+dukeWidth) && (dukeY < y && y < dukeY+dukeHeight)) { hitLabel.setText(String.valueOf(++hits)); hit = true; } else { Toolkit.getDefaultToolkit().beep(); missLabel.setText(String.valueOf(++misses)); hit = false; } repaint(); }
public void mouseReleased(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {}
/** * Implementing MouseMotionListener */ public void mouseMoved(MouseEvent event) {
if(event.getX()%(MAX_LEVEL - level) == 0 && event.getY()%(MAX_LEVEL - level) == 0) { hit = false; repaint(); } }
public void mouseDragged(MouseEvent event) {} }
/** * the main method */ public static void main(String[] argv) { // Create a frame CatchDuke frame = new CatchDuke(); Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); frame.setSize(size.width-50, size.height-50); frame.setLocation(20, 20);
// add window closing listener frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { System.exit(0); } }); // Show the frame frame.setVisible(true); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started