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

📄 paint3.java

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

public class Paint3 extends Frame implements MouseListener, MouseMotionListener
{
    int[] xa = new int[100000];
    int[] ya = new int[100000];
    int count = 0;
    int width = 0, height = 0;
    Image image = null;
	public static void main(String[] args) 
	{
		new Paint3();
	}
    public Paint3() {
        setBackground(Color.blue);
        setForeground(Color.red);
        addMouseListener(this);
        addMouseMotionListener(this);
        setSize(400,300);  width = 400;   height = 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 addNotify() {
        super.addNotify();
        image = createImage(width, height);
    }
    public void update(Graphics g) {
        paint(g);
    }
    public void paint(Graphics g2) {
        //with double buffering technology.
        Graphics g = image.getGraphics();  //usually, we needn't to clear the content.
        /*
        g.setColor(getBackground());
        g.fillRect(0,0,width,height);
        g.setColor(getForeground());
        */
        boolean flag = false;
        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]);
                flag = true;
            }
        }
        if (flag)  {
            g2.drawImage(image,0,0,this);
        }
    }

}

⌨️ 快捷键说明

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