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

Edited 4 Years Ago by schoolbus11: Explanation was missing

  1. Put all the strings with the fortunes ("yes" etc) into an array. Use a random number as an index into the array to pick one of the strings.
  2. Create a boolean tha tells you whether you are currently displaying the 8 or a fortune. Test that in the mouseClicked to decide whether to display a fortue or an 8 next (and update it accordingly).

Thanks for your reply JamesCherrill,
I tried to put the fortune strings into an array. ( I haven't learn how to do array yet)

    public void getFortune()
    {
        fortunes.setFont(new Font("Ariel", Font.BOLD, 15));
        fortunes.setColor(Color.WHITE);

        String [] fortune = new String[6];
        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(0,6)]);
        fortunes.setLocation((BALL_RADIUS*2-fortunes.getWidth())/2, BALL_RADIUS);
        add(fortunes);



    }

This is what I got so far but I don't think fortunes.setLabel(fortune[rgen.nextInt(0,6)]); this part is working because it's knowing it as an int? and you said create a boolean in the mouseClicked method? please provide more information for me little more specifically since I don't understand what your talking about :( Sorry for being stupid but I'll be waiting again!

YOur array is OK, it's just that when you create an array of size 6 it has 6 elements, numbered 0-5. So if you want to use elements 0-6 the array size must be 7.

nextInt takes one parameter, not two, which is how many values to pick randomly from. Just like the array size, if you call nextInt(6) you get a random int 0-5. Java knows that nextInt returns an int, so its perfectly OK to use as an array index.

I have to go out now, so no time to explain the booelan further - hopefully someone else can step in.
goos luck
J

Just got a moment:
This pseudo code turns a light on or off everyt time you click (that's the same logic as you need)...

boolean lightIsOn = false;

mouseClicked() {
   if (lightIsOn) {
      turn light off
      lightIsOn = false
   } else {
      turn light on
      lightIsOn = true
   } 
}

Thanks for your help so far! I got the values to print out randomely inside of the ball.
However, I still can't understand how the boolean works inside of the mouseclicked method.

    public void mouseClicked(MouseEvent e)
    {
        boolean startAtEight = false;

        if(startAtEight)
        {
            fortunes.setLabel(fortune[0]);
            startAtEight = false;
        }
        else
        {
            startAtEight = true;
        }


//      obj = getElementAt(getX(), getY());
//      
//      while(obj != null)
//      {
//          println(fortunes);
//      }
    }

This is where I'm at trying to figure out how to work it out. but
if(fortunes.equals(fortune[0])) part gives me error.
please anyone who can help provide me with new information!
Thank you so much!

PS: Then first of all, I have to make it so that It start at 8 from the beginning also. How do I do that..?

Edited 4 Years Ago by schoolbus11

Thanks for your tips James but I'm still not quite at the end of this assignment.

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;
    private GLabel fortunes = new GLabel("");
    public String fortune[];
    public boolean startAtEight = false;



    RandomGenerator rgen = new RandomGenerator();



    public void run()
    {
        addMouseListeners();
        createBall();
        getFortune();


    }

    public void mouseClicked(MouseEvent e)
    {
        while(!startAtEight)
        {
            if()
            {
                fortunes.setLabel(fortune[rgen.nextInt(7)]);
                startAtEight = false;
                fortunes.setLocation((BALL_RADIUS*2-fortunes.getWidth())/2, BALL_RADIUS);

            }
            else
            {
                fortunes.setLabel(fortune[0]);
                startAtEight = true;
                fortunes.setLocation((BALL_RADIUS*2-fortunes.getWidth())/2, BALL_RADIUS);
            }
        }
        //fortunes.setLabel(fortune[rgen.nextInt(7)]);
        add(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);



    }
}

These are my codes and I'm not sure how to set up my loops inside of the mouseClicked method and I just can't seem to understand quite well.. Please help me out I have stayed all night yesterday just because of this I'm so mad how I can't do this because I know it's probably 1 minute thing for people who actually know java. I want to be one of them.. please anyone help me

I finished the assignment now perfectly :)
Thanks for your help James I appreciate your help.

This article has been dead for over six months. Start a new discussion instead.