Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Click me if you can!

Thread view: 
George Cherry - 05 Jan 2006 05:00 GMT
I saw a Java applet several years ago that invited
the user to click a button--which moved away
smartly as soon as the mouse cursor hovered over
the button. It was a hoot. I'd like to show the program
to some highschool kids I'm tutoring. Could one of
you folks give me a link to such an "application" or
applet, post its code, or give me a few hints on
how to write it. It's just the kind of thing that will amuse
my students.

Georte
RiCaRdO - 05 Jan 2006 05:49 GMT
No JDK on this computer... but the trick would be to add implement
java.awt.event.MouseListener
(http://java.sun.com/j2se/1.5.0/docs/api/java/awt/event/MouseMotion.html)
and add it to an awt or swing button

and have the method:
mouseEntered(MouseEvent e)
change the location of the button...

> I saw a Java applet several years ago that invited
> the user to click a button--which moved away
[quoted text clipped - 7 lines]
>
> Georte
RiCaRdO - 05 Jan 2006 05:52 GMT
Sorry,
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/event/MouseListener.html
Chris Smith - 05 Jan 2006 15:12 GMT
> No JDK on this computer... but the trick would be to add implement
> java.awt.event.MouseListener
[quoted text clipped - 4 lines]
> mouseEntered(MouseEvent e)
> change the location of the button...

Perhaps, but I think the game would be rather less fun if you make it
impossible.  If you move the button in mouseEntered (and assuming you
move it such that it's no longer under the mouse) there will be no
possible sequence of actions by which the user can win.

Better might be a javax.swing.Timer, along with a maximum rate of motion
per timer fire for the button... and the button should start moving
before the mouse is directly over it.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Tony Morris - 05 Jan 2006 06:22 GMT
> I saw a Java applet several years ago that invited
> the user to click a button--which moved away
[quoted text clipped - 7 lines]
>
> Georte

If you are describing the same thing I saw a while back, it was written in
Javascript.

Signature

Tony Morris
http://tmorris.net/

Chris Smith - 05 Jan 2006 15:12 GMT
> If you are describing the same thing I saw a while back, it was written in
> Javascript.

I've definitely seen this in a Java applet as well, but unfortunately, I
also don't remember where.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

George Cherry - 05 Jan 2006 17:17 GMT
Thanks for everyone's hints and suggestions. Here's
my first stab. It will teach "my kids" about events,
event listeners, and so on. I'll challenge them to make
it more interesting, perhaps incorporating Chris's
suggestion (which will teach a little about Timers
and time). Any other pedagogical suggestions?
(Programming the button to dart in a random direction
will also be on my agenda.)

George

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MovingButton extends JApplet {
   JButton movingButton  = new JButton("Click for $1,000,000.");
   int x = 0, y = 0;

   public void init() {
       Container container = getContentPane();
       container.setLayout( new FlowLayout() );
       container.add(movingButton);

       movingButton.addMouseListener(
           new MouseAdapter() {
               public void mouseEntered(MouseEvent event) {
                   x = x + 10;
                   y = y + 20;
                   movingButton.setLocation(x, y);

               }
           }
       );
   }
}
Chris Smith - 05 Jan 2006 17:27 GMT
> Thanks for everyone's hints and suggestions. Here's
> my first stab. It will teach "my kids" about events,
> event listeners, and so on. I'll challenge them to make
> it more interesting, perhaps incorporating Chris's
> suggestion (which will teach a little about Timers
> and time). Any other pedagogical suggestions?

This is one of the VERY few times that I'd advise against using a
LayoutManager.  Use of FlowLayout is just plain confusing, and it will
cause your button to be suddenly moved back to its starting position at
undefined times.

>     JButton movingButton  = new JButton("Click for $1,000,000.");

Considering this new information, perhaps Ricardo's idea -- which makes
the button impossible to click -- would be better for you, at least from
a financial standpoint.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

George Cherry - 05 Jan 2006 17:42 GMT
>> Thanks for everyone's hints and suggestions. Here's
>> my first stab. It will teach "my kids" about events,
[quoted text clipped - 7 lines]
> cause your button to be suddenly moved back to its starting position at
> undefined times.

Here's my code again:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MovingButton extends JApplet {
   JButton movingButton  = new JButton("Click for $1,000,000.");
   int x = 0, y = 0;

   public void init() {
       Container container = getContentPane();
       container.setLayout( new FlowLayout() );
       container.add(movingButton);

       movingButton.addMouseListener(
           new MouseAdapter() {
               public void mouseEntered(MouseEvent event) {
                   x = x + 10;
                   y = y + 20;
                   movingButton.setLocation(x, y);

               }
           }
       );
   }
}

If I comment out "container.setLayout( new FlowLayout() );"
the program no longer works. Can anyone explain this?

>>     JButton movingButton  = new JButton("Click for $1,000,000.");
>
> Considering this new information, perhaps Ricardo's idea -- which makes
> the button impossible to click -- would be better for you, at least from
> a financial standpoint.

Indeed!  : o )

