0% found this document useful (0 votes)
13 views3 pages

Week 14

html codes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Week 14

html codes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

WEEK - 14

Objective: To learn the modelling of data using Java beans

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);
}

public void paint(Graphics g){

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

You might also like