|
|
Web Configuration:
<applet code="DrawCanvas.class"
codebase="applet" width=200 height=200>
</applet>
Source Code:
/*
* DrawCanvas.java
*
* This example demonstrates how to build a custom button.
* The button displays a blinking colored circle rather than
* a text string. A thread is created to blink the button. The
* custom also overrides enabled and disabled method in
* order to pain different images depending on the state of
* this property. The custom button also fires an action event
* when clicked. This example creates three colored buttons to
* control the color of the main canvas.
*/
import java.awt.*;
import java.awt.event.*;
public class DrawCanvas extends java.applet.Applet implements
ActionListener{
String[] colorNames = {"red", "green", "blue"};
Color[] colorValues = {Color.red, Color.green, Color.blue};
MainButton[] button = new MainButton[colorNames.length];
Canvas cv = new Canvas();
//set the layout of the applet
public void init () {
Panel p = new Panel(new GridLayout(1,0));
for (int i=0; i<colorNames.length; i++){
// create the instance of the MainButton and add that
// to the panel
p.add(button[i] = new MainButton(colorNames[i],
colorValues[i]));
// clicking button will invoke action
button[i].addActionListener(this);
}
setLayout(new BorderLayout());
add(p, BorderLayout.SOUTH);
cv.setSize(150,150);
add(cv, BorderLayout.CENTER);
}
// when a MainButton is clicked, update color of main canvas
public void actionPerformed(ActionEvent e) {
for (int i=0; i<colorNames.length; i++){
//System.out.println (e.getActionCommand());
if (colorNames[i].equals(e.getActionCommand())){
button[i].setEnabled(false);
cv.setBackground(colorValues[i]);
cv.repaint();
} else {
button[i].setEnabled (true);
}
}
}
}
class MainButton extends Canvas implements Runnable{
boolean on;
boolean engaged;
String label;
Thread timerThread;
ActionListener actionListener;
MainButton(String label, Color color){
this.label = label;
(timerThread = new Thread(this)).start();
setSize(40,40);
setForeground(color);
setBackground(Color.gray);
// Button will be invoked by the mouse event
addMouseListener(new MouseListener());
}
public void setEnabled(boolean on){
super.setEnabled(on);
if(on){
if (timerThread == null){
(timerThread = new Thread(this)).start();
}
}else {
timerThread = null;
}
repaint();
}
public void addActionListener(ActionListener l){
actionListener = AWTEventMulticaster.add(actionListener, l);
}
public void paint(Graphics g){
update(g);
}
public void update(Graphics g){
FontMetrics fm = g.getFontMetrics();
int w = getSize().width;
int h = getSize().height;
int ovalSize = Math.min(w/3, h/3);
g.clearRect(0,0,w,h);
if (isEnabled()){
if (engaged || on){
g.fillOval((w-ovalSize)/2, (h-ovalSize)/2,
ovalSize, ovalSize);
} else {
g.drawOval((w-ovalSize) /2, (h-ovalSize)/2,
ovalSize, ovalSize);
}
} else {
g.setColor(Color.white);
g.drawOval((w-ovalSize)/2, (h-ovalSize)/2,
ovalSize, ovalSize);
}
g.setColor(getBackground());
g.draw3DRect(0,0,w-1,h-1, !engaged);
g.draw3DRect(0,0,w-2,h-2, !engaged);
}
//Mouse Event Listener
class MouseListener extends MouseAdapter {
public void mousePressed (MouseEvent e){
engaged = true;
repaint();
}
public void mouseReleased (MouseEvent e){
engaged = false;
repaint();
if (actionListener != null){
// create action event for listener to process
ActionEvent action = new ActionEvent (this,
ActionEvent.ACTION_PERFORMED,
label);
actionListener.actionPerformed(action);
}
}
}
public void run(){
while (timerThread == Thread.currentThread()){
on = !on;
repaint();
try { Thread.sleep(1000); } catch (Exception e) {};
}
}
}
|