激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java實現的簡單畫圖板示例

Java實現的簡單畫圖板示例

2021-05-27 13:51SX_csu2016sw Java教程

這篇文章主要介紹了Java實現的簡單畫圖板,涉及java使用swing組件進行圖形繪制相關操作技巧,需要的朋友可以參考下

本文實例講述了java實現的簡單畫圖板。分享給大家供大家參考,具體如下:

這個畫圖板是我好久之前做的,之后浙大的同學需要做課設然后就花了一點時間將它改了一下,變得簡單些能夠方便擴充功能,同時學習java基礎

先截圖一下吧,就可以知道有哪些功能了~

Java實現的簡單畫圖板示例

三個分區,上面選擇圖形,下面選擇顏色,立體圓就是一個分形,也先放著不需要的同學可以注釋了它

代碼很簡單,就是jpanel進行分區,得到畫筆,同時使用畫圖的函數就可以做到了

貼代碼應該很快就會了~

主類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package awtdemo;
import java.awt.borderlayout;
import java.awt.color;
import java.awt.dimension;
import java.awt.flowlayout;
import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.jpanel;
@suppresswarnings("serial")
public class drawmain extends jpanel {
    public static void main(string[] args) {
        // todo auto-generated method stub
        drawmain draw = new drawmain();
        draw.initui();
    }
    public void initui() {
        jframe jf = new jframe();
        jf.setsize(1000, 780);
        jf.settitle("簡單畫板");
        jf.setdefaultcloseoperation(3);
        jf.setlocationrelativeto(null);
        jf.setlayout(new borderlayout());
        // 實例化事件監聽類
        drawlistener dl = new drawlistener(this);
        // 實現中間面板
        this.setbackground(color.white);
        jf.add(this, borderlayout.center);
        // 實現性狀面板
        jpanel shapepanel = new jpanel();
        shapepanel.setbackground(color.black);
        shapepanel.setlayout(new flowlayout(flowlayout.center));
        shapepanel.setbackground(color.gray);
        ;
        string[] shape = { "直線", "曲線", "圓", "噴槍", "橡皮擦", "矩形", "橢圓", "圓角矩形",
                "弧線", "多邊形", "圖形", "三角形", "立體圓", };
        for (int i = 0; i < shape.length; i++) {
            jbutton button = new jbutton(shape[i]);
            button.setbackground(color.white);
            button.addactionlistener(dl); // 添加事件監聽機制
            shapepanel.add(button);
        }
        jf.add(shapepanel, borderlayout.north);
        // 實現顏色面板
        jpanel colorpanel = new jpanel();
        colorpanel.setbackground(color.black);
        colorpanel.setlayout(new flowlayout(flowlayout.center));
        colorpanel.setbackground(color.gray);
        ;
        color[] color = { color.black, color.blue, color.white, color.gray,
                color.red, color.cyan, color.green, color.darkgray, color.pink };
        for (int i = 0; i < color.length; i++) {
            jbutton button = new jbutton();
            button.addactionlistener(dl); // 添加事件監聽機制
            button.setpreferredsize(new dimension(30, 30));
            button.setbackground(color[i]);
            colorpanel.add(button);
        }
        jf.add(colorpanel, borderlayout.south);
        jf.setvisible(true);
        this.addmouselistener(dl);
        this.addmousemotionlistener(dl);
    }
}

