aspose file tools*
The moose likes Applets and the fly likes Newbie in need of help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Newbie in need of help" Watch "Newbie in need of help" New topic
Author

Newbie in need of help

Cameron Mitchell
Greenhorn

Joined: Apr 09, 2004
Posts: 2
I'm trying to write some code that will allow me to move a counter on a chessboard. Got the board and counter, 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);
}
}
Korol Bloom
Ranch Hand

Joined: Jan 13, 2004
Posts: 33
Hi Cameron,
Welcome to JavaRanch!
I tried your code and seems to work fine, just that the red circle is not centered to the point.
replace

by

and you should get what you want.
,korol
[ April 12, 2004: Message edited by: Korol Bloom ]
 
 
subject: Newbie in need of help