
A modified checkers program with class name Checkers using double buffering. Somehow the program must have two top squares and two bottom squares. There should be two checkers of different color. The two checkers always move in opposite directions. When a checker reaches the end of its path, it should start moving backwards in the opposite direction.
Somehow need to enhance the Checkers program above to respond to mouse clicks. The checkers must stop moving whenever the user clicks and holds down the mouse. Then start moving again whenever the mouse is released. Cannot figure this out at all, please help.

1)Checkers.java:
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.*;
public class Checkers extends java.applet.Applet implements Runnable, MouseListener
{
Thread thread;
int x;
int y;
Image image;
Graphics graphics;
public void init()
{
addMouseListener(this);
x = 5;
y = 100;
image = createImage(this.size().width, this.size().height);
graphics = image.getGraphics();
}
public void start()
{
if (thread == null);
{
thread = new Thread(this);
thread.start();
}
}
public void stop()
{
if(thread != null)
{
thread.stop();
thread = null;
}
}
public void run()
{
while (true)
{
while(x <= 105)
{
repaint();
try { Thread.sleep(100); }
catch (InterruptedException e) { }
y-=4;
x+=4;
}
while(x >= 5)
{
repaint();
try { Thread.sleep(100); }
catch (InterruptedException e) { }
x-=4;
y+=4;
}
}
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
graphics.setColor(Color.black);
graphics.fillRect(0,0,100,100);
graphics.setColor(Color.black);
graphics.fillRect(100,100,100,100);
graphics.setColor(Color.gray);
graphics.fillRect(100,0,100,100);
graphics.setColor(Color.gray);
graphics.fillRect(0,100,100,100);
graphics.setColor(Color.red);
graphics.fillOval(x,5,90,90);
graphics.setColor(Color.blue);
graphics.fillOval(y,100,90,90);
g.drawImage(image,0,0,this);
}
public void destroy()
{
graphics.dispose();
}
public void mousePressed (MouseEvent e)
{
this.stop();
}
public void mouseReleased (MouseEvent e)
{
this.start();
}
public void mouseClicked(MouseEvent arg0) {}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
}
2)applet.html:
<HTML> <BODY> <APPLET CODE ="Checkers.class" WIDTH="800" HEIGHT="500"></APPLET> </BODY> </HTML>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.