監聽輔助類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package awtdemo;
import java.awt.basicstroke;
import java.awt.color;
import java.awt.graphics2d;
import java.awt.renderinghints;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.awt.event.mouseadapter;
import java.awt.event.mouseevent;
import java.util.random;
import javax.swing.jbutton;
public class drawlistener extends mouseadapter implements actionlistener {
    private int x1, y1, x2, y2;
    private int newx1, newy1, newx2, newy2;
    private graphics2d g;
    private drawmain df;
    private boolean flag = false;
    string shape = "直線";
    color color;
    private int[] arrx = new int[4];
    private int[] arry = new int[4];
    private int temp = 0;
    drawlistener(drawmain d) {
        df = d;
    }
    // 獲取形狀和顏色
    public void actionperformed(actionevent e) {
        if (e.getactioncommand().equals("")) {
            jbutton button = (jbutton) e.getsource();
            color = button.getbackground();
            system.out.println("color = " + color);
        } else {
            jbutton button = (jbutton) e.getsource();
            shape = button.getactioncommand();
            system.out.println("string = " + shape);
        }
    }
    // 實現畫筆
    public void mousepressed(mouseevent e) {
        g = (graphics2d) df.getgraphics();
        g.setcolor(color);
        x1 = e.getx();
        y1 = e.gety();
    }
    public void mousereleased(mouseevent e) {
        x2 = e.getx();
        y2 = e.gety();
        if (shape.equals("直線")) {
            g.drawline(x1, y1, x2, y2);
        } else if (shape.equals("弧線")) {
            g.drawarc(x1, y1, math.abs(x2 - x1), math.abs(y2 - y1), 0, 180);
        } else if (shape.equals("多邊形") && !flag) {
            g.drawline(x1, y1, x2, y2);
            newx1 = x1;
            newy1 = y1;
            newx2 = x2;
            newy2 = y2;
            flag = true;
        } else if (shape.equals("圓")) {
            g.drawoval(x1, y1, math.abs(x2 - x1), math.abs(y2 - y1));
        } else if (shape.equals("矩形")) {
            g.drawrect(x1, y1, math.abs(x2 - x1), math.abs(y2 - y1));
        } else if (shape.equals("圓角矩形")) {
            g.drawroundrect(x1, y1, math.abs(x2 - x1), math.abs(y2 - y1), 2, 10);
        } else if (shape.equals("橢圓")) {
            g.drawoval(x1, y1, math.abs(x2 - x1), math.abs(y2 - y1));
        }
    }
    public void mouseclicked(mouseevent e) {
        if (shape.equals("多邊形") && flag) {
            x2 = e.getx();
            y2 = e.gety();
            if (e.getclickcount() == 2) {
                g.drawline(newx1, newy1, newx2, newy2);
                flag = false;
            }
            g.drawline(newx2, newy2, x2, y2);
            newx2 = x2;
            newy2 = y2;
        } else if (shape.equals("圖形")) {
            arrx[temp] = e.getx();
            arry[temp] = e.gety();
            temp++;
            if (temp == 4) {
                int x = arrx[3];
                int y = arry[3];
                for (int i = 0; i <= 10000; i++) {
                    random ran = new random();
                    int k = ran.nextint(3);
                    x = (x + arrx[k]) / 2;
                    y = (y + arry[k]) / 2;
                    g.drawline(x, y, x, y);
                }
                temp = 0;
            }
        } else if (shape.equals("立體圓")) {
            // double a=-2,b=-2,c=-1.2,d=2;
            double a = 1.40, b = 1.56, c = 1.40, d = -6.56;
            double x = 0, xo = 0;
            double y = 0, yo = 0;
            color[] col = { color.blue, color.cyan, color.green, color.magenta,
                    color.red, color.yellow };
            for (int i = 0; i <= 90000; i++) {
                random r = new random(); // 增加顏色
                int r = r.nextint(col.length);
                g.setcolor(col[r]);
                // x=math.sin(a*yo)-math.cos(b*xo);
                // y=math.sin(c*xo)-math.cos(d*yo);
                x = d * math.sin(a * xo) - math.sin(b * yo);
                y = c * math.cos(a * xo) + math.cos(b * yo);
                int temp_x = (int) (x * 50);
                int temp_y = (int) (y * 50);
                g.drawline(temp_x + 500, temp_y + 300, temp_x + 500,
                        temp_y + 300);
                xo = x;
                yo = y;
            }
        } else if (shape.equals("三角形")) {
            double a = -2, b = -2, c = -1.2, d = 2;
            double x = 0, xo = 0;
            double y = 0, yo = 0;
            color[] col = { color.blue, color.cyan, color.green, color.magenta,
                    color.red, color.yellow };
            for (int i = 0; i <= 90000; i++) {
                random r = new random(); // 增加顏色
                int r = r.nextint(col.length);
                g.setcolor(col[r]);
                x = math.sin(a * yo) - math.cos(b * xo);
                y = math.sin(c * xo) - math.cos(d * yo);
                int temp_x = (int) (x * 50);
                int temp_y = (int) (y * 50);
                g.drawline(temp_x + 500, temp_y + 300, temp_x + 500,
                        temp_y + 300);
                xo = x;
                yo = y;
            }
        }
    }
    public void mousedragged(mouseevent e) {
        x2 = e.getx();
        y2 = e.gety();
        if (shape.equals("曲線")) {
            // g.setstroke(new basicstroke(10));
            // g.setrenderinghint(renderinghints.key_antialiasing,
            // renderinghints.value_antialias_on);
            g.drawline(x1, y1, x2, y2);
            x1 = x2;
            y1 = y2;
        } else if (shape.equals("橡皮擦")) {
            g.setstroke(new basicstroke(80));
            g.setrenderinghint(renderinghints.key_antialiasing,
                    renderinghints.value_antialias_on);
            g.setcolor(color.white);
            g.drawline(x1, y1, x2, y2);
            x1 = x2;
            y1 = y2;
        } else if (shape.equals("噴槍")) {
            // g.setstroke(new basicstroke(2)); //不用加粗
            // g.setrenderinghint(renderinghints.key_antialiasing,
            // renderinghints.value_antialias_on);
            for (int k = 0; k < 20; k++) {
                random i = new random();
                int a = i.nextint(8);
                int b = i.nextint(10);
                g.drawline(x2 + a, y2 + b, x2 + a, y2 + b);
            }
        }
    }
}

代碼量也還是挺小的,因為是簡單畫板嘛~~

希望本文所述對大家java程序設計有所幫助。

原文鏈接:https://blog.csdn.net/SX_csu2016sw/article/details/76570307

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: av在线免费观看不卡 | 免费黄色日韩电影 | 国毛片| 91不雅视频 | 可以看逼的视频 | 欧美成人精品一区二区男人小说 | 欧美亚州 | 成人午夜看片 | 黄色网址免费在线播放 | 黄在线| h色视频网站 | 看免费一级毛片 | 久久55 | 日本特级a一片免费观看 | 青青草好吊色 | 欧美性猛交xxxxx按摩国内 | 7m视频成人精品分类 | 欧美日韩亚洲国产精品 | 激情夜色 | 精品一区二区亚洲 | 久久夜夜视频 | 久久国产精品二区 | 中文在线日韩 | 欧美福利视频一区二区 | 黄色一级视频 | 日韩黄色片免费看 | 久久久久久久99 | 一级黄色淫片 | 日韩在线播放中文字幕 | 调教小男生抽打尿孔嗯啊视频 | 99999久久久久久 | 欧洲精品久久 | 久久国产成人午夜av浪潮 | 成人免费激情视频 | 午夜九九九 | 欧美人与性禽动交精品 | 999久久国产| 国产一区毛片 | 久草在线视频新 | 欧美大屁股精品毛片视频 | 情侣啪啪网站 |