Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default I need help adding a mouse click event to my code

    I new to java and i having a hard time adding a mouseClick event to my code. I need to get this done.

    Create a Person class that draws a person. The Person class should implement the following methods:
    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
    main a static method used for testing. It should display a person object and when you click on it the next body part should show, if all parts are visible, then it should be reset so it is no longer visible. This will be removed for the final game, but will allow you to test each piece as you go, which is a good programming practice. (See the hint below for help with handling the mouse clicking.)

    so far my code looks like this:

    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JApplet;
    import java.awt.event.*;


    public class Person extends JApplet implements MouseListener
    {
    private int turnsleft;
    private Circle Head;
    private Square Body;
    private Rectangle Arm;
    private Rectangle Arm2;
    private Rectangle leg;
    private Rectangle leg2;
    private Square hand;
    private Square hand2;
    private Rectangle feet;
    private Rectangle feet2;




    public void mouseClicked()
    {

    }

    public int getNumLeft() {
    return turnsleft;
    }

    public void reset(){
    turnsleft = 6;
    repaint();

    }
    public void showNext(){
    turnsleft = turnsleft -1;
    repaint();
    }

    public void init()
    {
    this.setSize(500, 400);

    Head = new Circle();
    Head.setX(250);
    Head.setY(70);
    Head.setRadius(40);
    Head.setFillColor(new Color(255,218,185));

    Body = new Square();
    Body.setX(190);
    Body.setY(100);
    Body.setSize(125);
    Body.setFillColor(Color.red);

    Arm = new Rectangle();
    Arm.setX(300);
    Arm.setY(80);
    Arm.setWidth(80);
    Arm.setHeight(50);
    Arm.setFillColor(Color.red);

    Arm2 = new Rectangle();
    Arm2.setX(125);
    Arm2.setY(80);
    Arm2.setWidth(80);
    Arm2.setHeight(50);
    Arm2.setFillColor(Color.red);

    leg = new Rectangle();
    leg.setX(290);
    leg.setY(220);
    leg.setWidth(60);
    leg.setHeight(70);
    leg.setFillColor(Color.red);

    leg2 = new Rectangle();
    leg2.setX(155);
    leg2.setY(220);
    leg2.setWidth(60);
    leg2.setHeight(70);
    leg2.setFillColor(Color.red);

    hand = new Square();
    hand.setX(120);
    hand.setY(50);
    hand.setSize(50);
    hand.setFillColor(new Color(255,218,185));

    hand2 = new Square();
    hand2.setX(335);
    hand2.setY(50);
    hand2.setSize(50);
    hand2.setFillColor(new Color(255,218,185));

    feet = new Rectangle();
    feet.setX(150);
    feet.setY(260);
    feet.setWidth(67);
    feet.setHeight(30);
    feet.setFillColor(new Color(255,218,185));

    feet2 = new Rectangle();
    feet2.setX(286);
    feet2.setY(260);
    feet2.setWidth(67);
    feet2.setHeight(30);
    feet2.setFillColor(new Color(255,218,185));
    }

    public void paint(Graphics g)
    {
    Body.paintComponent(g);
    Head.paintComponent(g);
    Arm.paintComponent(g);
    Arm2.paintComponent(g);
    hand.paintComponent(g);
    hand2.paintComponent(g);
    leg.paintComponent(g);
    leg2.paintComponent(g);
    feet2.paintComponent(g);
    feet.paintComponent(g);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub
    }
    @Override
    public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub
    }
    @Override
    public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub
    }
    @Override
    public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

    }
    @Override
    public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

    }


    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: I need help adding a mouse click event to my code

    I'd recommend a google search of "Java MouseListener" to help you get started.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. how to call mouse event code
    By such orb in forum New To Java
    Replies: 3
    Last Post: 09-16-2012, 11:11 PM
  2. Applet+Mouse click
    By Verbiage in forum New To Java
    Replies: 6
    Last Post: 04-18-2012, 09:00 PM
  3. some simple wait for a mouse click code
    By such orb in forum New To Java
    Replies: 2
    Last Post: 03-20-2012, 12:32 AM
  4. Mouse Click not working.
    By itcmanish in forum AWT / Swing
    Replies: 1
    Last Post: 01-12-2012, 03:25 PM
  5. mouse click alert
    By amir in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2008, 10:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •