Java learning notes - graphic drawing

UI - JFrame class

thinking

Design your own UI class.

  • There are interface initialization functions.
  • There is a running function to generate a UI object and execute the initialization method.

How to design initialization function

Use the JFrame class to create a form (window).

  • Interface design
  • Listening event - trigger function

Interface design

JFrame jf = new JFrame("The name of the form");                //Create a JFrame object
jf.setSize(1000,800);                               //The size of the form
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //The default is to close and exit, and directly exit the process
jf.setVisible(true);                                //Set visual hardware configuration (graphics card)
//There are many other settings

Listening event - trigger function

myListener mylis = new myListener(); //Define a listener object, which is a method written by itself and an inherited MouseListener interface
jf.addMouseListener(mylis);          //Let your interface add this listener function

Listener listener - MouseListener interface

What is a listener and what can it do?

  • Monitor the user's operations on the interface, such as clicking, pressing and releasing your mouse.
  • Learn button (mouse?) The data obtained by operating on the form, such as the coordinates of mouse click, left click, right click and click time.

thinking

Design a myListener class. This class inherits the MouseListener interface, so all the methods in MouseListener need to be rewritten, that is, let you decide what program these operations should execute.

How to design a class to inherit the MouseListener interface

  • Override all methods of the interface.
  • Design some functions to realize the functions you want to achieve.

Override method (all methods must be written, as specified by the interface characteristics)

public class byListener implements MouseListener {    
    int x1,y1,x2,y2,x,y;                         //It can be global or local
    Graphics mypen;                              //Define Brush 
    @Override
    public void mouseClicked(MouseEvent e) {     //Click / / the parameter of each method is an event object. An event is a subclass that captures the data monitored by the monitor (including coordinates, events, left and right buttons, etc.)
        x = e.getX();                            //Get the click position coordinates
        y = e.getY();
        System.out.println("click!");            //Output text in the run box
        //In addition to obtaining the coordinates, you can also obtain the location, time, etc. (you can see the methods that can be called by all e objects in the object prompt)
    }

    @Override
    public void mousePressed(MouseEvent e) {     //Mouse press
        x1 = e.getX();
        y1 = e.getY();
        System.out.println("press!");
    }

    @Override
    public void mouseReleased(MouseEvent e) {    //Mouse release
        x2 = e.getX();
        y2 = e.getY();
        System.out.println("release!");
    }

    @Override
    public void mouseEntered(MouseEvent e) {     //Mouse entry interface
        System.out.println("In");
    }

    @Override
    public void mouseExited(MouseEvent e) {      //The mouse leaves the interface
        System.out.println("Out");
    }

    //There are only so many methods in the MouseListener interface
}

Design your own functions

//Draw polyline function
    Random random_num = new Random();
    public void drawKLine(int x1,int y1,int x2,int y2,int range){   //Implement a polyline drawing function: polyline implementation method: find a midpoint of the line segment each time. When the midpoint of the line segment is less than 3 from the left (right) endpoint, connect this point with the left (right endpoint). It is implemented by recursive method.
        if(Math.abs(x2-x1)<3){             //End condition                        
            mypen.drawLine(x1,y1,x2,y2);   //Call the connection method of the brush
            return;                        
        }
        int cx = (x1+x2)/2;                //Find midpoint coordinates
        int cy = (y1+y2)/2;
        range *=0.7;                       //The y coordinate of the midpoint needs to be offset up and down, but the limit of offset is limited
        cy+= random_num.nextInt(range)-(range/2);  //Generate a random offset value within this boundary
        drawKLine(x1,y1,cx,cy,range);      //Recursive this method
        drawKLine(cx,cy,x2,y2,range);
    }

Graphics 

Brush Graphics. To define the brush object, you need to get it from the getGraphics() method of the interface function.

 Graphics pen = jf.getGraphics();

The brush object can call many methods, such as the line drawing function drawLine(); Then this method can be used in the listener method. (there is no interface function in the listener class. You can first define the brush object < as the attribute member of the listener function >, which can be used in the method. In the UI, you need to obtain a brush object through the getGraphics() function of the interface and assign the object to the brush member of the listener. The assignment time must be before the listener method uses the brush.)

 mypen.drawLine(x1,y1,x2,y2);

Practice

//Draw a line and generate a broken line graph between the line segments
//byUI.java

import javax.swing.*;
import java.awt.*;

public class byUI {
    public void initUI(){
        JFrame jf = new JFrame("Shanghai and Shenzhen K Line diagram");
        jf.setSize(1000,800);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);

        Graphics pen = jf.getGraphics();
        byListener mylis = new byListener();
        mylis.mypen=pen;
        jf.addMouseListener(mylis);
    }
    public static void main (String[] args){
        byUI myui = new byUI();
        myui.initUI();
    }
}


//byListener.java

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

public class byListener implements MouseListener {
    int x1,y1,x2,y2,x,y;
    Graphics mypen;
    @Override
    public void mouseClicked(MouseEvent e) {
        x = e.getX();
        y = e.getY();
        System.out.println("click!");

    }

    @Override
    public void mousePressed(MouseEvent e) {
        x1 = e.getX();
        y1 = e.getY();
        System.out.println("press!");
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        x2 = e.getX();
        y2 = e.getY();
        System.out.println("release!");
        mypen.drawLine(x1,y1,x2,y2);
        drawKLine(x1,y1,x2,y2,500);
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        System.out.println("In");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        System.out.println("Out");
    }
   
    //Draw polyline function
    Random random_num = new Random();
    public void drawKLine(int x1,int y1,int x2,int y2,int range){
        if(Math.abs(x2-x1)<3){
            mypen.drawLine(x1,y1,x2,y2);
            return;
        }
        int cx = (x1+x2)/2;
        int cy = (y1+y2)/2;
        range *=0.7;
        cy+= random_num.nextInt(range)-(range/2);
        drawKLine(x1,y1,cx,cy,range);
        drawKLine(cx,cy,x2,y2,range);
    }
}

Keywords: Java intellij-idea

Added by pngtest on Tue, 18 Jan 2022 04:27:24 +0200