java - 如何在java中删除画线?

标签 java swing graphics

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

这是更新后的代码:

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

问题是如何在不改变状态的情况下保留这 3 个按钮?

最佳答案

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.

  • 将所有要保留的行保存在 LinkedList 或类似列表中。
  • 重新开始绘制背景,这将清除您的旧线条。
  • 在 LinkedList 中绘制所有行。

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

  1. 再次绘制背景,这将删除旧线条。
  2. 画出你的线条。

你可以再次绘制背景:

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

关于java - 如何在java中删除画线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2784063/

相关文章:

Java 除以零不会引发 ArithmeticException - 为什么?

java - 获取 JRadioButtons ButtonGroup 中的焦点以转到当前选定的项目而不是第一个

Java Swing : Custom Cell Editor does not return most recent value

java - 添加没有 JPanel 的组件是正确的事情

c++ - 调整 OpenGL 窗口的大小会导致它分崩离析

c# - 使用矩阵变换(旋转)图像 - 设计时与运行时渲染结果

java - 强制 JTextArea 不适合 JPanel

java - intellij idea 在 : import org. apache.spark.{SparkContext, SparkConf} 上出现 scala 错误

java - JTable 打印,顶部带有前缀文本(不是页眉/页脚)

java - 在 Java 中绘制图元的最简单方法(不是 OpenGL)