⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paint2.java

📁 实现画直线功能
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;

public class Paint2 extends Frame implements MouseListener, MouseMotionListener
{
    int[] xa = new int[100000];
    int[] ya = new int[100000];
    int count = 0;
	public static void main(String[] args) 
	{
		new Paint2();
	}
    public Paint2() {
        setBackground(Color.blue);
        setForeground(Color.red);
        addMouseListener(this);
        addMouseMotionListener(this);
        setSize(400,300);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0); }});
    }

    public void mouseClicked(MouseEvent me) {}
    public void mouseEntered(MouseEvent me) {}
    public void mouseExited(MouseEvent me) {}
    public void mousePressed(MouseEvent me) {}
    public void mouseReleased(MouseEvent me) {
        xa[count] = -1; ya[count++] = -1;
    }
    public void mouseMoved(MouseEvent me) {}
    public void mouseDragged(MouseEvent me) {
        xa[count] = me.getX();  ya[count++] = me.getY();
        repaint();       
    }
    public void update(Graphics g) {
        paint(g);
    }
    public void paint(Graphics g) {
        for(int i=0; i<count-1; i++) {
            if (xa[i]!=-1 && xa[i+1]!=-1)
            {
                g.drawLine(xa[i],ya[i],xa[i+1],ya[i+1]);
            }
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -