Results 1 to 2 of 2
  1. #1
    acash229 is offline Member
    Join Date
    Sep 2010
    Rep Power

    Default Java Game, Really need help

    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:

    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 */

    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);

    public static void main(String[] args){
    new JumpingButton();



    I will return the favor to you. THank you

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power

Similar Threads

  1. help Java game
    By rawan in forum JCreator
    Replies: 1
    Last Post: 12-25-2009, 07:43 PM
  2. Java Game
    By MuslimCoder in forum New To Java
    Replies: 6
    Last Post: 11-12-2009, 11:38 AM
  3. Java 2d game
    By Mr.Beans in forum Java 2D
    Replies: 0
    Last Post: 08-05-2009, 10:27 PM
  4. java game
    By mayhewj7 in forum New To Java
    Replies: 1
    Last Post: 04-10-2009, 08:01 AM
  5. Help with my game in java
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 05:40 PM

Posting Permissions

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