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.
Button location randomization
Collapse
X
-
You'll need to be more specific. e.g by "form" do you mean JFrame, JPanel or an HTML form?Originally posted by helpme09Hello 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. -
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
-
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
-
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.Originally posted by helpme09i 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
-
What part is the problem? The random element or the moving?Originally posted by helpme09this 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
-
Use java.util.Rando m: http://java.sun.com/javase/6/docs/ap...il/Random.htmlOriginally posted by helpme09the random element, i don't know how to set up the random element, can you tell me how?Comment
-
snap .Originally posted by BigDaddyLHUse java.util.Rando m: http://java.sun.com/javase/6/docs/ap...il/Random.htmlComment
-
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.Originally posted by helpme09Thank 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
-
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:
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.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();} }Comment
Comment