Week 14
Week 14
Aim: Create a simple visual bean with a area filled with a color. The shape of the area
dependson the property shape. If it is set to true then shape of the area is rectangle and it is
circle if it isfalse. The color of the area should be changed dynamically for every mouse
click.
Program:
Colors.java:
package sunw.demo.colors;
import java.awt.*;
import java.awt.event.*;
public class Colors extends Canvas{
transient private Color color;
private boolean rectangular;
public Colors(){
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent me){
change();
}
});
rectangular = false;
setSize(200,100);
change();
}
public boolean getRectangular(){
return rectangular;
}
public void setRectangular(boolean flag){
this.rectangular = flag;
repaint();
}
public void change(){
color = randomColor(); repaint();
}
private Color randomColor(){
int r = (int)(255*Math.random());
int g = (int)(255*Math.random());
int b = (int)(255*Math.random());
return new Color(r, g, b);
}
21331A1262 163
Dimension d = getSize();
int h = d.height;
int w = d.width;
g.setColor(color);
if(rectangular){
g.fillRect(0,0,w-1,h-1);
}
else{
g.fillOval(0,0,w-1,h-1);
}
}
}
Color.mft:
Manifest-Version: 1.0
Name: sunw/demo/colors/Colors.class
Java-Bean: True
Created-By: 1.6.0 (Sun Microsystems Inc.)
OUTPUT :
21331A1262 164
21331A1262 165