Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This assignment handles topics of events and GUI-elements. It\'s a little game.

ID: 3628780 • Letter: T

Question


This assignment handles topics of events and GUI-elements. It's a little game. The task: create a window with 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 to finish this task. Surround the button with 8 other buttons (=>grid 3x3) which the user is not allowed to hit! That's it. short and simple.



HELP:

It will be helpful to follow these steps:

1. import packages: you will need three packages: javax.swing.*, java.awt.*, java.awt.event.*? (if you are using netbeans, it is interesting NOT to import the packages, because netbeans will do so for you, reminding you at the time they are needed!)

2. Create a class "JumpingButton", which extends JFrame.

3. Do all the necessary things a JFrame needs (setDefaultCloseOperation etc). The source of the previous lab will be helpful.

4. Set the layout (for example: getContentPane().setLayout(new BorderLayout());

5. Create a button, add it to your frame's content pane ("this.getContentPane().add(myButton)").

6. make the frame visible.

So far you only set up the GUI for the game, no functionality is provided. But, at least, there's a button on the screen! Let's add functionality:

7. prepare to register your program to the event-source, which is the button. You can only register to a button, when you guarantee that you are an ActionListener object. Currently, you are a JFrame object. To implement and guarantee the functionality of an Actionlistener, you have to implement the interface "ActionListener". So: change your classes declaration from "class JumpingButton extends JFrame" to "class JumpingButton extends JFrame implements ActionListener", which immediately makes netbeans complain that you didn't implement the method "actionPerformed" yet. Let netbeans do the work and implement the basics: click on the complaint, which implements the skeleton of the method "actionPerformed()". This is the method the button calls every time it is clicked! BUT: so far we didn't register. We only prepared to register.

8. register! now that you are an ActionListener, you may register yourself to the button: myButton.addActionListener(this)

9. The method actionPerformed" is the place to insert the code to let the button jump, see below.

10. Test if the button jumps.

11. Add the scoring logic: you need a counter, that increases every time your button is pressed. If it reaches 15, the time has to be measured.

12. Measuring time: you can get the current time in milliseconds using the method "System.currentTimeMillis()". It returns a long-integer. If you call this method at the very beginning and after 15 clicks, the difference in the results is the time in milliseconds. Divide by 1000 to get the seconds, print, exit, ready.



The following method relocates a JFrame on the screen to a random position (Toolkit and Dimension are in package java.awt):

public void relocate(JFrame f){
/*If this function is called, it will relocate the window to a new random position on the screen */
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension windowSize = f.getSize();
int x = rand.nextInt(screenSize.width - windowSize.width);
int y = rand.nextInt(screenSize.height - windowSize.height);
f.setLocation(x,y);
}






Thank You

Explanation / Answer

/* * JumpingButton.java * */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class JumpingButton extends JFrame implements ActionListener {
   
    private JButton myButton; //Button
    private int counter = 0; //count for 15
    //to calculate the time
    private long previousTime, currentTime, diff ;
   
    public JumpingButton() {
        super("Jumping Button"); //sets title
        //sets layout
        this.getContentPane().setLayout(new BorderLayout());
        //adding button
        myButton = new JButton("Jump");
        this.getContentPane().add(myButton);
        //registering button with action
        myButton.addActionListener(this);      
        //sets frame closing type
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack(); //fits to correct size
        this.setVisible(true); //sets visibility
    }
   
    //overridden action performed method
    public void actionPerformed(ActionEvent ae){
       
        // getting starting time
        if(counter == 0)
            previousTime = System.currentTimeMillis();           
        // getting time after 15 clicks
        if(counter == 14)
        {
            currentTime = System.currentTimeMillis();
            //calculating difference in seconds
            diff = currentTime - previousTime;
            diff /= 1000;
            System.out.println("Measured Time: "+diff+ " seconds");
            System.exit(0); //exit the program
        }               
        counter++;
        //relocates the frame
        relocate(this);
    }
   
    public void relocate(JFrame f){
        //random number generator       
        Random rand = new Random();
        /*If this function is called, it will relocate the window to a new random position on the screen */
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension windowSize = f.getSize();
        int x = rand.nextInt(screenSize.width - windowSize.width);
        int y = rand.nextInt(screenSize.height - windowSize.height);
        f.setLocation(x,y);
    }  
   
    //main method
    public static void main(String []args){
        JumpingButton jb = new JumpingButton();
    } //end of main
} //end of class