0% found this document useful (0 votes)
2 views4 pages

Bean Example

The document presents a Java class named 'Colors' that extends Canvas and implements Serializable. It allows users to change the color of a shape (either rectangular or oval) by clicking on the canvas, with the color being randomly generated. The class includes methods for setting the shape type, changing the color, and painting the shape on the canvas.

Uploaded by

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

Bean Example

The document presents a Java class named 'Colors' that extends Canvas and implements Serializable. It allows users to change the color of a shape (either rectangular or oval) by clicking on the canvas, with the color being randomly generated. The class includes methods for setting the shape type, changing the color, and painting the shape on the canvas.

Uploaded by

varshaponnus21
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Bean Example:

import [Link].*;
import [Link].*;
import [Link];
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();
}
});
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);
}
}

You might also like