21-07-2011, 09:40 AM
Aim: to create a Java Bean to draw various graphical shapes and display it using or without using BDK
Algorithm:
1. Create a class Appletdemo which extends Applet superclass and which implements ActionListener interface
2. Create five buttons with the required captions and add them to the container
3. Register the buttons with the ActionListener interface
4. In the Actionperformed () method, with the help of the instance, perform the action respectively
5. In the paint() method, check which button is pressed and display the shapes accordingly
Program
Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
//<applet code="Appletdemo" height=500 width=500></applet>
public class Appletdemo extends Applet implements ActionListener
{
String s;
Button b1,b2;
public void init()
{
b1= new Button("Test");
b2=new Button("circle");
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
s=ae.getActionCommand();
repaint();
}
public void paint(Graphics g)
{
try
{
if(s.equals("Test"));
g.drawOval(122,53,62,75);
//g.setColor(212, 23,45);
if(s.equals("circle"));
g.drawRect(122,122,122,122);
}
catch(NullPointerException e)
{}
}
}
Sample Output
[attachment=14669]