Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Learning how to use ActionListener

Status
Not open for further replies.

LBryant777

IS-IT--Management
Mar 24, 2004
23
US
Here I am again:

This time, I am attempting to create a smiley face (done) with a button that will change the smile from a frown (my problem). I believe I'm almost there, but I'm missing something that is probably relatively simple...

My code:
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class SmileFace_me extends Applet implements ActionListener {

	Button moodChange = new Button("Change my mood...");
	private boolean Mood = true;

	public void init() {
		add(moodChange);
		moodChange.addActionListener(this);
	}

	public void paint(Graphics g) {
		g.setColor(Color.yellow);
		g.fillOval(130, 100, 150, 150);
		g.setColor(Color.black);
		g.fillOval(180, 130, 15, 25);
		g.fillOval(220, 130, 15, 25);
		g.drawArc(155, 160, 100, 60, 180, 180);
	}

	public void actionPerformed(ActionEvent e) {

			if (Mood){
				Mood = false;
				e.drawArc(155, 190, 100, 60, 180, -180);
			} 
			else {
				Mood = true;
				e.drawArc(155, 160, 100, 60, 180, 180);
			}
		}

	}

I know it is the code in the ActionPerformed() method that is my problem, but I don't know what to change. Can someone tell me what I am doing wrong? I appreciate all responses, provided you give comments also so that I can learn from my mistakes! Thanks in advance!
 
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class SmileFace_me extends Applet implements ActionListener {

Button moodChange = new Button("Change my mood...");
private int count = 0;
private boolean Mood = true;
private boolean clicked = false;

public void init() {
add(moodChange);
moodChange.addActionListener(this);

}

public void paint(Graphics g) {

g.setColor(Color.yellow);
g.fillOval(130, 100, 150, 150);
g.setColor(Color.black);
g.fillOval(180, 130, 15, 25);
g.fillOval(220, 130, 15, 25);
if (count==0)
g.drawArc(155, 160, 100, 60, 180, 180);
if (clicked)
{
if (Mood){
Mood = false;
clicked = false;
g.drawArc(155, 190, 100, 60, 180, -180);
}
else {
Mood = true;
clicked = false;
g.drawArc(155, 160, 100, 60, 180, 180);
}
}
}

public void actionPerformed(ActionEvent e) {
count++;
clicked = true;
repaint();
}

}
 
// my previous modification makes the smilng face switch when users resize the applet, your logic does not have this bug
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class SmileFace_me extends Applet implements ActionListener {

Button moodChange = new Button("Change my mood...");
private boolean Mood = true;

public void init() {
add(moodChange);
moodChange.addActionListener(this);

}

public void paint(Graphics g) {

g.setColor(Color.yellow);
g.fillOval(130, 100, 150, 150);
g.setColor(Color.black);
g.fillOval(180, 130, 15, 25);
g.fillOval(220, 130, 15, 25);

if (Mood)
g.drawArc(155, 190, 100, 60, 180, -180);
else
g.drawArc(155, 160, 100, 60, 180, 180);

}

public void actionPerformed(ActionEvent e) {
if (Mood)
Mood = false;
else
Mood = true;
repaint();
}

}
 
I understand now - thanks for the response.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top