![]() |
I need help adding the mouse click events & some other small things
This is my assignment:
The goal of this project is to become more familiar with creating and using classes and objects in Java. In order to do so, you will create a Person class. Your class will keep track of how many parts are not shown, display the next item when a method is called, and reset. I need to have these methods included. paintComponent – with the same signature/header of the other paintComponent methods we have written. This will paint the person. getNumLeft – returns the number of turns the player has left (e.g. 6 at the beginning assuming head, body, left/right arm/leg). Hint: this is named like a getter/accessor method for a reason. • reset – which resets the number of turns left and erases the person so it is not visible • showNext – progressively causes the next body part to be shown: head, body, left arm, right arm, left leg, right leg This is the code I have written so far (along with the Circle and Square, which aren't included). import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.text.DecimalFormat; import javax.swing.JApplet; public class Person extends JApplet implements MouseListener { private Square torso; // The torso private Square lowertorso; private Square hip; // The left leg private Square thigh; private Square legA3; private Square legA4; private Square hip2; //The right leg private Square thigh2; private Square legB3; private Square legB4; private Square shoulder; //The left arm private Square bicep; private Square armA3; private Square armA4; private Square hand; private Square shoulder2; //The left leg private Square bicep2; private Square armB3; private Square armB4; private Square hand2; private Circle head; //Obviously the head public int x; public void init() { this.setSize(400, 600); this.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { // add code so if not all shown, the next body part // is shown, otherwise the person is reset } }); torso = new Square(150, 125, 100, Color.green); lowertorso = new Square(150, 225, 100, Color.red); hip = new Square(155, 325, 35, Color.black); thigh = new Square(155, 360, 35, Color.blue); legA3 = new Square(155, 395, 35, Color.blue); legA4 = new Square(155, 430, 35, Color.black); hip2 = new Square(210, 325, 35, Color.black); thigh2 = new Square(210, 360, 35, Color.blue); legB3 = new Square(210, 395, 35, Color.blue); legB4 = new Square(210, 430, 35, Color.black); shoulder = new Square(115, 150, 35, Color.green); bicep = new Square(115, 185, 35, Color.green); armA3 = new Square(115, 215, 35, Color.blue); armA4 = new Square(115, 250, 35, Color.blue); hand = new Square(115, 285, 35, Color.black); shoulder2 = new Square(250, 150, 35, Color.green); bicep2 = new Square(250, 185, 35, Color.green); armB3 = new Square(250, 215, 35, Color.blue); armB4 = new Square(250, 250, 35, Color.blue); hand2 = new Square(250, 285, 35, Color.black); head = new Circle(200, 100, 30, Color.black); } public void paint(Graphics g) { torso.paintComponent(g); lowertorso.paintComponent(g); hip.paintComponent(g); thigh.paintComponent(g); legA3.paintComponent(g); legA4.paintComponent(g); hip2.paintComponent(g); thigh2.paintComponent(g); legB3.paintComponent(g); legB4.paintComponent(g); shoulder.paintComponent(g); bicep.paintComponent(g); armA3.paintComponent(g); armA4.paintComponent(g); hand.paintComponent(g); shoulder2.paintComponent(g); bicep2.paintComponent(g); armB3.paintComponent(g); armB4.paintComponent(g); hand2.paintComponent(g); head.paintComponent(g); } public int getNumLeft() { return x; } public void paintComponent(Graphics g) { Color tc = g.getColor(); g.setColor(fillColor); g.fillRect(x, y, size, size); g.setColor(Color.black); DecimalFormat df = new DecimalFormat("0.##"); g.drawString("You are now " + df.format(getNumLeft()) + " limb less of being a cripple. Keep on going!", 0, 0 + 15); g.setColor(tc); } } |
| All times are GMT +1. The time now is 01:14 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.