Hello, I want someone to comment off the following coding so that I can produce appropriate javadocs. Thanks. Here's the coding:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.util.Random;
import javax.swing.*;
public class ShellGame extends JPanel implements ActionListener
{
final int ROWS = 10;
final int COLS = 10;
final int PAD = 20;
final int EMPTY = 0;
final int COIN = 1;
final int LINEAR = 0;
final int BINARY = 1;
int[] shells = new int[ROWS*COLS];
int coin = 0;
int lastCoin = coin;
int currentShell = -1;
int high;
int low;
int searchMode;
JLabel label;
Random seed = new Random();
Seeker seeker = new Seeker(this);
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
String ac = button.getActionCommand();
if(ac.equals("linear"))
searchMode = LINEAR;
if(ac.equals("binary"))
{
searchMode = BINARY;
high = shells.length;
low = -1;
}
if(!seeker.isRunning())
{
label.setText(" ");
coin = seed.nextInt(shells.length);
System.out.println("coin = " + coin);
shells[coin] = COIN;
shells[lastCoin] = EMPTY;
lastCoin = coin;
currentShell = -1;
seeker.start();
}
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
double w = getWidth();
double h = getHeight();
double xInc = (w - 2*PAD)/COLS;
double yInc = (h - 2*PAD)/ROWS;
double dia = Math.min(xInc, yInc)*3/4;
Color color;
for(int j = 0; j < shells.length; j++)
{
if(j < currentShell)
color = Color.green.darker();
else if(j == currentShell)
color = Color.red;
else
color = Color.blue;
g2.setPaint(color);
double x = PAD + (j%10) * xInc + xInc/2;
double y = PAD + (j/10) * yInc + yInc/2;
g2.fill(new Ellipse2D.Double(x-dia/2, y-dia/2, dia, dia));
}
}
public void checkNext()
{
if(searchMode == LINEAR)
currentShell++;
else // BINARY
{
currentShell = (high + low)/2;
if(currentShell > coin)
high = currentShell;
else
low = currentShell;
}
repaint();
if(shells[currentShell] == COIN)
{
seeker.stop();
Thread t = new Thread(pause);
t.setPriority(Thread.NORM_PRIORITY);
t.start();
}
}
private Runnable pause = new Runnable()
{
public void run()
{
try
{
Thread.sleep(500);
}
catch(InterruptedException ie)
{
System.err.println("pause.run interrupt: " + ie.getMessage());
}
label.setText("coin found - game over");
}
};
private JLabel getLabel()
{
label = new JLabel(" ", JLabel.CENTER);
Dimension d = label.getPreferredSize();
d.height = 25;
label.setPreferredSize(d);
return label;
}
private JPanel getUIPanel()
{
JButton linear = new JButton("linear");
JButton binary = new JButton("binary");
linear.setActionCommand("linear");
binary.setActionCommand("binary");
linear.addActionListener(this);
binary.addActionListener(this);
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder("search"));
panel.add(linear);
panel.add(binary);
return panel;
}
public static void main(String[] args)
{
ShellGame game = new ShellGame();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(game.getLabel(), "North");
f.getContentPane().add(game);
f.getContentPane().add(game.getUIPanel(), "South");
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
class Seeker implements Runnable
{
ShellGame shellGame;
Thread runner;
boolean keepSearching;
public Seeker(ShellGame sg)
{
shellGame = sg;
keepSearching = false;
}
public void start()
{
if(!keepSearching)
{
keepSearching = true;
runner = new Thread(this);
runner.setPriority(Thread.NORM_PRIORITY);
runner.start();
}
}
public void stop()
{
keepSearching = false;
runner = null;
}
public boolean isRunning()
{
return keepSearching;
}
public void run()
{
while(keepSearching)
{
try
{
Thread.sleep(500);
}
catch(InterruptedException ie)
{
System.err.println("Seeker.run interrupt: " + ie.getMessage());
keepSearching = false;
}
shellGame.checkNext();
}
}
}