Java Program to show Various Types Of Shapes On Clicking the Buttons
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class shp extends Applet implements ActionListener
{
Button l;
Button r;
Button c;
Button t;
Object source;
public static void main(String[] args)
{
Frame f = new Frame();
shp ex = new shp();
ex.init();
f.add("Center",ex);
f.setVisible(true);
f.pack();
}
public void init()
{
l = new Button("ADD lINE");
r = new Button("ADD RECTANGLE");
c = new Button("ADD CIRCLE");
t = new Button("ADD STRING");
add(l);
add(r);
add(c);
add(t);
l.addActionListener( this );
r.addActionListener( this );
c.addActionListener( this );
t.addActionListener( this );
}
public void paint (Graphics g)
{
if (source == l){
g.setColor(Color.green);
g.drawLine(500,50,1000,100);
}
if (source == r){
g.setColor(Color.blue);
g.drawRect(500,50,500,50);
}
if (source == c){
g.setColor(Color.pink);
g.drawOval(680,50,100,100);
}
if (source == t){
g.setColor(Color.magenta);
g.drawString("RUPINDER KAUR & SONIA DAYMA ARE THE BEST",500,50);
}
}
public void actionPerformed(ActionEvent ev)
{
if (ev.getSource() == l){
source = l;
}
if (ev.getSource() == r){
source = r;
}
if (ev.getSource() == c){
source = c;
}
if (ev.getSource() == t){
source = t;
}
repaint();
}
}
OUTPUT :
1.Buttons is Shown-
2. Shown the Image Of Rectangle :
3.Shown the Circle:



No comments:
Post a Comment