Friday, October 14, 2016

a program to demonstrate the Simple bean

Aim: write a program to demonstrate the Simple bean.

Program:

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();
                                    }
                        });
                        setSize(200,100);
                        rectangular=false;
                        change();
            }
            public boolean getRectangular()
            {
                        return rectangular;
            }
            public void setRectangular(boolean flag)
            {
                        this.rectangular=flag;
                        repaint();
            }
            public void change()
            {
                        color=randomColor();
                        repaint();
            }
            public 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 w=d.width;
                        int h=d.height;
                        g.setColor(color);
                        if(rectangular)
                        g.fillRect(0,0,w-1,h-1);
                        else
                        g.fillOval(0,0,w-1,h-1);
            }
}

Output:




Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home