Implementation of sprouting new Java drawing board

Posted by optik on Tue, 09 Jun 2020 10:21:46 +0200

Implementation of Java drawing board

Hello everyone, this is my first blog. The specific content is how to write a drawing board in Java.
This process can be divided into two parts: one is the implementation of the drawing board interface, the other is the monitoring of the drawing board and the implementation of drawing.
First of all, we need to use JFrame. First, we need to create an interface form, and then we need to set various properties of the form, and set the form visible. The code is as follows:

import javax.swing.JButton;

public class DrawUI {
	// How to display the interface
	public void shouUI() {
		// Create a form
		JFrame drawFrame = new JFrame();
		// Set the properties of the form
		drawFrame.setSize(1200, 800);
		drawFrame.setLocationRelativeTo(null);
		drawFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Set the layout of the form to flow layout
		drawFrame.setLayout(new FlowLayout());

		// Add listener
		DrawListener drawL = new DrawListener();
		// Add listener to form
		drawFrame.addMouseListener(drawL);

		// Array to store all information
		String[] typeArr = { "straight line",  "triangle", "Water drop", "square", "circular" };
		// Traversal array
		for (int i = 0; i < typeArr.length; i++) {

			// Create some buttons
			JButton btn = new JButton(typeArr[i]);
			// Add button to form
			drawFrame.add(btn);
			// monitor
			btn.addActionListener(drawL);

		}

		// Set visible
		drawFrame.setVisible(true);

		// Get the canvas of the form
		Graphics g = drawFrame.getGraphics();
		// Let the listener's canvas equal to the form's canvas
		drawL.g = g;

	}

	// Main method
	public static void main(String[] args) {
		// Create an interface object
		DrawUI ui = new DrawUI();
		// Display interface
		ui.shouUI();

	}
}

So far, we have created a visual form. The next thing we need to do is to add the drawing function we need to implement to this form. We need to use the monitor here. Suppose we want to draw a straight line. Generally, we drag a distance by pressing the mouse. When we release the mouse, the straight line will be drawn from the starting point to the ending point. We can create a new listening class. In the Buttonlistener class, we implement the MouseListener interface, so that we can monitor the mouse. Note here: the listener is an interface, so we need to implement all methods in the interface. For specific operations, select MouseListener, press F3 to view the source code, and then copy it to our newly created listener class. The specific code is as follows:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DrawListener implements MouseListener, ActionListener {

	// Record the number of coordinates
	int startx, starty, endx, endy;
	// canvas
	Graphics g;
	// Record graphic information
	String shapeType = "straight line";

	public void setGraphics(Graphics g) {
		this.g = g;

	}

	// When the button is clicked, execute
	public void actionPerformed(ActionEvent e) {
		// Get information on button
		String action = e.getActionCommand();
		// Record button information
		shapeType = action;
		System.out.println(shapeType);

	}

	public void mousePressed(MouseEvent e) {
		// Get coordinates
		startx = e.getX();
		starty = e.getY();

	}

	public void mouseReleased(MouseEvent e) {
		// Get coordinates
		endx = e.getX();
		endy = e.getY();

		System.out.println(shapeType);

		if ("straight line".equals(shapeType)) {
			g.drawLine(startx, starty, endx, endy);
		} else if ("triangle".equals(shapeType)) {
			for (int i = 0; i < 256; i++) {

				// Draw a triangle
				Color TColor = new Color(100, 200, i);
				g.setColor(TColor);
				g.drawLine(startx, starty, endx, i + 200);
			}
		} else if ("Water drop".equals(shapeType)) {
			for (int i = 0; i < 300; i++) {
				// Draw a drop of water
				Color c = new Color(180-i, 205, 230);
				g.setColor(c);
				g.fillOval(startx, starty, 200 - i, 200 - i);
			}
		} else if ("square".equals(shapeType)) {
			g.setColor(Color.pink);
			g.fillRect(startx, starty, 200, 200);

		}else if("circular".equals(shapeType)) {
			g.setColor(Color.YELLOW);
			g.fillOval(startx, starty, 150, 150);
		}

	}

	public void mouseClicked(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

}

So far we have realized some simple functions of drawing board.

(it's very rough. If you have any questions, you are welcome to communicate with me.)

Topics: Java