BulbTest.javaimport java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
class bulb implements ActionListener
{
boolean on;
Color C;
int x, y;
int radius = 25;
String name;
public bulb()
{
C = Color.blue;
on = true;
x = 100;
y = 100;
name = "bulb";
}
public bulb(String name, int x, int y, Color C)
{
this.C = C;
this.x = x;
this.y = y;
this.name = name;
on = true;
}
public void draw(Graphics g)
{
if (on) g.setColor( C );
else g.setColor(Color.black);
g.fillOval(x-radius,y-radius,2*radius,2*radius);
}
public void change_state()
{
on = !on;
}
public String toString()
{
return "bulb " + name + " " + on;
}
public void actionPerformed(ActionEvent e)
{
on = !on;
System.out.println(this);
}
}
public class BulbTest extends JFrame implements ActionListener
{
bulb left, right;
JButton b1, b2;
Canvas upper;
JPanel lower;
public BulbTest()
{
super("Bulb Test");
b1 = new JButton("left");
b2 = new JButton("right");
left = new bulb("left",80,60,Color.yellow);
right = new bulb("right",220,60,Color.green);
b1.addActionListener(left);
b2.addActionListener(right);
b1.addActionListener(this);
b2.addActionListener(this);
upper = new Canvas();
upper.setPreferredSize(new Dimension(300,120));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setPreferredSize(new Dimension(300,200));
lower = new JPanel(new FlowLayout());
lower.add(b1);
lower.add(b2);
getContentPane().add(upper);
getContentPane().add(lower,BorderLayout.SOUTH);
pack();
}
class Canvas extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
left.draw(g);
right.draw(g);
}
}
public static void main(String[] args)
{
BulbTest app = new BulbTest();
app.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
upper.repaint();
}
}
Maintained by John Loomis, updated Mon Sep 30 20:46:26 2013