0% found this document useful (0 votes)
3 views3 pages

PROGRAM10

This Java program creates a graphical user interface that allows users to draw shapes (circle, square, ellipse, rectangle) at mouse click positions. It utilizes Swing components and event listeners to capture mouse clicks and shape selections from a dropdown menu. The selected shape is drawn on the screen based on the user's input and click location.

Uploaded by

joyalprincess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

PROGRAM10

This Java program creates a graphical user interface that allows users to draw shapes (circle, square, ellipse, rectangle) at mouse click positions. It utilizes Swing components and event listeners to capture mouse clicks and shape selections from a dropdown menu. The selected shape is drawn on the screen based on the user's input and click location.

Uploaded by

joyalprincess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PROGRAM-10

Write a Java Program to draw a circle, square, ellipse and rectangle at the mouse
click positions
SOURCE CODE
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PL10 extends JFrame


{
private Point clickPoint;
private String selectedShape;

public PL10()
{
setTitle("Mouse Drawer");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());

addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
clickPoint = e.getPoint();
repaint(); //calls paint method
}
});

String[] shapes = {"Circle", "Square", "Ellipse", "Rectangle"};

JComboBox<String> shapeCB = new JComboBox<String>(shapes);


shapeCB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
selectedShape = (String) shapeCB.getSelectedItem();
repaint();
}
});
add(new JLabel("Select Shape: "));
add(shapeCB);

public void paint(Graphics g)


{
super.paint(g);
if (clickPoint != null)
{
int size = 50;
if (selectedShape.equals("Circle"))
{
g.drawOval(clickPoint.x - size / 2, clickPoint.y - size / 2, size, size);
}
else if (selectedShape.equals("Square"))
{
g.drawRect(clickPoint.x - size / 2, clickPoint.y - size / 2, size, size);
}
else if (selectedShape.equals("Ellipse"))
{
g.drawOval(clickPoint.x - size, clickPoint.y - size / 2, size * 2, size);
}
else if (selectedShape.equals("Rectangle"))
{
g.drawRect(clickPoint.x - size, clickPoint.y - size / 2, size * 2, size);
}
}
}
public static void main(String[] args)
{
PL10 shapes = new PL10();
shapes.setVisible(true);
}
}
PROGRAM-10-OUTPUT

You might also like