加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

如何删除java中的绘制线?

发布时间:2020-12-15 08:33:29 所属栏目:Java 来源:网络整理
导读:问题是如何删除旧行?我的意思是,只在屏幕上显示当前的x和y行,使两行之间的交点“跟随”鼠标指针. 这是更新的代码: import javax.swing.*;import javax.*;import java.awt.*;import java.awt.Color;import java.awt.Graphics.*;import java.awt.event.*;imp
问题是如何删除旧行?我的意思是,只在屏幕上显示当前的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,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

问题是如何保持这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中绘制所有行.

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

>再次绘制背景,这将删除旧行.
>画出你的台词.

您可以使用以下方法再次绘制背景:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读