LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 04-26-2006, 12:33 PM   #1
mema_UAE
LQ Newbie
 
Registered: Apr 2006
Posts: 14

Rep: Reputation: 0
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();
}
}


}
 
Old 04-26-2006, 12:56 PM   #2
deroB
Member
 
Registered: Dec 2005
Location: Sydney, Australia
Distribution: Arch Linux
Posts: 208

Rep: Reputation: 30
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
 
Old 04-26-2006, 01:43 PM   #3
mema_UAE
LQ Newbie
 
Registered: Apr 2006
Posts: 14

Original Poster
Rep: Reputation: 0
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) .
 
Old 04-26-2006, 02:01 PM   #4
Mega Man X
Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 63
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!
 
Old 04-27-2006, 06:27 AM   #5
mema_UAE
LQ Newbie
 
Registered: Apr 2006
Posts: 14

Original Poster
Rep: Reputation: 0
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.
 
Old 04-27-2006, 09:33 AM   #6
Mega Man X
Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 63
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();
        }
    }
}
 
Old 04-27-2006, 12:37 PM   #7
mema_UAE
LQ Newbie
 
Registered: Apr 2006
Posts: 14

Original Poster
Rep: Reputation: 0
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 02:37 PM.
 
Old 04-27-2006, 06:18 PM   #8
mema_UAE
LQ Newbie
 
Registered: Apr 2006
Posts: 14

Original Poster
Rep: Reputation: 0
I Need Help ?????
 
  


Reply

Tags
applet


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java plugin installed correctly for Firefox but not able to view any java applet tvn Linux - Software 10 04-15-2010 03:13 AM
JAVA JRE installation to view java applet through browser dipenchaudhary Linux - Software 1 01-23-2006 10:20 AM
Java applet error: "Applet Failed" nro Programming 1 08-28-2004 06:52 PM
Java applet problem davholla Programming 13 08-04-2003 09:45 AM


All times are GMT -5. The time now is 02:52 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration