SoFunction
Updated on 2025-03-08

Java implementation drawing a straight line on the drawing board

Goal: Draw a straight line on the drawing board for your reference. The specific content is as follows

1. First, you need to create an interface

Process: 1. Create interface objects (already available in Java language, you can directly create objects for use).

2. For an interface, we need to set its size, center, close the interface, and visible the interface.

3. Write the main method to implement the interface.

(The interface has not yet implemented the function of drawing board)

public class DrawUI {
 //How to display the interface public void show(){
  //Create interface object JFrame jframe = new JFrame();
  //Set the interface size, position centered, close, title (1000, 900);
 (null);
 (JFrame.EXIT_ON_CLOSE);
 ("Drawing Board v2.0");
 
  //The setting interface is visible  (true);
 }
 //Main method public static void main(String[] args){
 DrawUI UI = new DrawUI();
 ();
 }
 
}

2. Create a listener class

1. Recreate a listener class: To implement drawing on the interface, we need to add a listener to the interface. ====>In fact, it allows some mouse instructions to react on the interface. In other words, the listener is a bridge connecting the mouse to the interface.

2. Listener class (Listener) implements the interface of the mouse listener (MouseListener).

3. Add all methods under the mouse listener.

public class Listener implements MouseListener{
 
  //Mouse click public void mouseClicked(MouseEvent e){}
 
  // Press the mouse public void mousePressed(MouseEvent e){}
 
  //Release the mouse public void mouseReleased(MouseEvent e){}
 
  // Enter public void mouseEntered(MouseEvent e){}
 
  // quit public void mouseExited(MouseEvent e){}
 }

3. Realize drawing straight lines

1. After creating a listener class (Listener), we need to add a listener to the interface class =====> Only after adding a listener to the interface can we draw a picture on the interface. (Written before the drawing board "visible").

Enter the code in the drawing board

//Create a listener Listener draw = new Listener(); 
//Add a listener (draw);

2. Draw a straight line, we need to use Graphics (Canvas Tool) to draw pictures. Here, the drawing board needs to obtain Graphics and set the canvas on the listener object.

(Written after the drawing board is "visible")

//Get GraphicsGraphics graph = ();
// Assign values ​​to the listener's canvas object(graph);

Then go to the listener class (Listener) to enter the code

3. Start drawing straight lines

<1> First, to draw a straight line, we need to get the coordinates of the two end points of the straight line. So define x1, y1, x2, y2.

<2>Canvas drawing, Graphics need to be defined

//Record coordinatesint x1,y1,x2,y2;
 
//Define GraphicsGraphics graph1;

<3> Methods to transfer the value of the canvas ====> The canvas value on the drawing board is equal to the canvas value on the monitor

// Method of passing canvas valuespublic void setGraphics(Graphics graph2){
graph1 = graph2;

<4>Draw a straight line with the mouse

Enter the starting point coordinate in the mouse press event

 public void mousePressed(MouseEvent e){
 //Get press information x1=();
 y1=();
 }

Enter the end coordinates in the mouse release event and draw a straight line

public void mouseReleased(MouseEvent e){
 //Get release information x2=();
 y2=();
 //Draw lines (x1,y1,x2,y2);
 }

4. Complete process:

interface

package Draw;
 
import ;
 
import ;
 
public class DrawUI {
 public void show(){
 JFrame jframe = new JFrame();
 (1000, 900);
 (null);
 (JFrame.EXIT_ON_CLOSE);
 ("Drawing Board v2.0");
 
 //Create a listener Listener draw = new Listener(); 
 //Add a listener (draw);
 
 
 (true);
 //Get Graphics Graphics graph = ();
 // Assign values ​​to the listener's canvas object (graph);
 }
 //Main method public static void main(String[] args){
 DrawUI UI = new DrawUI();
 ();
 }
 
}

Listener

package Draw;
 
import ;
import ;
import ;
 
 
public class Listener implements MouseListener{
 //Record coordinates int x1,y1,x2,y2;
 
 //Define Graphics Graphics graph1;
 
 // Method of passing canvas values public void setGraphics(Graphics graph2){
 graph1 = graph2;
 
 
 }
 
 public void mouseClicked(MouseEvent e){}
 
 public void mousePressed(MouseEvent e){
 //Get press information x1=();
 y1=();
 }
 
 public void mouseReleased(MouseEvent e){
 //Get release information x2=();
 y2=();
 //Draw lines (x1,y1,x2,y2);
 }
 
 
 public void mouseEntered(MouseEvent e){}
 
 public void mouseExited(MouseEvent e){}
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.