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("");
public String fortune[];
RandomGenerator rgen = new RandomGenerator();
public void run()
{
addMouseListeners();
createBall();
getFortune();
}
public void mouseClicked(MouseEvent e)
{
boolean startAtEight = false;
if(startAtEight)
{
}
// 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);
String [] fortune = new String[7];
fortune[0] = "8";
fortune[1] = "Yes";
fortune[2] = "No";
fortune[3] = "Most Likely";
fortune[4] = "Maybe";
fortune[5] = "Make it work";
fortune[6] = "Not going to happen";
fortunes.setLabel(fortune[rgen.nextInt(7)]);
fortunes.setLocation((BALL_RADIUS*2-fortunes.getWidth())/2, BALL_RADIUS);
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 part that I'm stuck at right now is how to make the applet start at 8 in the beginning and it have to show fortune after a click then if i click another it will be back to 8, then another a fortune and so on.
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