Google Answers Logo
View Question
 
Q: java applett problem ( Answered 4 out of 5 stars,   2 Comments )
Question  
Subject: java applett problem
Category: Computers > Programming
Asked by: purplepit-ga
List Price: $40.00
Posted: 05 May 2003 11:49 PDT
Expires: 04 Jun 2003 11:49 PDT
Question ID: 199724
Given the following code which has two buttons that are displayed on 
the applet, but only one is enabled at a time.(and producing the 
desired colours) Buttons are turned on or off using the setEnabled() 
method: What would I need to add to have three colours red,green,and 
blue?? 
 
******************************************************************************** 
 
import java.awt.*; 
import java.awt.event.*; 
import java.applet.*; 
 
public class But2 extends Applet implements ActionListener 
{ 
    Button butRed, butGreen; 
    Color red = Color.red; 
    Color green = Color.green; 
    Color current = red; 
 
 
    public void init() 
    { 
 butRed = new Button("Red"); 
 butRed.setEnabled(false); 
 butRed.addActionListener(this); 
 butGreen = new Button("Green"); 
 butGreen.addActionListener(this); 
 add(butRed); 
 add(butGreen); 
 current = red; 
    } 
     
    public void actionPerformed(ActionEvent e) 
    { 
 String butName = e.getActionCommand(); 
  
 if("Red".equals(butName)) 
     { 
  butRed.setEnabled(false); 
  butGreen.setEnabled(true); 
  current = red; 
     } 
 if("Green".equals(butName)) 
     { 
  butRed.setEnabled(true); 
  butGreen.setEnabled(false); 
  current = green; 
     } 
 repaint(); 
    } 
 
    public void paint(Graphics g) 
    { 
 g.setColor(current); 
 g.fillRect(100,50,200,100); 
    } 
} 
 
******************************************************************************* 
 
Thanks.
Answer  
Subject: Re: java applett problem
Answered By: answerguru-ga on 05 May 2003 12:11 PDT
Rated:4 out of 5 stars
 
Greetings purplepit-ga!

The answer to this is fairly straight forward, but I have commented
the areas that I have added/changed from your original code:

*************BEGIN CODE********************

import java.awt.*;  
import java.awt.event.*;  
import java.applet.*;  
  
public class But2 extends Applet implements ActionListener  
{  
    Button butRed, butGreen, butBlue;  // now defining a blue button
    Color red = Color.red;  
    Color green = Color.green;  
	Color blue = Color.blue;		// storing the blue Color object
    Color current = red;			// default is still the same
  
  
    public void init()  
    {  
		 butRed = new Button("Red");  
		 butRed.setEnabled(false);  
		 butRed.addActionListener(this);  
		 butGreen = new Button("Green");  
		 butGreen.addActionListener(this); 
		 butBlue = new Button("Blue");		// declaring blue button
		 butBlue.addActionListener(this);	// adding listener to blue button
		 add(butRed);  
		 add(butGreen); 
		 add(butBlue);						// adding to applet
		 current = red;  
    }  
      
    public void actionPerformed(ActionEvent e)  
    {  
		String butName = e.getActionCommand();  
   
		if("Red".equals(butName))  
		{  
			  butRed.setEnabled(false); 
			  butBlue.setEnabled(true);		// now need to control blue button too
			  butGreen.setEnabled(true);  
			  current = red;  
		}
		else if("Green".equals(butName))  
		{  
			  butRed.setEnabled(true);  
			  butBlue.setEnabled(true);		// now need to control blue button too
			  butGreen.setEnabled(false);  
			  current = green;  
		}
		else if("Blue".equals(butName))		// added a new condition for the
blue button
		{
			butBlue.setEnabled(false);		// disable blue button when color is
blue
			butRed.setEnabled(true);		// need to enable red 
			butGreen.setEnabled(true);		// and green
			current = blue;
		}
		repaint();  
    }  
  
    public void paint(Graphics g)  
    {  
 g.setColor(current);  
 g.fillRect(100,50,200,100);  
    }  
} 

*************END CODE********************

That's all there is to it! Hopefully the formatting is still
acceptable - you never can tell until it has been posted :)

If you have any questions or concerns please do post a clarification
and I'll be happy to help out.

Cheers!

answerguru-ga
purplepit-ga rated this answer:4 out of 5 stars
Thanks!!! Very helpful indeed!!!!

Comments  
Subject: Re: java applett problem
From: answerguru-ga on 06 May 2003 15:59 PDT
 
Hi again purplepit!

Just wanted to say thanks for the kind words - if you need any
programming done in the future feel free to direct the question to me
and I'd be happy to help out.

answerguru-ga
Subject: Re: java applett problem
From: purplepit-ga on 06 May 2003 16:48 PDT
 
Hi answerguru.ga

Thank you too, but how do I actually direct a question to you?????

Purplepit.ga

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy