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);
}
}
);
}
}
> 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
> 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]
> }
> }
> 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.