Friday, July 5, 2024

Drawing Button Through Square , Oval , Line , Word (String) Java Swing

// Draw Square ,Oval ,Line ,Word (String) Through Button Swing 

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;


public class mouse extends JFrame implements ActionListener

{

JFrame f;

JButton b1,b2,b3,b4,b5;


public mouse()

{

f=new JFrame();

b1=new JButton("SQUARE");

b1.addActionListener(this);

b2=new JButton("OVAL");

b2.addActionListener(this);

b3=new JButton("LINE");

b3.addActionListener(this);

b4=new JButton("WORD");

b4.addActionListener(this);

b5=new JButton("CLOSE");

b5.addActionListener(this);

add(b1);

  add(b2);

add(b3);

add(b4);

add(b5);

setLayout(new FlowLayout());

setDefaultCloseOperation(EXIT_ON_CLOSE);

setSize(500,500);

setVisible(true);

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource()==b1)

{

Graphics g=getGraphics();

g.setColor(Color.ORANGE);

g.fillRect(100,200,100,100);

}

if(ae.getSource()==b2)

{

Graphics g=getGraphics();

g.setColor(Color.RED);

g.fillOval(300,200,100,100);

}

if(ae.getSource()==b3)

{

Graphics g=getGraphics();

g.setColor(Color.BLACK);

g.drawLine(50,50,200,150);

}

if(ae.getSource()==b4)

{

Graphics g=getGraphics();

g.setColor(Color.BLUE);

g.drawString("SIDHDHAPURA AMIT'S BLOG",250,160);

}

if(ae.getSource()==b5)

{

System.exit(0);

}

}

public static void main(String args[])

{

new mouse();

}

}


Output :-



                    Drawing Square,Oval,Line,Word(String) Through Button And 
                                     Also Use Close Button To Close Form.