0% found this document useful (0 votes)
37 views5 pages

Program 10 - Draw Shapes at Mouse Click Positions

Uploaded by

Bhuvaneshwari M
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)
37 views5 pages

Program 10 - Draw Shapes at Mouse Click Positions

Uploaded by

Bhuvaneshwari M
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/ 5

Program 10: Draw Shapes at Mouse Click Positions

Aim: Create a Java program that allows the user to draw a circle, square,
ellipse, and rectangle at the mouse click positions. The shapes can be selected
using keyboard input.

Algorithm:

Step 1: Start.

Step 2: Create a class `DrawShapesAtClick` that extends `JFrame`.

Step 3: Define an `ArrayList` to store the drawn shapes and set the default
shape choice to "C" (circle).

Step 4: Create a `JPanel` named `drawingPanel` with overridden


`paintComponent(Graphics g)` method to draw the shapes.

Step 5: Add a `MouseListener` to `drawingPanel` to handle mouse click events.


Inside the `mouseClicked(MouseEvent e)` method, get the x and y coordinates
of the mouse click, create the appropriate shape object, and add it to the
`shapes` list. Call `repaint()` on `drawingPanel` to update the display.

Step 6: Add a `KeyListener` to `drawingPanel` to handle keyboard input. Inside


the `keyTyped(KeyEvent e)` method, update the `shapeChoice` if the typed
character corresponds to a valid shape choice.

Step 7: Set `drawingPanel` to be focusable and request focus in window.

Step 8: Create classes `Circle`, `Square`, `Ellipse`, and `Rectangle` that


implement the `Shape` interface. In the `draw(Graphics g)` method of each
class, draw the corresponding shape using `Graphics` methods.

Step 9: Create the frame, add the `drawingPanel`, and set frame properties.
Show the frame.

Step 10: Stop.


Program:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class DrawShapesAtClick extends JFrame {


private ArrayList<Shape> shapes;
private String shapeChoice;

public DrawShapesAtClick() {
setTitle("Draw Shapes at Mouse Clicks");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
shapes = new ArrayList<>();
shapeChoice = "C";

JPanel drawingPanel = new JPanel() {


@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
shapes.forEach(shape -> shape.draw(g));
}
};

drawingPanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();

switch (shapeChoice) {
case "S":
shapes.add(new Square(x, y, 40));
break;
case "E":
shapes.add(new Ellipse(x, y, 50, 30));
break;
case "R":
shapes.add(new Rectangle(x, y, 70, 40));
break;
default:
shapes.add(new Circle(x, y, 30));
}
drawingPanel.repaint();
}
});

drawingPanel.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
char keyChar = Character.toLowerCase(e.getKeyChar());
if ("cser".indexOf(keyChar) != -1) {
shapeChoice = Character.toString(keyChar).toUpperCase();
}
}
});

drawingPanel.setFocusable(true);
drawingPanel.requestFocusInWindow(); // Set focus to the drawingPanel
to receive keyboard events

add(drawingPanel);
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> {
DrawShapesAtClick frame = new DrawShapesAtClick();
frame.setVisible(true);
});
}

private interface Shape {


void draw(Graphics g);
}

private static class Circle implements Shape {


private int x, y, radius;

public Circle(int x, int y, int radius) {


this.x = x;
this.y = y;
this.radius = radius;
}

@Override
public void draw(Graphics g) {
g.setColor(Color.RED);
g.drawOval(x - radius, y - radius, radius * 2, radius * 2);
}
}

private static class Square implements Shape {


private int x, y, side;
public Square(int x, int y, int side) {
this.x = x;
this.y = y;
this.side = side;
}

@Override
public void draw(Graphics g) {
g.setColor(Color.BLUE);
g.drawRect(x - side / 2, y - side / 2, side, side);
}
}

private static class Ellipse implements Shape {


private int x, y, width, height;

public Ellipse(int x, int y, int width, int height) {


this.x = x;
this.y = y;
this.width = width;
this.height = height;
}

@Override
public void draw(Graphics g) {
g.setColor(Color.GREEN);
g.drawOval(x - width / 2, y - height / 2, width, height);
}
}

private static class Rectangle implements Shape {


private int x, y, width, height;

public Rectangle(int x, int y, int width, int height) {


this.x = x;
this.y = y;
this.width = width;
this.height = height;
}

@Override
public void draw(Graphics g) {
g.setColor(Color.ORANGE);
g.drawRect(x - width / 2, y - height / 2, width, height);
}
}
}
Output:

You might also like