Graphic User Interaction using
Class java.awt.Button
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class ColorCircle extends Applet implements ActionListener
{
public Button bRed, bGreen;
public Color myColor;
public void init( )
{
bRed = new Button("RED");
bRed.setBackground(Color.red);
bRed.setForeground(Color.white);
bGreen = new Button("GREEN");
bGreen.setBackground(Color.green);
bGreen.setForeground(Color.white);
add(bRed);
add(bGreen);
bRed.addActionListener(this);
bGreen.addActionListener(this);
myColor = Color.white;
}
public void paint(Graphics g)
{
g.setColor(myColor);
g.fillOval(50, 50, 50, 50);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("RED"))
myColor = Color.red;
else if (e.getActionCommand().equals("GREEN"))
myColor = Color.green;
repaint();
}
}
Class java.awt.TextField
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Squares extends Applet implements ActionListener
{
public Button bSquare;
public TextField input;
public Color myColor;
public int num, numSquared;
public void init( )
{
setBackground(Color.blue);
setForeground(Color.yellow);
bSquare = new Button("SQUARE");
bSquare.setBackground(Color.red);
bSquare.setForeground(Color.white);
input = new TextField(20);
input.setBackground(Color.yellow);
input.setForeground(Color.black);
input.setText("0");
myColor = Color.white;
num = 0;
numSquared = 0;
add(input);
add(bSquare);
bSquare.addActionListener(this);
}
public void paint(Graphics g)
{
g.setColor(myColor);
g.drawString("" + numSquared, 100, 100);
}
public void actionPerformed(ActionEvent e)
{
num = Integer.parseInt(input.getText() );
numSquared = num * num;
repaint();
}
}