The code in this repository creates a Java Swing simple drawing application.

Overview

Simple Drawing

Introduction

Recently on Stack Overflow, a student had questions about his Swing project. He was trying to create a simple Swing drawing application. The code he posted was incomplete in several ways. Now, I'm not trying to pick on the student. I gave his projet a try, and it took me close to 8 hours to code it from start to finish. I have to wonder how much time the lecturer / professor gave his students for the project, and how much the student underestimated what was involved in creating the project.

I based my GUI design loosely on what the student designed. Here's what my version of the GUI looks like:

Simple Drawing GUI

The user has a choice of three colors and three shapes. The user can also clear the drawing area, save the drawing, and load a previously saved drawing. The drawing is saved using the Java Serializable interface. Although it's not shown in the image, the user can left-click on an existing shape and remove it.

To draw a shape, the user presses the left mouse button somewhere on the drawing panel, drags the mouse to a different position, and releases the left mouse button. This also works with the right mouse button. While the user is dragging, a blue rectangle outline is drawn. When the user releases the mouse button, the blue rectangle disappears and is replaced by the shape. All the shapes are filled in, as you can see in the picture.

This is a pretty simple drawing application. Microsoft Paint, to take one example, has way more functionality. As we'll see, this simple drawing application leads to some complex code. This type of Swing application makes for a good end-of-semester project.

Explanation

If you’re not familiar with Java Swing, Oracle has an excellent tutorial to get you started, Creating a GUI With Swing. Skip the Netbeans section.

When I code a Swing application, I use the model/view/controller (MVC) pattern. To be fair, the student attempted to use the MVC pattern. The MVC pattern implies that you create the model first, then the view, then the controllers.

In Java Swing, the MVC pattern is implemented like this:

  • The view reads information from the model
  • The view does not update the model
  • The controller updates the model and repaints/revalidates the view.

There's usually not one controller to "rule them all". Each listener updates its own portion of the model and repaints/revalidates its own portion of the view. The nine JButtons in the Swing GUI use nine separate anonymous controller classes. They are anonymous because they're two to three lines of code each. It's not worth setting up separate controller classes for such short classes.

Not counting the anonymous controller classes, I wrote two enum classes, two model classes, three view classes, and two controller classes. Including the initiator class, that's ten separate classes for this simple drawing application.

I did not write this code all at once. I wrote a little and tested a lot. I'd add one method at a time and test the code. Using the Eclipse Java integrated development environment (IDE) makes rapid coding and testing much easier, but any IDE would work just as well.

Model

The ColorType enum contains the three colors. Each enum points to a Color and a display String. This way, I can get the drawing Color and the JButton labels from the enum.

The ShapeType enum contains the three shapes. Each enum points to a display String. This way, I can get the JButton labels from the enum.

The DrawingShape class is a plain Java getter/setter class that contains one drawing shape. Each shape has an int x, y, width, and height. Each shape has a ColorType and a ShapeType.

The DrawingModel class is a plain Java getter/setter class that contains the current ColorType and ShapeType, two Point instances for drawing a rectangle outline, and a java.util.List of DrawingShape instances.

View

The DrawingFrame class creates the JFrame. The JFrame uses a default BorderLayout. The JFrame methods must be called in a specific order. This is the order I use for most of my Swing applications. I have three helper methods that call methods in the JPanel classes. These helper methods allow me to pass the DrawingFrame instance to the controller classes. The controller classes don't need to know how my GUI is structured.

The ControlPanel class creates the control JPanel. The control JPanel is made up of a button JPanel and a display JPanel. The button JPanel uses a FlowLayout to lay out the JButtons. I use a Box createHorizontalStrut method to space the color JButtons, shape JButtons, and control JButtons. This makes it easier for the user to see the groups.

A JMenuBar would probably work better than the nine JButtons, but I went with what the student had posted in his question. Creating an intuitive user experience is difficult. What seems natural to a developer might be strange to a typical user of the application.

The display JPanel shows the current ColorType and ShapeType instances.

The button JPanel and display JPanel are combined into a control JPanel using a BorderLayout. You can create complex Swing layouts by nesting JPanels with simpler layouts.

The DrawingPanel class creates a drawing JPanel to draw the shapes. The DrawingPanel class draws the shapes and the rectangle outline. Period. Nothing else. It's up to the controller classes to update the model and give the illusion of animation when the user drags the mouse.

Controller

The DrawingListener class implements a MouseListener and a MouseMotionListener. I use a MouseAdapter so I don't have to implement every method of the two interfaces. I've only implemented the methods I needed, mouse pressed, released, moved, and dragged.

The LoadSaveSerializable class contains the code for saving the DrawingModel to disk, and loading the DrawingModel from disk. For some reason, I had to save and load each component of the DrawingModel separately.

You might also like...

This is the primary repository for the source code of the OpenJML project.

OpenJML This is the primary repository for the OpenJML project. The active issues list for OpenJML development is here and the wiki contains informati

Dec 22, 2022

This repository will contain useful matriel and source code for OOP exam.

