patternjavaMinor
Swing Game EyeHandCoordination
Viewed 0 times
gameswingeyehandcoordination
Problem
Question copied from the book:
(Game: eye-hand coordination) Write a program that displays a circle
of radius 10 pixels filled with a random color at a random location on
a panel, as shown in Figure 16.28c. When you click the circle, it
disappears and a new randomcolor circle is displayed at another random
location. After twenty circles are clicked, display the time spent in
the panel, as shown in Figure 16.28d.
My solution:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class EyeHandCoordination extends JFrame {
private final int CIRCLE_RADIUS = 10;
private final int TOTAL_CIRCLES = 20;
private RandomCirclePanel panel = new RandomCirclePanel();
public EyeHandCoordination() {
add(panel);
}
public static void main(String[] args) {
EyeHandCoordination frame = new EyeHandCoordination();
frame.setTitle("EyeHandCoordination");
frame.setSize(300, 300);
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private class RandomCirclePanel extends JPanel {
private long startTime;
private long endTime;
private int circleX = 0;
private int circleY = 0;
private int currentCircle = 0;
public RandomCirclePanel() {
startTime = System.currentTimeMillis();
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
// Check if in circle
if (inCircle(e.getX(), e.getY()) && currentCircle = TOTAL_CIRCLES) {
g.setColor(Color.BLACK);
endTime = System.currentTimeMillis();
FontMetrics fm = g.getFontMetrics();
String s = "Time spent: " + ((endTime - startTime) / 1000.0) + " seconds";
g.drawString(s, width / 2 - fm.stringW
(Game: eye-hand coordination) Write a program that displays a circle
of radius 10 pixels filled with a random color at a random location on
a panel, as shown in Figure 16.28c. When you click the circle, it
disappears and a new randomcolor circle is displayed at another random
location. After twenty circles are clicked, display the time spent in
the panel, as shown in Figure 16.28d.
My solution:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class EyeHandCoordination extends JFrame {
private final int CIRCLE_RADIUS = 10;
private final int TOTAL_CIRCLES = 20;
private RandomCirclePanel panel = new RandomCirclePanel();
public EyeHandCoordination() {
add(panel);
}
public static void main(String[] args) {
EyeHandCoordination frame = new EyeHandCoordination();
frame.setTitle("EyeHandCoordination");
frame.setSize(300, 300);
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private class RandomCirclePanel extends JPanel {
private long startTime;
private long endTime;
private int circleX = 0;
private int circleY = 0;
private int currentCircle = 0;
public RandomCirclePanel() {
startTime = System.currentTimeMillis();
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
// Check if in circle
if (inCircle(e.getX(), e.getY()) && currentCircle = TOTAL_CIRCLES) {
g.setColor(Color.BLACK);
endTime = System.currentTimeMillis();
FontMetrics fm = g.getFontMetrics();
String s = "Time spent: " + ((endTime - startTime) / 1000.0) + " seconds";
g.drawString(s, width / 2 - fm.stringW
Solution
public boolean inCircle(int mouseX, int mouseY) {
if (distanceFromCenterOfCircle(mouseX, mouseY) <= CIRCLE_RADIUS) {
return true;
}
return false;
}Can be
public boolean inCircle(int mouseX, int mouseY) {
return distanceFromCenterOfCircle(mouseX, mouseY) <= CIRCLE_RADIUS;
}(int)(Math.random() (getWidth() - CIRCLE_RADIUS 2));g.setColor(new Color((int)(Math.random() 256), (int)(Math.random() 256), (int)(Math.random() * 256)));Looks like you could benefit from a function that takes an integer as maximum value... and then returns an integer between 0 and max minus 1.
Which does exist - you'd simply create a new instance of
Random and then call randomInstance.nextInt(maxValue).Lastly, I think you would be better off by renaming the
currentCircle variable to amountOfCirclesDisplayed because that's what it really is. currentCircle seems to point to the idea that somewhere, you have a list of circles, and right now, you're working on 1 specific circle. But that's not true, because you HAVE no list of circles and all that really matters is how many you've displayed so far.Code Snippets
public boolean inCircle(int mouseX, int mouseY) {
if (distanceFromCenterOfCircle(mouseX, mouseY) <= CIRCLE_RADIUS) {
return true;
}
return false;
}public boolean inCircle(int mouseX, int mouseY) {
return distanceFromCenterOfCircle(mouseX, mouseY) <= CIRCLE_RADIUS;
}Context
StackExchange Code Review Q#139181, answer score: 2
Revisions (0)
No revisions yet.