Button location randomization

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • helpme09
    New Member
    • Jan 2008
    • 6

    Button location randomization

    Hello I am new to java and doing simple programs such as information input forms, and i was wondering what the code would be to setLocation a button to move randomly through out the form.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by helpme09
    Hello I am new to java and doing simple programs such as information input forms, and i was wondering what the code would be to setLocation a button to move randomly through out the form.
    You'll need to be more specific. e.g by "form" do you mean JFrame, JPanel or an HTML form?

    Comment

    • helpme09
      New Member
      • Jan 2008
      • 6

      #3
      welll i am working in Jbuilder currently, and i am trying to set this up under the mouseEntered

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Why do you want a button to move randomly? If you are new to Java, I'd recommend staying away from GUI programs until you get some programming chops.

        Comment

        • helpme09
          New Member
          • Jan 2008
          • 6

          #5
          i am currently a student in a JAVA programming class, i don't have a particular reason for wanting a button to move randomly across my program other than to be of an annoyance, is it possible to set up the code for the button to move randomly under the mouseEntered code?

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by helpme09
            i am currently a student in a JAVA programming class, i don't have a particular reason for wanting a button to move randomly across my program other than to be of an annoyance, is it possible to set up the code for the button to move randomly under the mouseEntered code?
            I'm not sure what you mean by the "under the mouseEntered code" part but you can move buttons around a bit depending on where the button is. A more helpful exercise is to draw a button(or image) on a Canvas and do some animation to move it around randomly.

            Comment

            • helpme09
              New Member
              • Jan 2008
              • 6

              #7
              this is the code i was talking about

              public void mouseEntered(Mo useEvent e)

              is there any code that can go in this area that when the mouse enters the area of a button that the button moves to a random location.

              Comment

              • BigDaddyLH
                Recognized Expert Top Contributor
                • Dec 2007
                • 1216

                #8
                Originally posted by helpme09
                this is the code i was talking about

                public void mouseEntered(Mo useEvent e)

                is there any code that can go in this area that when the mouse enters the area of a button that the button moves to a random location.
                What part is the problem? The random element or the moving?

                Comment

                • helpme09
                  New Member
                  • Jan 2008
                  • 6

                  #9
                  the random element, i don't know how to set up the random element, can you tell me how?

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by helpme09
                    the random element, i don't know how to set up the random element, can you tell me how?
                    Have a read at the java.util.Rando m class.

                    Comment

                    • BigDaddyLH
                      Recognized Expert Top Contributor
                      • Dec 2007
                      • 1216

                      #11
                      Originally posted by helpme09
                      the random element, i don't know how to set up the random element, can you tell me how?
                      Use java.util.Rando m: http://java.sun.com/javase/6/docs/ap...il/Random.html

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by BigDaddyLH
                        snap .

                        Comment

                        • helpme09
                          New Member
                          • Jan 2008
                          • 6

                          #13
                          Thank you for the link, I looked through a lot of it but since i am new to JAVA i am not sure which parts are important to what i am trying to do, can anyone tell me which parts would be important to wanting a button to move randomly?

                          Comment

                          • BigDaddyLH
                            Recognized Expert Top Contributor
                            • Dec 2007
                            • 1216

                            #14
                            Originally posted by helpme09
                            Thank you for the link, I looked through a lot of it but since i am new to JAVA i am not sure which parts are important to what i am trying to do, can anyone tell me which parts would be important to wanting a button to move randomly?
                            Well, after carefully studying the API page for Random, would set the button task aside for a moment and write a very trivial program that exercised java.util.Rando m to make sure I understood that class. This just seems like common sense to me -- understand separate parts in isolation, first.

                            Comment

                            • Buggritt
                              New Member
                              • Aug 2013
                              • 1

                              #15
                              BigDaddy has the right idea here: break the problem down. I too was trying to solve this problem and this is sadly the first link in google but it provides no answers. Here's what you need to look up in order to understand it: Events, Event Handler, Random number generation, Button creation, location setting, MouseListener.

                              For everyone else who googles the random jumping button code I present it here:

                              Code:
                              import java.awt.*; 
                              import java.awt.event.*; 
                              import javax.swing.*;
                              import java.util.Random;
                              import java.awt.event.MouseListener;
                              import java.awt.event.MouseEvent;
                              
                              public class TestButt extends JFrame implements MouseListener
                              {
                                JPanel pane = new JPanel();
                                JButton pressme = new JButton("Press Me");
                               
                              TestButt()        // the frame constructor
                                {
                                  super("Genius Test"); setBounds(100,100,300,200);
                                  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                  Container con = this.getContentPane(); // inherit main frame
                                  con.add(pane);
                              	pane.setSize(500, 500);   
                                  	pressme.setMnemonic('P'); // associate hotkey to button
                                  	pane.add(pressme);
                              	pressme.requestFocus();
                              	pressme.addMouseListener(this); //tells listener to listen
                                  setVisible(true); // make frame visible
                                }
                              
                              public void mouseEntered(MouseEvent e)
                              
                                  {
                              	Random r = new Random();
                              	pressme.setLocation(r.nextInt(150), r.nextInt(150));
                                  }
                                
                              public void mouseExited(MouseEvent e) {}
                              public void mousePressed(MouseEvent e) {}
                              public void mouseReleased(MouseEvent e) {}
                              public void mouseClicked(MouseEvent e) {}
                              
                                public static void main(String args[]) {new TestButt();}
                              }
                              I'm relatively new to programming and there's probably a more streamlined way of doing this but it's a start. If you're learning yourself don't just copy it, you need to understand it if you want to use it in your own programs.

                              Comment

                              Working...