944,010 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1934
  • Java RSS
Dec 30th, 2005
0

producing javadoc

Expand Post »
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();
}
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ismedave is offline Offline
2 posts
since Dec 2005
Dec 30th, 2005
0

Re: producing javadoc

It's your program, you document the methods.

http://java.sun.com/j2se/javadoc/writingdoccomments/
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Jan 3rd, 2006
0

Re: producing javadoc

No, it's not my code. I was asked by my lecturer to comment on the code and I'm stuck. Just wanted to see how you would have done it so that I can make more of it and try to understand it.

Quote originally posted by Phaelax ...
It's your program, you document the methods.

http://java.sun.com/j2se/javadoc/writingdoccomments/
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ismedave is offline Offline
2 posts
since Dec 2005
Jan 4th, 2006
0

Re: producing javadoc

So it's your homework indeed and we're not here to do it for you.
There's ample documentation about how to write javadoc online and included with the JDK documentation, read it.
Or buy a book about the subject and read that.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: black jack game
Next Thread in Java Forum Timeline: Swing Problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC