Drawing Shapes with Mouse Events

 

  1. Set up the main frame and panel:

    • Create a JFrame for the main window.
    • Add a custom JPanel to handle drawing.
  2. Handle mouse events:

    • Implement MouseListener and MouseMotionListener to track the starting and ending points for drawing.
  3. Implement drawing logic:

    • Allow the user to choose between drawing lines, rectangles, and ovals.
    • Draw shapes based on user interactions.

Here's a complete Java program to accomplish this:

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

public class DrawingApp extends JFrame {
    private DrawArea drawArea;
    private String currentShape = "Line";

    public DrawingApp() {
        drawArea = new DrawArea();
        add(drawArea, BorderLayout.CENTER);

        JPanel controls = new JPanel();
        String[] shapes = {"Line", "Rectangle", "Oval"};
        JComboBox<String> shapeSelector = new JComboBox<>(shapes);
        shapeSelector.addActionListener(e -> currentShape = (String) shapeSelector.getSelectedItem());
        controls.add(new JLabel("Shape:"));
        controls.add(shapeSelector);

        add(controls, BorderLayout.NORTH);

        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Drawing App");
        setVisible(true);
    }

    private class DrawArea extends JPanel implements MouseListener, MouseMotionListener {
        private ArrayList<Shape> shapes = new ArrayList<>();
        private Point start, end;

        public DrawArea() {
            setBackground(Color.WHITE);
            addMouseListener(this);
            addMouseMotionListener(this);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            for (Shape shape : shapes) {
                shape.draw(g);
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {
            start = e.getPoint();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            end = e.getPoint();
            Shape shape = null;
            switch (currentShape) {
                case "Line":
                    shape = new Line(start, end);
                    break;
                case "Rectangle":
                    shape = new Rect(start, end);
                    break;
                case "Oval":
                    shape = new Oval(start, end);
                    break;
            }
            if (shape != null) {
                shapes.add(shape);
            }
            repaint();
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            end = e.getPoint();
            repaint();
        }

        @Override
        public void mouseMoved(MouseEvent e) {}
        @Override
        public void mouseClicked(MouseEvent e) {}
        @Override
        public void mouseEntered(MouseEvent e) {}
        @Override
        public void mouseExited(MouseEvent e) {}
    }

    abstract class Shape {
        Point start, end;

        Shape(Point start, Point end) {
            this.start = start;
            this.end = end;
        }

        abstract void draw(Graphics g);
    }

    class Line extends Shape {
        Line(Point start, Point end) {
            super(start, end);
        }

        @Override
        void draw(Graphics g) {
            g.drawLine(start.x, start.y, end.x, end.y);
        }
    }

    class Rect extends Shape {
        Rect(Point start, Point end) {
            super(start, end);
        }

        @Override
        void draw(Graphics g) {
            int x = Math.min(start.x, end.x);
            int y = Math.min(start.y, end.y);
            int width = Math.abs(start.x - end.x);
            int height = Math.abs(start.y - end.y);
            g.drawRect(x, y, width, height);
        }
    }

    class Oval extends Shape {
        Oval(Point start, Point end) {
            super(start, end);
        }

        @Override
        void draw(Graphics g) {
            int x = Math.min(start.x, end.x);
            int y = Math.min(start.y, end.y);
            int width = Math.abs(start.x - end.x);
            int height = Math.abs(start.y - end.y);
            g.drawOval(x, y, width, height);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(DrawingApp::new);
    }
}

Explanation:
  1. Main Frame (DrawingApp):

    • Contains the DrawArea panel for drawing and a control panel with a shape selector.
    • Uses JComboBox to allow the user to choose the shape type.
  2. DrawArea (DrawArea extends JPanel):

    • Manages the drawing logic.
    • Implements MouseListener and MouseMotionListener to handle mouse events.
    • Maintains a list of shapes (ArrayList<Shape>) and redraws them in paintComponent.
  3. Shape Classes:

    • Abstract Shape class and its subclasses (Line, Rect, Oval) handle the drawing of different shapes.

Comments

Popular posts from this blog

KTU OOP LAB JAVA CSL 203 BTech CS S3 - Dr Binu V P

Syllabus and Practice Questions KTU OOPS Lab Java CSL 203

String Problems