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. |