Showing posts with label drawing. Show all posts
Showing posts with label drawing. Show all posts

December 24, 2020

Published December 24, 2020 by

Freehand Drawing Application with Java Swing

Here is a sample Java program which is used for freehand drawing with mouse. Features include: - Colors: You can draw in different colors. The supported colors are Black, Blue, Cyan, Gray, Green, Magenta, Orange, Pink, Red, White and Yellow. - Pointer Location: The program always shows the mouse pointer location in the title bar. The title is changed dynamically for the mouse movement. This is not a serous program – it’s a fun. You can use it for education purpose only to learn about how to draw in Java.

Output of the program

Here is the full source code:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.WindowConstants;

public class FreehandDrawing extends JFrame {

    private static final long serialVersionUID = 1L;

    public FreehandDrawing() {
        initComponents();
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(dimension.width, dimension.height - 30);
    }

    private void initComponents() {

        colorButtonGroup = new ButtonGroup();
        drawingPanel = new JPanel();
        colorPanel = new JPanel();
        radioButtonBlack = new JRadioButton();
        radioButtonBlue = new JRadioButton();
        radioButtonCyan = new JRadioButton();
        radioButtonGray = new JRadioButton();
        radioButtonGreen = new JRadioButton();
        radioButtonMagenta = new JRadioButton();
        radioButtonOrange = new JRadioButton();
        radioButtonPink = new JRadioButton();
        radioButtonRed = new JRadioButton();
        radioButtonWhite = new JRadioButton();
        radioButtonYellow = new JRadioButton();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        drawingPanel.setBorder(BorderFactory.createEtchedBorder());
        drawingPanel.addMouseListener(new MouseAdapter() {
            public void mouseExited(MouseEvent evt) {
                drawingPanelMouseExited(evt);
            }
        });
        drawingPanel.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent evt) {
                drawingPanelMouseDragged(evt);
            }

            public void mouseMoved(MouseEvent evt) {
                drawingPanelMouseMoved(evt);
            }
        });

        getContentPane().add(drawingPanel, java.awt.BorderLayout.CENTER);

        colorPanel.setBorder(BorderFactory.createEtchedBorder());
        colorPanel.setLayout(new java.awt.GridLayout(1, 0));

        radioButtonBlack.setBackground(Color.BLACK);
        colorButtonGroup.add(radioButtonBlack);
        radioButtonBlack.setForeground(Color.WHITE);
        radioButtonBlack.setSelected(true);
        radioButtonBlack.setText("Black");
        radioButtonBlack.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.BLACK;
            }
        });
        colorPanel.add(radioButtonBlack);

        radioButtonBlue.setBackground(Color.BLUE);
        colorButtonGroup.add(radioButtonBlue);
        radioButtonBlue.setText("Blue");
        radioButtonBlue.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.BLUE;
            }
        });
        colorPanel.add(radioButtonBlue);

        radioButtonCyan.setBackground(Color.CYAN);
        colorButtonGroup.add(radioButtonCyan);
        radioButtonCyan.setText("Cyan");
        radioButtonCyan.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.CYAN;
            }
        });
        colorPanel.add(radioButtonCyan);

        radioButtonGray.setBackground(Color.GRAY);
        colorButtonGroup.add(radioButtonGray);
        radioButtonGray.setText("Gray");
        radioButtonGray.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.GRAY;
            }
        });
        colorPanel.add(radioButtonGray);

        radioButtonGreen.setBackground(Color.GREEN);
        colorButtonGroup.add(radioButtonGreen);
        radioButtonGreen.setText("Green");
        radioButtonGreen.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.GREEN;
            }
        });
        colorPanel.add(radioButtonGreen);

        radioButtonMagenta.setBackground(Color.MAGENTA);
        colorButtonGroup.add(radioButtonMagenta);
        radioButtonMagenta.setText("Magenta");
        radioButtonMagenta.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.MAGENTA;
            }
        });
        colorPanel.add(radioButtonMagenta);

        radioButtonOrange.setBackground(Color.ORANGE);
        colorButtonGroup.add(radioButtonOrange);
        radioButtonOrange.setText("Orange");
        radioButtonOrange.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.ORANGE;
            }
        });
        colorPanel.add(radioButtonOrange);

        radioButtonPink.setBackground(Color.PINK);
        colorButtonGroup.add(radioButtonPink);
        radioButtonPink.setText("Pink");
        radioButtonPink.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.PINK;
            }
        });
        colorPanel.add(radioButtonPink);

        radioButtonRed.setBackground(Color.RED);
        colorButtonGroup.add(radioButtonRed);
        radioButtonRed.setText("Red");
        radioButtonRed.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.RED;
            }
        });
        colorPanel.add(radioButtonRed);

        radioButtonWhite.setBackground(Color.WHITE);
        colorButtonGroup.add(radioButtonWhite);
        radioButtonWhite.setText("White");
        radioButtonWhite.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.WHITE;
            }
        });
        colorPanel.add(radioButtonWhite);

        radioButtonYellow.setBackground(Color.YELLOW);
        colorButtonGroup.add(radioButtonYellow);
        radioButtonYellow.setText("Yellow");
        radioButtonYellow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                color = Color.YELLOW;
            }
        });
        colorPanel.add(radioButtonYellow);

        getContentPane().add(colorPanel, java.awt.BorderLayout.PAGE_START);

        pack();
    }

    private void drawingPanelMouseMoved(MouseEvent evt) {
        oldX = evt.getX();
        oldY = evt.getY();
        setTitle(oldX + "," + oldY + " - Freehand Drawing");
    }

    private void drawingPanelMouseExited(MouseEvent evt) {
        setTitle("Freehand Drawing");
    }

    private void drawingPanelMouseDragged(MouseEvent evt) {

        newX = evt.getX();
        newY = evt.getY();
        graphics = drawingPanel.getGraphics();
        graphics.setColor(color);
        graphics.drawLine(oldX, oldY, newX, newY);
        oldX = newX;
        oldY = newY;
        setTitle(oldX + "," + oldY + " - Freehand Drawing");
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FreehandDrawing().setVisible(true);
            }
        });
    }

    private ButtonGroup colorButtonGroup;
    private JPanel colorPanel;
    private JPanel drawingPanel;
    private JRadioButton radioButtonBlack;
    private JRadioButton radioButtonBlue;
    private JRadioButton radioButtonCyan;
    private JRadioButton radioButtonGray;
    private JRadioButton radioButtonGreen;
    private JRadioButton radioButtonMagenta;
    private JRadioButton radioButtonOrange;
    private JRadioButton radioButtonPink;
    private JRadioButton radioButtonRed;
    private JRadioButton radioButtonWhite;
    private JRadioButton radioButtonYellow;
    private int oldX, oldY, newX, newY;
    private Color color = Color.BLACK;
    private Graphics graphics;
}
Read More