Created
November 26, 2013 11:33
-
-
Save BornRiot/7656962 to your computer and use it in GitHub Desktop.
First portion of Java Bean
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package sample; | |
| //A simple Bean. | |
| import java.awt.*; | |
| import java.awt.event.*; | |
| import java.io.Serializable; | |
| public class Colors extends Canvas implements Serializable { | |
| transient private Color color; // not persistent | |
| private boolean rectangular; // is persistent | |
| 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) { | |
| 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); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment