I have a problem in java applet and I need Help ????
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Circle extends Applet implements ActionListener {
private Button blue = new Button("Blue");
private Button red = new Button("Red");
private Color color;
public void init () {
add(blue);
blue.addActionListener(this);
add(red);
red.addActionListener(this);
}
public void paint (Graphics g) {
g.setColor(color);
g.drawOval(30, 50, 100, 100);
g.drawOval(135, 50, 100, 100);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()== blue) {
color = Color.blue;
repaint();
}
if (e.getSource()== red) {
color = Color.red;
repaint();
}
}
}
Few tips: You might consider naming Java classes with uppercases (thus Circle, not circle). Lowercases are usually used for variables name. You may also want to use the <code></code> tags when posting here. It gets easier to read ^_^.
Thanks ..... Mega Man X ..... for the correction ......
The problem is when I click the blue button the two circles color will change to blue, I just want to change one circle color to blue when I click the button blue and the other circle color will be red if I click the button red.
No problems. I can't test the following code right now but I can tell you why it won't work . If you press the red button once, it will work. If you press the blue button, it will work. Now, if you press either button twice, both circles will be of the same color.
I will let you puzzle a litte with the code and try to find a way to fix it ^_^. It's possible, but the very best solution in this case would be to create a Circle class with a paint method I believe. In this paint method you would have the color and position to draw the circle. You will then make a call for the Circle's paint method inside your paing (Graphics g) method. Anyway, good luck
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Circle extends Applet implements ActionListener {
private Button blue = new Button("Blue");
private Button red = new Button("Red");
private Color color;
private Color old_color;
public void init () {
add(blue);
blue.addActionListener(this);
add(red);
red.addActionListener(this);
}
public void paint (Graphics g) {
g.setColor(color);
g.drawOval(30, 50, 100, 100);
g.setColor(old_color);
g.drawOval(135, 50, 100, 100);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()== blue) {
old_color = color;
color = Color.blue;
repaint();
}
if (e.getSource()== red) {
old_color = color;
color = Color.red;
repaint();
}
}
}
Thanks ..... Mega Man X again ..... for your help......
The code is working know properly but it just has a simple problem which I try to
fix it but I couldn’t. In the beginning when I click the blue button the 2 circle color change to blue (that is the wrong) because I want just one circle color change to blue and the other color have to be black till I click the button red . Its right after when I click button red the circle 2 colors change to red and the circle 1 color still blue.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.