Hey guys i tried this project for almost 2 days now and i cannot seem to get it
ID: 3620017 • Letter: H
Question
Hey guys i tried this project for almost 2 days now and i cannot seem to get itCan some1 help:
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.
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 CODE I have so far:
package jumpingbutton;
/**
*
* @author Rolf
*/
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_CL… /* 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().getScreenSiz… /* 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();
}
}
Thank you guys. Ill proivde all my karama points if you can help me asap.
Thanks