How to delete drawing lines in Java?

The question is how to delete old rows? I mean, only the current X and Y lines are displayed on the screen, so that the intersection between the two lines "follows" the mouse pointer

This is the updated code:

import javax.swing.*;
import javax.*;
import java.awt.*;
import java.awt.Color;
import java.awt.Graphics.*;
import java.awt.event.*;
import javax.swing.UIManager;
public class SimpleGUI extends JFrame {
    public SimpleGUI() {
           this.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE) ;
    }

    public void go() {
        Drawpanel = new Mypanel();
        JFrame frame = new JFrame("Chasing Line");
        JButton mybutton1 = new JButton("Please");
        JButton mybutton2 = new JButton("Help");
        JButton mybutton3 = new JButton("Me!!");
        Drawpanel.add(mybutton1); 
        Drawpanel.add(mybutton2);
        Drawpanel.add(mybutton3);

        frame.getContentPane().add(BorderLayout.CENTER,Drawpanel);
        frame.setSize(300,300);
        frame.setVisible(true);

        Drawpanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                DrawpanelMouseMoved(evt);
            }
        }); 

    }

    public void DrawpanelMouseMoved(java.awt.event.MouseEvent evt) {
        xpos=evt.getX();
        ypos=evt.getY();
        System.out.println("Coordinates : X :"+ xpos+"Y: "+ypos);
        Drawpanel.paintImage(xpos,ypos);
    } 

    class Mypanel extends JPanel {
        public void paintImage(int xpost,int ypost){
            Graphics d = getGraphics();
            d.clearRect(0,this.getWidth(),this.getHeight());
            d.setColor(Color.black);
            d.drawLine(xpost,xpost,this.getHeight());
            d.setColor(Color.red);
            d.drawLine(0,ypost,ypost);
            this.validate();
        }

    } // end the inner class 

    public static void main(String[] args){
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch(Exception e) {
            System.err.println("Look and feel not set");
        }

        SimpleGUI win = new SimpleGUI();
        win.go();
    }

    Mypanel Drawpanel;
    private int xpos=0;
    private int ypos=0;
}  // close SimpleGUI class

The question is how to keep these three buttons from changing their state?

Solution

>Save all rows to be retained in LinkedList or similar. > Draw the background again and start painting, which will clear the old lines. > Draw all rows in the LinkedList

Or, if you do not want to save any rows:

>Draw the background again, which will delete the old line. > Draw your lines

You can draw the background again using the following methods:

clearRect(int x,int y,int width,int height)
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>