Newbie MouseListener Problem

C

Cammy

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);

}
}
 
K

Knute Johnson

Cammy said:
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();
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top