473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 7882
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

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

Similar topics

6
by: Skip Hollowell | last post by:
I am working on a menu bar for a site, and am using buttons in the bar (because the customer wants to use accessKeys for each selection, apparently it is too much work to click on them with a...
11
by: terti | last post by:
Hi there, newbie here. appologies if this is OT. (DOM based) I have a button defined in the html as follows: <input name="btnPay" value="Continue with payment"...
4
by: Arif Çimen | last post by:
Hi to everybody, I have chnged a button text in design mode. But After compiling and executing the program the text of the button do not change to new value. Any Ideas? Thaks for helps.
18
by: Colin McGuire | last post by:
Hi - this was posted last weekend and unfortunately not resolved. The solutions that were posted almost worked but after another 5 days of working on the code everynight, I am not further ahead....
5
by: Wonder | last post by:
How can I create or use the msgobx to show a message without a default button. The user has explicity to click on the button, so the msgbox closes it. Thanks,
1
by: JHawk24821 | last post by:
I am building a simple card matching game where the user picks two buttons on a form out of a group of buttons in an attempt to find a match. I have already taken care of the randomization needed...
2
by: RobAMacAF | last post by:
I am making it that when they click the form it adds a button. Sometimes however if they click to close to another button then it makes them overlap. Is there any easy way to tell if the buttons over...
0
KalariaNitya
by: KalariaNitya | last post by:
Hello All, in windows application(vb.net) i have one button control i want to relocate that button on Second button's click event. means old location is:184, 111 i want new location :59, 375...
1
by: =?Utf-8?B?UmljaA==?= | last post by:
I placed a button on a form menustrip for the purpose of causing the horizontal scrollbar of my form to appear so that I can access controls outside of the form's current view (the controls are...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.