| Author |
Message |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 03/29/2006 16:52:55
|
Stirex
Newbie
Joined: 03/29/2006 16:50:31
Messages: 2
Offline
|
Need a detailed explanation on how this works, I know it does, but not sure how to explain it to some one else.
public void actionPerformed(ActionEvent event) {
int value = 0;
if (event.getSource() == tryAgain)
guess=0;
else if (event.getSource() == one)
value = 1;
else if (event.getSource() == two)
value = 2;
else if (event.getSource() == three)
value = 3;
if(value > 0) {
guess *= 10;
guess += value;
}
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 03/29/2006 19:10:51
|
stdunbar
Newbie
![[Avatar]](/images/avatar/a87ff679a2f3e71d9181a67b7542122c.png)
Joined: 06/22/2005 14:51:37
Messages: 849
Location: Superior, CO, USA
Offline
|
Somewhere up in the code there are a few objects (hard to tell what they are from this code) that are named tryAgain, one, two, and three. Something is triggering an event on these that is calling actionPerformed.
I would "guess" that guess is defined as an int somewhere else. It looks like if the event came from tryAgain then you reset the guess. Otherwise you do the math as shown.
Does that make sense?
|
Thanks for using the forums at hotjoe.com |
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 03/29/2006 19:54:00
|
destin
Newbie
Joined: 01/07/2006 19:13:04
Messages: 224
Offline
|
Looking at that code, I would assume that the four objects, tryAgain, one, two, and three, all share the same action listener, and this method is the actionPerformed(ActionEvent) method within that class. When an event is triggered, this method is called.
Then, you check four things. First, you check if the object that triggered the event (event.getSource()) is the same object as tryAgain. You perform the same check three more times with different objects. Note that == compares memory. The code works because when objects are passed into methods the value is not passed in, but a reference of the memory location.
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 03/29/2006 23:00:56
|
Stirex
Newbie
Joined: 03/29/2006 16:50:31
Messages: 2
Offline
|
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Safetest extends Applet
implements ActionListener {
private int guess = 0;
private int combination = 321;
private Button one, two, three, tryAgain;
public void init() {
one = new Button("1");
add (one);
one.addActionListener(this);
two = new Button("2");
add (two);
two.addActionListener(this);
three = new Button ("3");
add (three);
three.addActionListener(this);
tryAgain = new Button("Try Again");
add(tryAgain);
tryAgain.addActionListener(this);
}
public void paint(Graphics g) {
if (guess==combination)
g.drawString ("Unlocked", 50,50);
else
g.drawString ("Locked", 50,50);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == one)
guess = guess*10 + 1;
if (event.getSource() == two)
guess = guess*10 + 2;
if (event.getSource() == three)
guess = guess*10 + 3;
if (event.getSource() == tryAgain)
guess = 0;
repaint();
}
}
ok so thats the whole code (tad bit different gets rid of X) basically it gives you 3 buttons, and you try to guess the combination of the safe in 3 guesses.
Now the part that I dont totally understand, how does it know you put in the right combination 321, and how does it keep track of the guesses. This is an example given in a lesson plan, but the explanation is very vague. I understand the action listeners, the button initialzations, just not the math.
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 03/30/2006 10:29:41
|
stdunbar
Newbie
![[Avatar]](/images/avatar/a87ff679a2f3e71d9181a67b7542122c.png)
Joined: 06/22/2005 14:51:37
Messages: 849
Location: Superior, CO, USA
Offline
|
The math is kinda funky but if you walk through it it makes sense.
guess starts out at zero or is reset to zero when the tryAgain Button is pushed. If you press the three button guess is set to the value 3. Then, if you press the two button guess is set to 32. Lastly, if you press the one button guess is set to 321. Every time any of the buttons are pressed the repaint() method is called which calls paint().
Make sense?
|
Thanks for using the forums at hotjoe.com |
|
|
 |
|
|