Newbie MouseListener Problem

Discussion in 'Java' started by Cammy, Apr 9, 2004.

  1. Cammy

    Cammy Guest

    Just started taking a class in java, and finding it pretty interesting, but
    very difficult. I'm trying to write some code that will allow me to move a
    counter on a chessboard. Got the board and counter setup (not very well),
    but the counter won't move. Thanks in advance for any help.
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;

    public class Romeo extends Applet implements MouseListener,
    MouseMotionListener {


    private Point snail1, mouse;
    private int select;

    public void init() {

    this.addMouseMotionListener(this);
    this.addMouseListener(this);
    select = 0;
    snail1 = new Point (25,25);
    mouse = new Point();
    }


    public void paint(Graphics g)
    {
    drawRect(g);
    g.setColor (Color.red);
    g.fillOval (snail1.x, snail1.y, 30,30);

    }

    public void mouseDragged(MouseEvent e) {
    mouse = e.getPoint();
    // continuously change the coordinates of the selected counter
    if (select == 1) snail1 = mouse;
    repaint();
    }

    public void mousePressed(MouseEvent e) {
    //select a counter using the mouse
    mouse = e.getPoint();

    if (mouse.x > snail1.x - 5 && mouse.x < snail1.x + 5 &&
    mouse.y > snail1.y - 5 && mouse.y < snail1.y + 5) select = 1;
    }


    // required for the interface
    public void mouseClicked(MouseEvent event){}
    public void mouseReleased(MouseEvent event){}
    public void mouseEntered(MouseEvent event){}
    public void mouseExited(MouseEvent event){}
    public void mouseMoved(MouseEvent event){}


    public void drawRect(Graphics g) {

    g.setColor (Color.black);
    g.fillRect (0,0,50,50);
    g.drawRect (50,0,50,50);
    g.fillRect (100,0,50,50);
    g.drawRect (150,0,50,50);
    g.fillRect (200,0,50,50);
    g.drawRect (250,0,50,50);
    g.fillRect (300,0,50,50);
    g.drawRect (350,0,50,50);

    g.drawRect (0,50,50,50);
    g.fillRect (50,50,50,50);
    g.drawRect (100,50,50,50);
    g.fillRect (150,50,50,50);
    g.drawRect (200,50,50,50);
    g.fillRect (250,50,50,50);
    g.drawRect (300,50,50,50);
    g.fillRect (350,50,50,50);


    g.fillRect (0,100,50,50);
    g.drawRect (50,100,50,50);
    g.fillRect (100,100,50,50);
    g.drawRect (150,100,50,50);
    g.fillRect (200,100,50,50);
    g.drawRect (250,100,50,50);
    g.fillRect (300,100,50,50);
    g.drawRect (350,100,50,50);

    g.drawRect (0,150,50,50);
    g.fillRect (50,150,50,50);
    g.drawRect (100,150,50,50);
    g.fillRect (150,150,50,50);
    g.drawRect (200,150,50,50);
    g.fillRect (250,150,50,50);
    g.drawRect (300,150,50,50);
    g.fillRect (350,150,50,50);

    g.fillRect (0,200,50,50);
    g.drawRect(50,200,50,50);
    g.fillRect(100,200,50,50);
    g.drawRect (150,200,50,50);
    g.fillRect (200,200,50,50);
    g.drawRect (250,200,50,50);
    g.fillRect (300,200,50,50);
    g.drawRect (350,200,50,50);

    g.drawRect (0,250,50,50);
    g.fillRect (50,250,50,50);
    g.drawRect (100,250,50,50);
    g.fillRect (150,250,50,50);
    g.drawRect (200,250,50,50);
    g.fillRect (250,250,50,50);
    g.drawRect (300,250,50,50);
    g.fillRect (350,250,50,50);

    g.fillRect (0,300,50,50);
    g.drawRect (50,300,50,50);
    g.fillRect (100,300,50,50);
    g.drawRect (150,300,50,50);
    g.fillRect (200,300,50,50);
    g.drawRect (250,300,50,50);
    g.fillRect (300,300,50,50);
    g.drawRect (350,300,50,50);

    g.drawRect (0,350,50,50);
    g.fillRect (50,350,50,50);
    g.drawRect (100,350,50,50);
    g.fillRect (150,350,50,50);
    g.drawRect (200,350,50,50);
    g.fillRect (250,350,50,50);
    g.drawRect (300,350,50,50);
    g.fillRect (350,350,50,50);

    }
    }
     
    Cammy, Apr 9, 2004
    #1
    1. Advertising

  2. Andrew Thompson, Apr 9, 2004
    #2
    1. Advertising

  3. Cammy wrote:
    > Just started taking a class in java, and finding it pretty interesting, but
    > very difficult. I'm trying to write some code that will allow me to move a
    > counter on a chessboard. Got the board and counter setup (not very well),
    > but the counter won't move. Thanks in advance for any help.
    > import java.applet.*;
    > import java.awt.*;
    > import java.awt.event.*;
    >
    > public class Romeo extends Applet implements MouseListener,
    > MouseMotionListener {
    >
    >
    > private Point snail1, mouse;
    > private int select;
    >
    > public void init() {
    >
    > this.addMouseMotionListener(this);
    > this.addMouseListener(this);
    > select = 0;
    > snail1 = new Point (25,25);
    > mouse = new Point();
    > }
    >
    >
    > public void paint(Graphics g)
    > {
    > drawRect(g);
    > g.setColor (Color.red);
    > g.fillOval (snail1.x, snail1.y, 30,30);
    >
    > }
    >
    > public void mouseDragged(MouseEvent e) {
    > mouse = e.getPoint();
    > // continuously change the coordinates of the selected counter
    > if (select == 1) snail1 = mouse;
    > repaint();
    > }
    >
    > public void mousePressed(MouseEvent e) {
    > //select a counter using the mouse
    > mouse = e.getPoint();
    >
    > if (mouse.x > snail1.x - 5 && mouse.x < snail1.x + 5 &&
    > mouse.y > snail1.y - 5 && mouse.y < snail1.y + 5) select = 1;
    > }
    >
    >
    > // required for the interface
    > public void mouseClicked(MouseEvent event){}
    > public void mouseReleased(MouseEvent event){}
    > public void mouseEntered(MouseEvent event){}
    > public void mouseExited(MouseEvent event){}
    > public void mouseMoved(MouseEvent event){}
    >
    >
    > public void drawRect(Graphics g) {
    >
    > g.setColor (Color.black);
    > g.fillRect (0,0,50,50);
    > g.drawRect (50,0,50,50);
    > g.fillRect (100,0,50,50);
    > g.drawRect (150,0,50,50);
    > g.fillRect (200,0,50,50);
    > g.drawRect (250,0,50,50);
    > g.fillRect (300,0,50,50);
    > g.drawRect (350,0,50,50);
    >
    > g.drawRect (0,50,50,50);
    > g.fillRect (50,50,50,50);
    > g.drawRect (100,50,50,50);
    > g.fillRect (150,50,50,50);
    > g.drawRect (200,50,50,50);
    > g.fillRect (250,50,50,50);
    > g.drawRect (300,50,50,50);
    > g.fillRect (350,50,50,50);
    >
    >
    > g.fillRect (0,100,50,50);
    > g.drawRect (50,100,50,50);
    > g.fillRect (100,100,50,50);
    > g.drawRect (150,100,50,50);
    > g.fillRect (200,100,50,50);
    > g.drawRect (250,100,50,50);
    > g.fillRect (300,100,50,50);
    > g.drawRect (350,100,50,50);
    >
    > g.drawRect (0,150,50,50);
    > g.fillRect (50,150,50,50);
    > g.drawRect (100,150,50,50);
    > g.fillRect (150,150,50,50);
    > g.drawRect (200,150,50,50);
    > g.fillRect (250,150,50,50);
    > g.drawRect (300,150,50,50);
    > g.fillRect (350,150,50,50);
    >
    > g.fillRect (0,200,50,50);
    > g.drawRect(50,200,50,50);
    > g.fillRect(100,200,50,50);
    > g.drawRect (150,200,50,50);
    > g.fillRect (200,200,50,50);
    > g.drawRect (250,200,50,50);
    > g.fillRect (300,200,50,50);
    > g.drawRect (350,200,50,50);
    >
    > g.drawRect (0,250,50,50);
    > g.fillRect (50,250,50,50);
    > g.drawRect (100,250,50,50);
    > g.fillRect (150,250,50,50);
    > g.drawRect (200,250,50,50);
    > g.fillRect (250,250,50,50);
    > g.drawRect (300,250,50,50);
    > g.fillRect (350,250,50,50);
    >
    > g.fillRect (0,300,50,50);
    > g.drawRect (50,300,50,50);
    > g.fillRect (100,300,50,50);
    > g.drawRect (150,300,50,50);
    > g.fillRect (200,300,50,50);
    > g.drawRect (250,300,50,50);
    > g.fillRect (300,300,50,50);
    > g.drawRect (350,300,50,50);
    >
    > g.drawRect (0,350,50,50);
    > g.fillRect (50,350,50,50);
    > g.drawRect (100,350,50,50);
    > g.fillRect (150,350,50,50);
    > g.drawRect (200,350,50,50);
    > g.fillRect (250,350,50,50);
    > g.drawRect (300,350,50,50);
    > g.fillRect (350,350,50,50);
    >
    > }
    > }
    >


    Cammy:

    I don't really see a counter in your code so I'm not really sure what
    you are trying to do. Take a look at the code below. It implements a
    component called Box that has a MouseListener that will increment a
    counter in a TextField. If you need multiple spots to click on and
    increment the counter you can attach the same MouseListener to all of
    the Box components. Use the Adapters when they are available and you
    don't need all of the methods of the Listeners. You can create a map to
    detect where the mouse clicks are coming from but if your Applet gets
    resized it won't work. If you use the MouseListener on the component
    then no matter the components size the mouse event will occur when the
    mouse pointer is over the component. Hope that helps some.

    import java.awt.*;
    import java.awt.event.*;

    public class MM extends Frame {
    int count;
    TextField tf;

    public MM() {
    setLayout(new FlowLayout());
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent we) {
    System.exit(0);
    }
    });

    tf = new TextField(Integer.toString(count));
    add(tf);

    MouseListener ml = new MouseAdapter() {
    public void mouseClicked(MouseEvent me) {
    tf.setText(Integer.toString(++count));
    }
    };

    Box box = new Box();
    box.addMouseListener(ml);
    add(box);

    pack();
    setVisible(true);
    }

    public class Box extends Canvas {
    public Dimension getPreferredSize() {
    return new Dimension(30,30);
    }

    public void paint(Graphics g) {
    g.setColor(Color.GREEN);
    g.fillRect(0,0,getHeight(),getWidth());
    }
    }

    public static void main(String[] args) {
    new MM();
    }
    }

    --

    Knute Johnson
    email s/nospam/knute/
    Molon labe...
     
    Knute Johnson, Apr 10, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian J. Sayatovic
    Replies:
    1
    Views:
    5,091
    Kleopatra
    Jul 31, 2003
  2. Peter the Swede

    MouseListener and hiding components.

    Peter the Swede, Nov 19, 2003, in forum: Java
    Replies:
    0
    Views:
    362
    Peter the Swede
    Nov 19, 2003
  3. Minti
    Replies:
    2
    Views:
    470
    Ian Shef
    Feb 19, 2004
  4. Jenny

    Interface MouseListener

    Jenny, Aug 19, 2004, in forum: Java
    Replies:
    1
    Views:
    358
    Lee Weiner
    Aug 20, 2004
  5. Cammy

    Newbie MouseListener Problem

    Cammy, Apr 9, 2004, in forum: Javascript
    Replies:
    1
    Views:
    112
    Hywel
    Apr 9, 2004
Loading...

Share This Page