Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-26-2006, 11:33 AM
|
#1
|
|
LQ Newbie
Registered: Apr 2006
Posts: 14
Rep:
|
I have a problem in java applet and I need Help ????
Hi friends
I don’t know how to color the circle when I press the button
here is my code
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class circle extends Applet implements ActionListener
{
Button b1=new Button("Bule");
Button b2=new Button("Yellow");
public void init ()
{
add(b1);
b1.addActionListener(this);
add(b2);
b2.addActionListener(this);
}
public void paint (Graphics g)
{
g.drawOval(30, 50, 100, 100);
g.drawOval(135, 50, 100, 100);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==b1)
{
repaint();
}
if (e.getSource()==b2)
{
repaint();
}
}
}
|
|
|
|
04-26-2006, 11:56 AM
|
#2
|
|
Member
Registered: Dec 2005
Location: Sydney, Australia
Distribution: Arch Linux
Posts: 208
Rep:
|
Looks like homework to me
check out the java docs on Graphics and Color.
http://java.sun.com/j2se/1.5.0/docs/api/index.html
"g.setColor(java.awt.Color)" in the paint method is what you are looking for
|
|
|
|
04-26-2006, 12:43 PM
|
#3
|
|
LQ Newbie
Registered: Apr 2006
Posts: 14
Original Poster
Rep:
|
Thanks deroB for your quick reply , I know how to use the g.setColor in
public void paint (Graphics g)
{ g.setColor(Color.black);
g.drawOval(30, 50, 100, 100);
g.drawOval(135, 50, 100, 100);
}
the problem is I don’t know how to use the g.setColor(Color.green) in
public void actionPerformed(ActionEvent e) which I can change the color of circle to
blue if when I press the button(blue) .
|
|
|
|
04-26-2006, 01:01 PM
|
#4
|
|
Guru
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339
Rep:
|
Here:
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;
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 ^_^.
Regards!
|
|
|
|
04-27-2006, 05:27 AM
|
#5
|
|
LQ Newbie
Registered: Apr 2006
Posts: 14
Original Poster
Rep:
|
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.
|
|
|
|
04-27-2006, 08:33 AM
|
#6
|
|
Guru
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339
Rep:
|
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();
}
}
}
|
|
|
|
04-27-2006, 11:37 AM
|
#7
|
|
LQ Newbie
Registered: Apr 2006
Posts: 14
Original Poster
Rep:
|
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.
Last edited by mema_UAE; 04-27-2006 at 01:37 PM.
|
|
|
|
04-27-2006, 05:18 PM
|
#8
|
|
LQ Newbie
Registered: Apr 2006
Posts: 14
Original Poster
Rep:
|
I Need Help ?????
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:13 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|