Results 1 to 19 of 19
  1. #1
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default Please Help with this problem

    Hey Guys, This is the assignment that i Received:


    This assignment handles topics of events and GUI-elements. It's a little game. The task: create a button that randomly relocates to a different position on the screen each time it is hit. The player has to hit the button 15 times, the score is the time needed. You will be provided a framework that implements the button, the JFrame, the relocation, yet not the event handling.

    Your first task: add the necessary event handling and the game-framework (counter, score etc.). Measure and display the time using java's System.currentTimeMillis() method (see API). Please re-label the button each time with the number of remaining hits.

    Your second task: to make the game more mean, surround the button with 8 buttons (GridLayout 3x3) which the player is NOT allowed to hit. if the center button is sufficiently small, the game becomes really hard. If the user clicks one of the 8 bad buttons by accident, the following should happen:
    first time: the center button becomes smaller, but the game continues.
    second time: game over.

    Here's the class JumpingButton, your starting point. If you create an instance of this class, a button in the center of the screen will appear. Clicking it results in nothing, since no event handling is built in. The class also has a method 'relocate()'. If you call that class, the button will be displayed at a random position on the screen.


    This is the coded I have so far. I cannot go anymore further than this: ( I have worked on it. What the teacher gave was different)


    Java Code:
    package jumpingbutton;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class JumpingButton
    extends JFrame
    {
    JButton theButton; //our jumping button
    Dimension screenSize; //class Dimension contains int width, int height
    Dimension windowSize;
    Random rand = new Random(); //used for generating random numbers
    
    public JumpingButton() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE); /* Frame will exit when close/X is pressed */
    windowSize = new Dimension(100,100); /* Constructs new Dimension "windowSize" with values 100 for width and height */
    setSize(windowSize.width, windowSize.height); /* Sets window's size to be 100x100 */
    
    theButton = new JButton("Hit me!"); /* Create a JButton labeled with "Hit me!" */
    getContentPane().add(theButton); /* Gets the content pane of "this" class, which is a JFrame, and attach the button */
    
    pack();
    setVisible(true); /* Display "this" (JFrame) that we attached the button to */
    
    screenSize = Toolkit.getDefaultToolkit().getScreenSize(); /* Gets the screen dimensions, used for repositioning the button */
    int windowX = (screenSize.width - windowSize.width ) / 2;
    int windowY = (screenSize.height - windowSize.height) / 2; /* Get the coordinates needed to display button at middle of screen */
    setLocation(windowX, windowY); /* Sets the window's location to the middle position */
    
    
    }
    
    public void relocate(){
    /*If this function is called, it will reloatce the window to a new random position on the screen */
    int x = rand.nextInt(screenSize.width - windowSize.width);
    int y = rand.nextInt(screenSize.height - windowSize.height);
    setLocation(x,y);
    }
    
    public static void main(String[] args){
    new JumpingButton();
    
    }
    
    
    }
    Last edited by acash229; 09-11-2010 at 07:30 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please don't double post. This goes against the forum rules that you agreed to when you joined this forum, and is impolite as it forces folks to split their discussion. I've locked your other thread.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by acash229 View Post
    This is the coded I have so far. I cannot go anymore further than this:
    What part of this code has been written by you, and what part was given by your teacher? Based on my reading of your assignment, you haven't done anything yet, and so I strongly suggest you first give it a try as you have nothing to lose. Break the big problem down into little steps and try to solve each little step one at a time. For instance, the first step should be to have the JButton react to a press, and this is solved by adding an ActionListener to the JButton via its addActionListener method. You can find out more about this at the Swing tutorials here: How to Write an ActionListener

    Again, give it a try, and if it doesn't work, come on back and let's see your newest code and any full error messages.

    Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    Much luck and welcome to the forum.

  4. #4
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    I have implemented the program a little already but it is still very hard.
    I cant get the format that i need. Its too hard to get everything to setup in the accurate corners. Please help and thanks for your response.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by acash229 View Post
    I have implemented the program a little already but it is still very hard.
    I cant get the format that i need. Its too hard to get everything to setup in the accurate corners.
    What do you mean by "setup in the accurate corners"?

    Please help and thanks for your response.
    We'll be glad to help, but let's see what you've done so far, as we've so far only seen the code that was given you, and please ask as specific question as possible. Those usually get more specific and helpful answers.

    Luck.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    To reiterate:
    1) first create and add the ActionListener. Again the tutorial shows you how.

    2) What code are you currently having problems with? You really need to ask a specific question to get specific help.

    Edit: I see that you posted the same question over in Yahoo Questions, but that the question got deleted perhaps by the mods as you were offering to give the answerers extra points for helping you by posting unrelated and easily answerable questions.

    Luck.
    Last edited by Fubarable; 09-11-2010 at 07:57 AM.

  7. #7
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    Basically I need to have a north,south, and center piece for the JFRAME.

    North(the top) - I need to add the timer and the clicker count
    Center( the middle) - I need to have a square
    South (the bottom) - I need to have a start button

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you want to place items in different locations on a JFrame, you'll want to use one or more layout managers. For laying out components in a north/south, east/west and center orientation, then BorderLayout looks like a good bet. You can read up more on using layout managers here: Layout Manager tutorial

    and BorderLayout here: BorderLayout tutorial

  9. #9
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    Can you please help me get started with this code atleast.

    The problem is that i took this class late so i am trying to catch up with the class but i have to do this lab no matter what. So i have been reading from the begining and trying to do lab which is way beyond still.
    I am getting close, but if you can get me started with the code it will be deeply appreciated.
    thanks

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by acash229 View Post
    Can you please help me get started with this code atleast.
    Read up on ActionListeners (you'll need one to attach to your button) and the BorderLayout class (as Fubarable already wrote). Then come back here if you have trouble but don't ask us to do, or start, your work for you.

    Quote Originally Posted by acash229 View Post
    The problem is that i took this class late so i am trying to catch up
    Nah, lame excuse; other people can do better than that: their dog ate the assignment, relatives are lethally ill, the poster got kicked out of the house and his computer is still in there, there was a terrorist attack in their back yard, they were kidnapped just before their assignment was due, they are their own professor etc. etc.

    kind regards,

    Jos

  11. #11
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    This is what i got. I cant get the button to say how many clicks its doing.

    Please Help

    Java Code:
    package jumpingbutton;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class JumpingButton
    extends JFrame
    {
    JButton theButton; //our jumping button
    Dimension screenSize; //class Dimension contains int width, int height
    Dimension windowSize;
    Random rand = new Random(); //used for generating random numbers
    TextField text = new TextField(20);
    
    public JumpingButton() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE); /* Frame will exit when close/X is pressed */
    windowSize = new Dimension(350,100); /* Constructs new Dimension "windowSize" with values 100 for width and height */
    setSize(windowSize.width, windowSize.height); /* Sets window's size to be 100x100 */
    
    theButton = new JButton("Hit me!"); /* Create a JButton labeled with "Hit me!" */
    getContentPane().add(theButton); /* Gets the content pane of "this" class, which is a JFrame, and attach the button */
    
    pack();
    setVisible(true); /* Display "this" (JFrame) that we attached the button to */
    
    screenSize = Toolkit.getDefaultToolkit().getScreenSize(); /* Gets the screen dimensions, used for repositioning the button */
    int windowX = (screenSize.width - windowSize.width ) / 2;
    int windowY = (screenSize.height - windowSize.height) / 2; /* Get the coordinates needed to display button at middle of screen */
    setLocation(windowX, windowY); /* Sets the window's location to the middle position */
    
    thehandler action = new thehandler();
    theButton.addActionListener(action);
    
    }
    
    private class thehandler implements ActionListener{
    
            public void actionPerformed(ActionEvent event){
                numClicks++;
                text.setText("Button Clicked" + numClicks + "times");
    
    
    
    
    
            }
    
    
    
    }
    
    
    
    
    public void relocate(){
    /*If this function is called, it will reloatce the window to a new random position on the screen */
    int x = rand.nextInt(screenSize.width - windowSize.width);
    int y = rand.nextInt(screenSize.height - windowSize.height);
    setLocation(x,y);
    }
    
    public static void main(String[] args){
    new JumpingButton();
    
    }
    
    
    }

  12. #12
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    This is the error i get:

    run:
    Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
    symbol: variable numClicks
    location: class jumpingbutton.JumpingButton.thehandler
    at jumpingbutton.JumpingButton$thehandler.actionPerfo rmed(JumpingButton.java:41)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6267)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:603 2)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    BUILD SUCCESSFUL (total time: 4 seconds)

  13. #13
    s2sgateway is offline Member
    Join Date
    Sep 2010
    Posts
    17
    Rep Power
    0

    Default

    You have to import the specific package to make this work for ex import javax.swing.groupLayout

  14. #14
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    Its still not doing anything

  15. #15
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Java Code:
    private class thehandler implements ActionListener{
            public void actionPerformed(ActionEvent event){
                numClicks++;
                text.setText("Button Clicked" + numClicks + "times");
            }
    }
    Scope, scope, scope. numClicks is not defined here (nor does it appear anywhere else in the provided code).

  16. #16
    s2sgateway is offline Member
    Join Date
    Sep 2010
    Posts
    17
    Rep Power
    0

    Default

    working code you have to assign the variable to some values before it is used in program here numClicks is assigned
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class JumpingButton
    extends JFrame
    {
    int numClicks=0;
    JButton theButton; //our jumping button
    Dimension screenSize; //class Dimension contains int width, int height
    Dimension windowSize;
    Random rand = new Random(); //used for generating random numbers
    TextField text = new TextField(20);
    
    public JumpingButton() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE); /* Frame will exit when close/X is pressed */
    windowSize = new Dimension(350,100); /* Constructs new Dimension "windowSize" with values 100 for width and height */
    setSize(windowSize.width, windowSize.height); /* Sets window's size to be 100x100 */
    
    theButton = new JButton("Hit me!"); /* Create a JButton labeled with "Hit me!" */
    getContentPane().add(theButton); /* Gets the content pane of "this" class, which is a JFrame, and attach the button */
    
    pack();
    setVisible(true); /* Display "this" (JFrame) that we attached the button to */
    
    screenSize = Toolkit.getDefaultToolkit().getScreenSize(); /* Gets the screen dimensions, used for repositioning the button 
    
    */
    int windowX = (screenSize.width - windowSize.width ) / 2;
    int windowY = (screenSize.height - windowSize.height) / 2; /* Get the coordinates needed to display button at middle of 
    
    screen */
    setLocation(windowX, windowY); /* Sets the window's location to the middle position */
    
    thehandler action = new thehandler();
    theButton.addActionListener(action);
    
    }
    
    private class thehandler implements ActionListener{
    
            public void actionPerformed(ActionEvent event){
                numClicks++;
                text.setText("Button Clicked" + numClicks + "times");
    
    
    
    
    
            }
    
    
    
    }
    
    
    
    
    public void relocate(){
    /*If this function is called, it will reloatce the window to a new random position on the screen */
    int x = rand.nextInt(screenSize.width - windowSize.width);
    int y = rand.nextInt(screenSize.height - windowSize.height);
    setLocation(x,y);
    }
    
    public static void main(String[] args){
    new JumpingButton();
    
    }
    
    
    }
    Last edited by s2sgateway; 09-12-2010 at 06:30 AM.

  17. #17
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    s2sgateway, unless you are going to provide ample comments & a description about why and how your code works, do not just hand out code to prospective forum-goers. The idea behind a forum is collaboration; a place where people can get help about things they don't understand. Like a school. If we just give them working code, they learn nothing.

    Please either revise your post with comments & a description, or make a new post to correspond to it.
    Last edited by Zack; 09-12-2010 at 06:32 AM.

  18. #18
    s2sgateway is offline Member
    Join Date
    Sep 2010
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    s2sgateway, unless you are going to provide ample comments & a description about why and how your code works, do not just hand out code to prospective forum-goers. The idea behind a forum is collaboration; a place where people can get help about things they don't understand. Like a school. If we just give them working code, they learn nothing.

    Please either revise your post with comments & a description, or make a new post to correspond to it.
    Thank you for your advice man

  19. #19
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    THanks for the help, but the program still does not show how many clicks it is doing.

    Please foward to this post :
    Java Gave Question

    I posted my code that i have got to work.
    The problem is that i cant get the top heard to say timer and clicks
    I need to get the top to read how long the game takes and how many clicks he does.

    Thanks

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •