Hi, I'm new with the java world..
I have a homework assignment for my java class due soon and I'm quite stuck in the middle of it.
I'm supposed to make an eight ball using GOval in the acm library and whenever I click on the black ball in the applet, the words (fortunes) need to be changed everytime. But another click after showing the fortune, it must come back and show me 8 again and another click will lead to another fortune and another will come back to 8.
import acm.program.GraphicsProgram;
import acm.graphics.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import acm.util.RandomGenerator;
public class EightBall extends GraphicsProgram
{
public static final int BALL_RADIUS = 100;
public static GObject obj;
private GLabel fortunes = new GLabel("");
RandomGenerator rgen = new RandomGenerator();
public void run()
{
addMouseListeners();
createBall();
getFortune();
}
public void mouseClicked(MouseEvent e)
{
obj = getElementAt(getX(), getY());
while(obj != null)
{
println(fortunes);
}
}
public void createBall()
{
GOval ball = new GOval(BALL_RADIUS*2,BALL_RADIUS*2);
ball.setColor(Color.BLACK);
ball.setFilled(true);
add(ball);
}
public void getFortune()
{
fortunes.setFont(new Font("Ariel", Font.BOLD, 15));
fortunes.setColor(Color.WHITE);
fortunes.setLocation((BALL_RADIUS*2-fortunes.getWidth())/2, BALL_RADIUS);
do
{
//randomely select the fortunes
fortunes.setLabel("8");
fortunes.setLabel("Yes");
fortunes.setLabel("No");
fortunes.setLabel("Most Likely");
fortunes.setLabel("Maybe");
fortunes.setLabel("Make it work");
fortunes.setLabel("Not going to happen");
}
add(fortunes);
}
}
This is what I got so far and I'm stuck in the middle of it..
This is the requirements:
Pick from a set of 6 fortunes choosing one randomly
Methods
In addition to the run method, you will make at least 3 other methods:
- One to create the eight ball
- One to pick a random fortune
- And a mouseClicked method that will change the fortune
The actual part that I'm stuck at is how to print out the fortunes to be random and how to make it change whenever I click on it then to come back and show number 8 again.
If you could help me get this through, I would be very appreciate it because I have no idea how to get going from here.. I will be waiting for any reply from now on.
Thank you so much