Thanks for your help computerJY though I managed to figure it out the 19th
Code:package javagame; import javax.swing.*; import java.awt.*; /** The MovingButton is designed to be a playful GUI component.<br> ** I control my own position on the screen.<br> ** When you click on me, my Moving Handler will notice that click ** and will ask me to move.<br> ** I then move to a new (random) position.<br> ** @author Gregg Tomlinson-Bell @version 1 **/ public class MovingButton extends JButton { private int xPosition; private int yPosition; private double aRandomNumber; /** Set the text to initially read "Click me." **/ public MovingButton () { super("Click me"); // Ask my Parent to set our text } /** Generate random numbers for my new x and y coordinates ** then I reposition myself using the setLocation method. **/ public void move () { aRandomNumber = Math.random() * (getParent() .getWidth() - getWidth() ); xPosition = (int)aRandomNumber; aRandomNumber = Math.random() * (getParent() .getHeight() - getHeight() ); yPosition = (int)aRandomNumber; setLocation (xPosition, yPosition); setText("Too Slow!!"); int red = (int)(Math.random() * 255); int green = (int)(Math.random() * 255); int blue = (int)(Math.random() * 255); setBackground(new Color ( red, green, blue ) ); System.out.println(red + ", " + green + ". " + blue ); System.out.println(xPosition + ", " + yPosition ); } } // end classCode:import java.awt.*; import java.awt.event.*; /** The Game is simply a GUI Panel that any GUI Window can use.<br> ** The Window will simply "host" the game.<br> ** It is up to the Window to get the game started and to control the ending.<br><br> ** ** The Game itself is also <u>just a dumb host</u>! ** It simply adds a playful button to itself, leaving it up to the button how the ** button interacts with the game player.<br> ** @author Gregg Tomlinson-Bell @version 1 **/ public class Game extends JPanel { /** thePlayfulButton is a MovingButton.<br> ** We do not know nor care how it operates, ** only that it plays with the user in some way.<br> ** All we know is that <b>we</b> must not control the layout - that is up to the Button. **/ private MovingButton thePlayfulButton = new MovingButton(); /** theController is a MovingButtonHandler, whose job is simply to wait until something happens, ** then it moves the button.<br> ** (In actual fact, it does actually move the button - it asks the button to move itself.<br> ** We need to link it to the button so that it can talk to the button to ask the button to move. **/ private MovingButtonHandler theController = new MovingButtonHandler(thePlayfulButton); /** The Game constructor<br> ** The Game is simply a GUI container for a playful Button to play in.<br> ** ** When I put myself together (construct myself), ** I initially put the button onto the screen, ** then link the button controller to the button. ** **/ public Game() { add(thePlayfulButton); thePlayfulButton.addActionListener( theController ); } private class MovingButtonHandler implements ActionListener { private MovingButton myButtton; // This is the link to a button which I shall move around. public MovingButtonHandler(MovingButton aButtonToMove) { myButtton = aButtonToMove; // Save the link we have been given (ready for later) } public void actionPerformed (ActionEvent anEvent) { myButtton.move(); } } // end class }




Reply With Quote