This repository will contain useful matriel and source code for OOP exam.

PrepForOopExam Hello everyone! I assume that you're currently studying for your OOP exam and you are probably tired from exercise 5 , don't know how o

Sep 20, 2022

This repository contains the source code for a Product Comparison solution

This repository contains the source code for a Product Comparison solution

Product Comparison Installation Guide This repository contains the source code for a Product Comparison solution. Please report any issues here. Insta

Dec 5, 2022

This repository contains all the code developed during lessions of Foundations of Informatics T2.

If you're using the content of this Repostory, please consider to Watch or Star it in order to help tracking how many people are drawing on it. Founda

Nov 24, 2022

This repository contains the code for the Runescape private server project, and this repo is soley maintained by @Avanae and @ThePolyphia and @Xeveral

Runescape: The private server project. A Runescape private server based on the 2009 era. This repository contains the code for the Runescape private s

Oct 1, 2022

This repository contains source code examples to support my course Spring Data JPA and Hibernate Beginner to Guru

Spring Data JPA - Spring Data JPA This repository contains source code examples to support my course Spring Data JPA and Hibernate Beginner to Guru Co

Aug 24, 2022

This repository includes selenium web driver tests examples using spring boot application.

This repository includes selenium web driver tests examples using spring boot application.

Selenium Web Driver Tests This repository includes selenium tests examples using custom spring boot application. Overview Run tests Additional Informa

Nov 27, 2022

Team 5468's 2022 FRC robot code. This code is written in Java and is based off of WPILib's Java control system and utilizes a command based system

FRC 2022 Team 5468's 2022 FRC robot code. This code is written in Java and is based off of WPILib's Java control system and utilizes a command based s

Oct 4, 2022

A web application to generate Java source code with spring-boot and mybatis-plus

A web application to generate Java source code with spring-boot and mybatis-plus

A web application to generate Java source code with spring-boot and mybatis-plus. Also, The class of Domain,Mapper,XML of Mapper Interface,Service,Controller are included. You can change the data source what you want to generate for your project in app running without restart this code -generator application.

Aug 29, 2022
Owner
Gilbert G. Le Blanc
I'm a retired Java software developer.
Gilbert G. Le Blanc
Java - Packet Analyzer Application based on Java, Networking and Swing UI

Network-Packet-Tracer-using-Java Java - Packet Analyzer / Sniffing System Application based on Java, Networking and Swing UI Java - Packet Analyzer Ap

Muhammad Asad 6 Feb 3, 2022
The Download Manager uses a simple yet effective GUI interface built with java’s Swing libraries

The Download Manager uses a simple yet effective GUI interface built with java’s Swing libraries.The use of Swing gives the interface a crisp, modern look and feel. The GUI maintains a list of downloads that are currently being managed.

Manish Kumar Mahawar 2 Jan 2, 2022
This repository is for Todo application. This contains the Backend part of the application.

Todo Application 개요(Abstract) 개인용 할일 목록 리스트 앱플리케이션 구축 (Personal Todo List Application) 목적 1. React.js기초, AWS서버 활용, 스프링 부트 공부 목적으로 프로젝트 시작했습니다.

Thom 3 Jan 8, 2022
Bank Statement Analyzer Application that currently runs in terminal with the commands: javac Application.java java Application [file-name].csv GUI coming soon...

Bank Statement Analyzer Application that currently runs in terminal with the commands: javac Application.java java Application [file-name].csv GUI coming soon...

Hayden Hanson 0 May 21, 2022
Business Application Platform - no-code/low-code platform to build business applications

Orienteer What is Orienteer Orienteer is Business Application Platform: Easy creation of business applications Extendable to fit your needs Dynamic da

Orienteer 189 Dec 6, 2022
Create a Music Playlist Library -Core JAVA, JAVA Swing, AWT

Project Specifications Manage Everything about a basic Music Playlist Application in Java A Music Library for Listing, Replaying, Navigating between c

Muhammad Asad 7 Nov 8, 2022
Repository with Backend code for InnoTutor project. It is written on Java/Spring.

Backend ᅠ ᅠ Developers: Daniil Livitn, Roman Soldatov Contents Requirements API Database Google credentials Hosting and CI How to install locally Code

InnoTutor 20 Sep 17, 2022
This repository consists of the code samples, assignments, and the curriculum for Data Structures & Algorithms in Java.

DSA_JAVA_REPO WELCOME TO THIS DSA REPO 100 DAYS OF CHALLENGE ⚡ Technologies Language ABOUT THE REPO It's contain basic syntex of java if you want to l

Sahitya Roy 6 Jan 21, 2022
JavaFX or Swing + jpackage + Maven template project for generating native desktop applications.

Java + Maven + GitHub Actions = Native Desktop Apps JavaFX or Swing + jpackage + Maven template project for generating native desktop applications. Go

Will Iverson 243 Dec 24, 2022
Use Swing in Minecraft

MinecraftSwing Use Swing in Minecraft This is a plugin library allowing you to use Swing in Minecraft. Throw inventory GUI away You are able to render

czm 12 Nov 15, 2022