如何在java中删除绘制的线?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2784063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 12:50:03  来源:igfitidea点击:

How to delete Drawn Line in java?

javaswinggraphics

提问by Jeyjey

The problem is how can I delete the old lines? I mean, make only the current x and y lines appear on the screen, make the intersection between both lines "follow" the mouse pointer.

问题是如何删除旧行?我的意思是,只让当前的 x 和 y 线出现在屏幕上,让两条线之间的交点“跟随”鼠标指针。

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,0, this.getWidth(), this.getHeight());
            d.setColor(Color.black);
            d.drawLine(xpost, 0, xpost, this.getHeight());
            d.setColor(Color.red);
            d.drawLine(0, ypost, this.getWidth(),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 can I keep those 3 buttons with out altering their state?

问题是我怎样才能保留这 3 个按钮而不改变它们的状态?

回答by Jonas

The problem is how can i delete the old lines?, i mea,make only the current x and y lines appear on the screen, make the intersection between both lines "follow" the mouse pointer.

问题是如何删除旧行?,我的意思是,让屏幕上只显示当前的 x 和 y 线,使两条线之间的交点“跟随”鼠标指针。

  • Save all your lines that you want to keep in a LinkedList or similar.
  • Start painting with drawing the background again, this will clear your old lines.
  • Paint all your lines in your LinkedList.
  • 将所有要保留的行保存在 LinkedList 或类似内容中。
  • 再次绘制背景开始绘画,这将清除您的旧线条。
  • 在 LinkedList 中绘制所有线条。

Or if you don't want to save any lines:

或者,如果您不想保存任何行:

  1. Draw the background again, this removes the old lines.
  2. Draw your lines.
  1. 再次绘制背景,这将删除旧线条。
  2. 画出你的线条。

You can draw the background again with:

您可以再次绘制背景:

clearRect(int x, int y, int width, int height)

回答by chris

you'll need to do d.clear();just after the Graphics d = getGraphics();line, this will clear the existing graphics and what you draw on it after that will be all that's shown.

您需要d.clear();在该Graphics d = getGraphics();行之后执行此操作,这将清除现有图形,之后您在其上绘制的内容将显示所有内容。

回答by camickr

You should never use code that invokes getGraphics(). Custom painting is done by overriding the paintComponent() method.

永远不要使用调用 getGraphics() 的代码。自定义绘画是通过覆盖paintComponent() 方法完成的。

Custom Painting Approachesshows 2 different approaches for doing some basic painting.

自定义绘画方法展示了两种不同的基本绘画方法。

I'm not sure I understand your question about the "widgets", but the examples have buttons that are not affected by the painting.

我不确定我是否理解您关于“小部件”的问题,但示例中的按钮不受绘画影响。