Results 1 to 3 of 3

Thread: Moving Button timer

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2007
    Location
    FAT32
    Posts
    61

    Moving Button timer

    Hey guys, been at this project for a week trying to get a Jbutton to move on a timer, and changing color when you click on it.

    I have been unsuccessful, after doing some tedious research.

    if anyone can give any hints, or assistance, will be much appreciated

    thanks,
    krit

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Moving Button timer

    Try this code:
    Code:
    import java.awt.Color;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.Random;
    import java.util.Timer;
    import java.util.TimerTask;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class MyFrame extends JFrame {
    
        private JButton myButton;
    
        public MyFrame() {
            /*
             * Initialize frame
             */
            super("Test");
            this.setSize(800, 600);
            this.setLayout(null);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            /*
             * Initialize button
             */
            myButton = new JButton("Click");
            myButton.setLocation(10, 10);
            myButton.setSize(100, 20);
    
            /*
             * Schedule a job at a fixed rate
             */
            Timer t = new Timer();
            final Random r = new Random();
            t.scheduleAtFixedRate(new TimerTask() {
    
                // Change button location randomly
                public void run() {
                    myButton.setLocation(r.nextInt(getWidth()), r.nextInt(getHeight()));
                }
            }, 0, 1000);
    
            myButton.addMouseListener(new MouseListener() {
    
                // On button click
                // Change background color
                public void mousePressed(MouseEvent e) {
                    myButton.setBackground(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
                }
    
                public void mouseClicked(MouseEvent e) {
                }
    
                public void mouseReleased(MouseEvent e) {
                }
    
                public void mouseEntered(MouseEvent e) {
                }
    
                public void mouseExited(MouseEvent e) {
                }
            });
    
            this.add(myButton);
        }
    
        public static void main(String[] args) {
            MyFrame frame = new MyFrame();
            frame.setVisible(true);
        }
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2007
    Location
    FAT32
    Posts
    61

    Re: Moving Button timer

    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 class
    Code:
    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
    }

Posting Permissions

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



Click Here to Expand Forum to Full Width