PROGRAM-10
Write a Java Program to draw a circle, square, ellipse and rectangle at the mouse
click positions
SOURCE CODE
import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
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 = [Link]();
repaint(); //calls paint method
}
});
String[] shapes = {"Circle", "Square", "Ellipse", "Rectangle"};
JComboBox<String> shapeCB = new JComboBox<String>(shapes);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e){
selectedShape = (String) [Link]();
repaint();
}
});
add(new JLabel("Select Shape: "));
add(shapeCB);
public void paint(Graphics g)
{
[Link](g);
if (clickPoint != null)
{
int size = 50;
if ([Link]("Circle"))
{
[Link](clickPoint.x - size / 2, clickPoint.y - size / 2, size, size);
}
else if ([Link]("Square"))
{
[Link](clickPoint.x - size / 2, clickPoint.y - size / 2, size, size);
}
else if ([Link]("Ellipse"))
{
[Link](clickPoint.x - size, clickPoint.y - size / 2, size * 2, size);
}
else if ([Link]("Rectangle"))
{
[Link](clickPoint.x - size, clickPoint.y - size / 2, size * 2, size);
}
}
}
public static void main(String[] args)
{
PL10 shapes = new PL10();
[Link](true);
}
}
PROGRAM-10-OUTPUT