3dfx
New member
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class MyDraughts extends Applet implements MouseListener,
MouseMotionListener {
private Point counter1, counter2, counter3, mouse;
private int select;
public void init() {
this.addMouseMotionListener(this);
this.addMouseListener(this);
select = 0;
counter1 = new Point(20,60);
counter2 = new Point(40,60);
counter3 = new Point(60,60);
mouse = new Point();
}
public void paint (Graphics g) {
g.fillOval(counter1.x, counter1.y, 5,5);
g.fillOval(counter2.x, counter2.y, 5,5);
g.fillOval(counter3.x, counter3.y, 5,5);
g.drawString("Use the mouse to move the Draughts around", 10,100);
}
public void mouseDragged(MouseEvent e) {
mouse = e.getPoint();
// continuously change the coordinates of the selected counter
if (select == 1) counter1 = mouse;
if (select == 2) counter2 = mouse;
if (select == 3) counter3 = mouse;
repaint();
}
public void mouseMoved(MouseEvent e) {}
// required for the interface
public void mousePressed(MouseEvent e) {
//select a counter using the mouse
mouse = e.getPoint();
// this could be done a lot more neatly using an array
if (mouse.x > counter1.x - 5 && mouse.x < counter1.x + 5 &&
mouse.y > counter1.y - 5 && mouse.y < counter1.y + 5) select = 1;
if (mouse.x > counter2.x - 5 && mouse.x < counter2.x + 5 &&
mouse.y > counter2.y - 5 && mouse.y < counter2.y + 5) select = 2;
if (mouse.x > counter3.x - 5 && mouse.x < counter3.x + 5 &&
mouse.y > counter3.y - 5 && mouse.y < counter3.y + 5) select = 3;
}
// 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 MyDraughts()
{
setLayout(new GridLayout(8,8));
for (int i=0; i<64; i++)
{
Panel square = new Panel();
if ((i+i/8)%2 == 0)
square.setBackground(Color.black);
else
square.setBackground(Color.white);
add(square);
}
setVisible(true);
}
}
Post follows:
For some reason my draught pieces aren't showing but my chessboard is, can anyone help out?
thanks
Last edited by a moderator: