/*
 * JumpingButton.java
 *
 * Created on September 10, 2008, 1:51 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

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_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 relocate 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(); 

}

    
}
