Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

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, dukeMiss; // 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; boolean miss = 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"); dukeMiss = toolkit.getImage("dukeMiss.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(miss ? dukeMiss : 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) { if(event.getX()%(MAX_LEVEL - level) == 0 && event.getY()%(MAX_LEVEL - level) == 0) { hit = false; repaint(); } } } /** * 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

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

Lab Manual For Database Development

Authors: Rachelle Reese

1st Custom Edition

1256741736, 978-1256741732

More Books

Students also viewed these Databases questions