George
ricky.clarkson@gmail.com - 05 Jan 2006 18:24 GMT
> If I comment out "container.setLayout( new FlowLayout() );"
> the program no longer works. Can anyone explain this?

The layout manager is not just responsible for the location, it's also
responsible for the size.  If you want null layout, then you can use
container.setLayout(null);  But don't forget to set the size of the
component, otherwise it will be 0x0.

Another option is AbsoluteLayout.  Netbeans has an implementation, and
here's one that I wrote a couple of years ago:
http://cime.net/~ricky/AbsoluteLayout.java

I don't know how well it would work to remove the component and then
add it again with new constraints, but it should be possible.

Yes, null layout is probably more appropriate for this case.
Oliver Wong - 09 Jan 2006 21:27 GMT
>> Thanks for everyone's hints and suggestions. Here's
>> my first stab. It will teach "my kids" about events,
[quoted text clipped - 13 lines]
> the button impossible to click -- would be better for you, at least from
> a financial standpoint.

Slight off topic, but...

   (1) I think the point of this "game" is for it to be impossible to win.
Idea is to tease the user rather than to challenge them.
   (2) Many implementations I've seen, you actually CAN click the button if
you're fast enough. Maybe it has something to do with threading.
   (3) In virtually all implementations I've seen, you can trivially use
tab to give focus to the button, then press space or enter on the keyboard
to activate it.
   (4) Perhaps not suitable for kids, but a very amusing variant I've seen
on this is a program which claimed to test whether or not your were gay. It
had a label "Are you gay?" with two buttons "yes" and "no", and the
mouseover event would change it whichever button you mouseover-ed into "yes"
(and the other one to "no"). The intent was for you to move your mouse to
"no", realize that it changed to "yes", but would be unable to send your
muscles the signal to cancel the clicking in time. To rub it in, the "yes"
button would flash several times, and Carmen Burana O Fortuna would play,
and videos would play of rocket ships exploding, building collapsings, etc.

   - Oliver
castillo.bryan@gmail.com - 05 Jan 2006 17:31 GMT
> Thanks for everyone's hints and suggestions. Here's
> my first stab. It will teach "my kids" about events,
> event listeners, and so on. I'll challenge them to make
> it more interesting, perhaps incorporating Chris's
> suggestion (which will teach a little about Timers
> and time). Any other pedagogical suggestions?

Perhaps its off topic, but I've always though an assignment on robocode
would be a fun introduction to java.

http://robocode.sourceforge.net/

> (Programming the button to dart in a random direction
> will also be on my agenda.)
[quoted text clipped - 26 lines]
>     }
> }
Vova Reznik - 05 Jan 2006 17:39 GMT
> import javax.swing.*;
> import java.awt.*;
[quoted text clipped - 5 lines]
>
>     public void init() {

// Add this
movingButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(MovingButton.this, "Clicked");

            }
        });

>         Container container = getContentPane();
>         container.setLayout( new FlowLayout() );
[quoted text clipped - 12 lines]
>     }
> }

I was able to click on "$1,000,000" button.
Are going to pay?

Also it is simple to move the button out of area.
George Cherry - 05 Jan 2006 18:07 GMT
>> import javax.swing.*;
>> import java.awt.*;
[quoted text clipped - 13 lines]
> }
> });

Whoa! You changed the Listener! I won't pay.

George

>>         Container container = getContentPane();
>>         container.setLayout( new FlowLayout() );
[quoted text clipped - 17 lines]
>
> Also it is simple to move the button out of area.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.