469,353 Members | 2,167 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,353 developers. It's quick & easy.

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.
Jan 29 '08 #1
14 6960
r035198x
13,262 8TB
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?
Jan 29 '08 #2
welll i am working in Jbuilder currently, and i am trying to set this up under the mouseEntered
Jan 29 '08 #3
BigDaddyLH
1,216 Expert 1GB
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.
Jan 29 '08 #4
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?
Jan 30 '08 #5
r035198x
13,262 8TB
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.
Jan 30 '08 #6
this is the code i was talking about

public void mouseEntered(MouseEvent 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.
Jan 30 '08 #7
BigDaddyLH
1,216 Expert 1GB
this is the code i was talking about

public void mouseEntered(MouseEvent 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?
Jan 30 '08 #8
the random element, i don't know how to set up the random element, can you tell me how?
Jan 30 '08 #9
r035198x
13,262 8TB
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.Random class.
Jan 30 '08 #10
BigDaddyLH
1,216 Expert 1GB
the random element, i don't know how to set up the random element, can you tell me how?
Use java.util.Random: http://java.sun.com/javase/6/docs/ap...il/Random.html
Jan 30 '08 #11
r035198x
13,262 8TB
Use java.util.Random: http://java.sun.com/javase/6/docs/ap...il/Random.html
snap .
Jan 30 '08 #12
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?
Jan 30 '08 #13
BigDaddyLH
1,216 Expert 1GB
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.Random to make sure I understood that class. This just seems like common sense to me -- understand separate parts in isolation, first.
Jan 30 '08 #14
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:

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*; 
  2. import java.awt.event.*; 
  3. import javax.swing.*;
  4. import java.util.Random;
  5. import java.awt.event.MouseListener;
  6. import java.awt.event.MouseEvent;
  7.  
  8. public class TestButt extends JFrame implements MouseListener
  9. {
  10.   JPanel pane = new JPanel();
  11.   JButton pressme = new JButton("Press Me");
  12.  
  13. TestButt()        // the frame constructor
  14.   {
  15.     super("Genius Test"); setBounds(100,100,300,200);
  16.     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17.     Container con = this.getContentPane(); // inherit main frame
  18.     con.add(pane);
  19.     pane.setSize(500, 500);   
  20.         pressme.setMnemonic('P'); // associate hotkey to button
  21.         pane.add(pressme);
  22.     pressme.requestFocus();
  23.     pressme.addMouseListener(this); //tells listener to listen
  24.     setVisible(true); // make frame visible
  25.   }
  26.  
  27. public void mouseEntered(MouseEvent e)
  28.  
  29.     {
  30.     Random r = new Random();
  31.     pressme.setLocation(r.nextInt(150), r.nextInt(150));
  32.     }
  33.  
  34. public void mouseExited(MouseEvent e) {}
  35. public void mousePressed(MouseEvent e) {}
  36. public void mouseReleased(MouseEvent e) {}
  37. public void mouseClicked(MouseEvent e) {}
  38.  
  39.   public static void main(String args[]) {new TestButt();}
  40. }
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.
Aug 22 '13 #15

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Skip Hollowell | last post: by
11 posts views Thread by terti | last post: by
4 posts views Thread by Arif Çimen | last post: by
18 posts views Thread by Colin McGuire | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.