• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Newbie in need of help  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!