Results 1 to 2 of 2
  1. #1
    backinqueez is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default MouseAdapter Class?

    okay well, im having trouble with this. im suppose to have the smiley face stop when i click it and resume when i click it again, can anybody help?

    heres my code so far:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class ReboundPanel extends JPanel
    {
    private final int WIDTH = 300, HEIGHT = 100;
    private final int DELAY = 20, IMAGE_SIZE = 35;

    private ImageIcon image;
    private Timer timer;
    private int x, y, moveX, moveY;

    //-----------------------------------------------------------------
    // Sets up the panel, including the timer for the animation.
    //-----------------------------------------------------------------
    public ReboundPanel()
    {
    timer = new Timer(DELAY, new ReboundListener());

    image = new ImageIcon ("happyFace.gif");

    x = 0;
    y = 40;
    moveX = moveY = 3;

    setPreferredSize (new Dimension(WIDTH, HEIGHT));
    setBackground (Color.black);
    timer.start();
    }

    //-----------------------------------------------------------------
    // Draws the image in the current location.
    //-----------------------------------------------------------------
    public void paintComponent (Graphics page)
    {
    super.paintComponent (page);
    image.paintIcon (this, page, x, y);
    }

    //************************************************** ***************
    // Represents the action listener for the timer.
    //************************************************** ***************
    private class ReboundListener implements ActionListener
    {
    //--------------------------------------------------------------
    // Updates the position of the image and possibly the direction
    // of movement whenever the timer fires an action event.
    //--------------------------------------------------------------
    public void actionPerformed (ActionEvent event)
    {
    x += moveX;
    y += moveY;

    if (x <= 0 || x >= WIDTH-IMAGE_SIZE)
    moveX = moveX * -1;

    if (y <= 0 || y >= HEIGHT-IMAGE_SIZE)
    moveY = moveY * -1;

    repaint();
    }
    }

    private class ReboundListener1 extends MouseAdapter{

    public void mouseClicked (MouseEvent event)
    {
    if(event.getButton()==MouseEvent.BUTTON1)
    {
    timer.stop();
    }
    addMouseListener(new MouseAdapter(){
    public void mousePressed(MouseEvent evt){
    if (evt.getButton()==MouseEvent.BUTTON1)
    {
    timer.start();
    }
    }
    });
    }
    }
    }

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: MouseAdapter Class?

    Hello and welcome! Please use [code][/code] tags when posting code so we can easily read it!

    Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2011, 11:55 AM
  2. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  3. Replies: 5
    Last Post: 01-26-2011, 06:38 PM
  4. Query on using MouseAdapter
    By halluc1nati0n in forum Java Applets
    Replies: 8
    Last Post: 07-02-2009, 08:04 AM
  5. Replies: 7
    Last Post: 05-06-2009, 12: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
  •