Java GUI 實現畫筆功能,鼠標點擊顯示

模擬畫筆功能,鼠標點擊,畫出點如圖:

在這裏插入圖片描述

一、思路

1.一個Main類裏new 一個自制的窗口類Frame
2.自制窗口類,實現一個基本的窗口,在添加一個自制的Panel,
3.在自制的panel中,使用靜態量,ArrayList points儲存應該打出的點,添加鼠標監聽MouseLitener,添加鼠標點擊的點到points中。並且repiant() 刷新Paint
points.add(new Point(e.getX(),e,getY()));
4.paint()方法中迭代 points 並打印。
 Iterator iterator= points.iterator();
 while(iterator.hasNext()){
     Point point = (Point) iterator.next();
     g.setColor(Color.red);
     g.fillOval(point.x,point.y,10,10);
 }

在這裏插入圖片描述

代碼

Main.java

package GUI.AWT.畫圖模擬;

public class Main {
    public static void main(String[] args) {
        new 畫圖Frame();
    }
}

畫圖Frame.java

package GUI.AWT.畫圖模擬;

import javax.swing.*;
import java.awt.*;

public class 畫圖Frame extends JFrame {

    public 畫圖Frame() throws HeadlessException {
        this.setTitle("畫圖Frame");
        this.setVisible(true);
        this.setBounds(100,100,400,400);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setResizable(true);
        this.add(new PaintPanle());
        //鼠標點擊事件增加數組數量


    }
}

畫圖Frame.java

package GUI.AWT.畫圖模擬;

import javax.swing.*;
import java.awt.*;

public class 畫圖Frame extends JFrame {

    public 畫圖Frame() throws HeadlessException {
        this.setTitle("畫圖Frame");
        this.setVisible(true);
        this.setBounds(100,100,400,400);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setResizable(true);
        this.add(new PaintPanle());
        //鼠標點擊事件增加數組數量


    }
}

效果

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章