| | |
producing javadoc
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2005
Posts: 2
Reputation:
Solved Threads: 0
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();
}
}
}
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();
}
}
}
•
•
Join Date: Dec 2005
Posts: 2
Reputation:
Solved Threads: 0
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.
•
•
•
•
Originally Posted by Phaelax
It's your program, you document the methods.
http://java.sun.com/j2se/javadoc/writingdoccomments/
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.
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
![]() |
Similar Threads
- Any recommendations on good Flash books? (Graphics and Multimedia)
- Javadoc problems (Java)
Other Threads in the Java Forum
- Previous Thread: black jack game
- Next Thread: Swing Problem
Views: 1571 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application apps arguments array arrays automation binary bluetooth businessintelligence card chat class classes client code collision component crashcourse database draw eclipse ee error event exception file fractal free game gis givemetehcodez graphics gui helpwithhomework html ide image input integer integration j2me java javadoc javafx javaprojects jmf jni jpanel julia jvm linux list loop machine map method methods migrate mobile netbeans newbie nls number object oracle physics print problem program programming project radio recursion scanner screen security server service set size sms socket software sort sql string swing test textfield threads time transfer tree trolltech utility windows






