Task 6: Visual Beans
Create a simple visual bean with an area filled with a color.
The shape of the area depends on the property shape. If it is set to true then the shape of the area is
Square and it is Circle, if it is false.
The color of the area should be changed dynamically for every mouse click. The color should also be
changed if we change the color in the “property window “.
[Link]
import [Link].*;
import [Link].*;
import [Link]; // Crucial for JavaBeans
public class Colors extends Canvas implements Serializable
{
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)
{
[Link] = flag;
repaint();
}
public void change()
{
color = randomColor();
repaint();
}
private Color randomColor()
{
int r = (int)(255*[Link]());
int g = (int)(255*[Link]());
int b = (int)(255*[Link]());
return new Color(r, g, b);
}
public void paint(Graphics g)
{
Dimension d = getSize();
int h = [Link];
int w = [Link];
[Link](color);
if(rectangular)
{
[Link](0, 0, w-1, h-1);
}
else
{
[Link](0, 0, w-1, h-1);
}
}
}
[Link]
Name: [Link]
Java-Bean: True
Compilation steps:
[Link] [Link] file using notepad and place in one folder
[Link] [Link] file using notepad and place this also in same folder
[Link] java file using javac
4. .class file is created in your folder
5. In order to create jar file open cmd prompt, go to your folder
C:> jar -cfm [Link] [Link] Colors*.class
6. It will create jar file in your folder
[Link] this jar in BDK>beans>jars
[Link] come to BDK >beans >beanbox and click on [Link]
OUTPUT: