Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Please help a newbie with a easy java assignment!!

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help a newbie with a easy java assignment!!

    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


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Please help a newbie with a easy java assignment!!

    Quote Originally Posted by schoolbus11 View Post
    ...I will be waiting for any reply...
    Maybe something like this:

    Create a boolean instance variable named, say Showing8.

    Applet init() function:
    BEGIN
     
        Perform other necessary initialization tasks. (Add mouse listener, etc.)
     
        Set Showing8 to true.
     
        Select the 8-ball label.
     
        Do whatever you have to do to draw/paint the screen.
     
    END init() function


    And

    Mouse click event handler:
    BEGIN
        IF Showing8 IS true 
        THEN
           Select a random fortune label.
           SET Showing8 to false. // For next click
        ELSE
           Select the 8-ball label.
           SET Showing8 to true.  // For next click
        END IF
     
        Do whatever you have to do to draw/paint the screen
     
    END of Mouse click event handler.


    Cheers!

    Z
    Last edited by Zaphod_b; July 24th, 2012 at 09:28 AM.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Location
    Almere, Nederlands, Earth, Sol, Milkyway
    Posts
    4
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help a newbie with a easy java assignment!!

    if this is easy than i m screwed

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Please help a newbie with a easy java assignment!!

    The stuff in my post is called pseudo-code. Have you never been exposed to pseudo-code?

    The things that I posted are not supposed to be Java, but they are supposed to suggest (in algorithmically-oriented Human language, not a Programming language) a way to approach the part of the task that you specifically asked about.

    Namely
    Quote Originally Posted by kloek8
    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.
    There are certainly other approaches, and maybe some of them might appear simpler to some folks, but that's just as about as simple as I can make it (coming off the tops of my heads in my usual stream-of-consciousness mode of expression).

    On the other hand...

    If you are new to Java and your very first assignment (in a class that has no prerequisites involving basic Java knowledge), is an applet graphics program using acm library functions, then maybe you are, indeed, screwed.

    After all, I'm not the one who called it "easy." I mean, as my old pal Junior Samples used to say, "If it wuz easy, anybody could do it!"


    Cheers!

    Z
    Last edited by Zaphod_b; July 24th, 2012 at 12:04 PM.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Please help a newbie with a easy java assignment!!

    Quote Originally Posted by kloek8 View Post
    if this is easy than i m screwed
    Love your sense of humor and that comment made my day. ty

  6. #6
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Re: Please help a newbie with a easy java assignment!!

    it is solved now thanks!
    Last edited by schoolbus11; July 26th, 2012 at 02:18 AM.

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Please help a newbie with a easy java assignment!!

    Please surround your code with [ highlight=java] and [ /highlight] tags. (without the space following each leading bracket)

    Going by the desired operation in your first post, I would say to drop the while loop from your mouseClicked method completely. Then using your boolean in your if, test to see if you should be showing your 8 or your message, and respond accordingly in either the if block or the else block.
    myFunkyMethodForClicks(ignoredClickSpecs ics) {
        if(messageIsShowing) {
            display 8;
        }
        else {
            display random message;
        }
        toggleBoolean;
    }//end myFunkyMethod

Similar Threads

  1. Java (Easy)
    By ErrorBuster in forum The Cafe
    Replies: 4
    Last Post: May 1st, 2012, 05:17 PM
  2. Easy Tic Tac Toe game and Applet Assignment
    By Jmastersam in forum Java Theory & Questions
    Replies: 2
    Last Post: April 28th, 2012, 10:06 AM
  3. this is easy for those who know java!!
    By speaker in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 28th, 2012, 10:04 AM
  4. Easy Java Problem
    By AATroop in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 12th, 2011, 02:05 PM
  5. Quick easy Java question.
    By DHG in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 25th, 2011, 02:59 PM