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

import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import jav

ID: 3638480 • Letter: I

Question

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

class Dice extends JLabel {
private int value;

Dice() {
super();
setValue(0);

addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
setValue(getValue() + 1);
}
});
}

public int getValue() {
return value;
}

void setValue(int value) {
this.value = value;
switch (value) {
case 0:
this.setIcon(getImage("images\one.GIF"));
break;
case 1:
this.setIcon(getImage("images\two.GIF"));
break;
case 2:
this.setIcon(getImage("images\three.GIF"));
break;
case 3:
this.setIcon(getImage("images\four.GIF"));
break;
case 4:
this.setIcon(getImage("images\five.GIF"));
break;
case 5:
this.setIcon(getImage("images\six.GIF"));
break;
}
}


ImageIcon getImage(String name) {
Object o = getClass().getResource(name);
return new ImageIcon(getClass().getResource(name));
}

public static void main(String[] a) {
JPanel panel = new JPanel();
Dice dice1 = new Dice();

dice1.setValue(2);

panel.add(dice1);

JFrame app = new JFrame();
app.getContentPane().add(panel);
app.setLocation(300, 200);
app.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
app.pack();
app.show();
}

}








2) In the dice folder there is a program Dice.java. Examine it, compile and run it. Not that when the dice is clicked it displays the next number.




a. What instance variables (instance variables are inside the class { }, but not inside of any method (function)) does it declare?


b. What are the data types of the variables? (Hint: Java is like C. Thus the data type precedes the name of the variable.


c. What kind of objects does the main method instantiate (i.e. what does it new)? (hint: there are 3 different kind of objects being instantiated, i.e., created)


d. Change the program so that the dice is initially displayed as a 5. Compile it and test it. Hint: in the main the value for the dice is being set by calling a method on the dice – be careful the internal representation of the dice value is NOT one based)

e. There is a bug in the program in that on clicking the dice when it displays a 6 is not doing anything. Change the program so that is displays a one when clicking on the dice after is displays a 6. Note: mouse clicks are being processed by the mouseClicked method. Compile it and test it. (Hint, use an if statement with a test against the value returned by getValue() and then use the setValue method to assign the correct value.

f. Change the program so that is instantiates two and adds 2 Dice to the JPanel object. Compile it and test it. (Hint: modify the main method in a similar fashion as problem 1 d)

Explanation / Answer

Hi, my friend hope this will help import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.WindowConstants; import java.util.ArrayList; import java.awt.event.MouseMotionListener; import java.awt.Image; import java.awt.Graphics; import java.awt.event.MouseListener; public class BouncingBalls extends JPanel implements Runnable, MouseMotionListener, MouseListener { Thread runner; Image buffer; Graphics bufferGraphics; ArrayList collideBalls = new ArrayList(); ArrayList obstacles = new ArrayList(); boolean intro = true; public static void main(String[] args) { JFrame frame = new JFrame("Bouncing Balls"); BouncingBalls clickBall = new BouncingBalls(); frame.getContentPane().add(clickBall); frame.setSize(500, 500); frame.setLocation(50, 50); frame.show(); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote