一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - Java教程 - Java實(shí)現(xiàn)的簡單畫圖板示例

Java實(shí)現(xiàn)的簡單畫圖板示例

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

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

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

這個(gè)畫圖板是我好久之前做的,之后浙大的同學(xué)需要做課設(shè)然后就花了一點(diǎn)時(shí)間將它改了一下,變得簡單些能夠方便擴(kuò)充功能,同時(shí)學(xué)習(xí)java基礎(chǔ)

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

Java實(shí)現(xiàn)的簡單畫圖板示例

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

代碼很簡單,就是jpanel進(jìn)行分區(qū),得到畫筆,同時(shí)使用畫圖的函數(shù)就可以做到了

貼代碼應(yīng)該很快就會了~

主類

?
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());
        // 實(shí)例化事件監(jiān)聽類
        drawlistener dl = new drawlistener(this);
        // 實(shí)現(xiàn)中間面板
        this.setbackground(color.white);
        jf.add(this, borderlayout.center);
        // 實(shí)現(xiàn)性狀面板
        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); // 添加事件監(jiān)聽機(jī)制
            shapepanel.add(button);
        }
        jf.add(shapepanel, borderlayout.north);
        // 實(shí)現(xiàn)顏色面板
        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); // 添加事件監(jiān)聽機(jī)制
            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);
    }
}

監(jiān)聽輔助類

?
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);
        }
    }
    // 實(shí)現(xiàn)畫筆
    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);
            }
        }
    }
}

代碼量也還是挺小的,因?yàn)槭呛唵萎嫲迓飤~

希望本文所述對大家java程序設(shè)計(jì)有所幫助。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 美女被到爽流动漫 | 精品久久久久中文字幕日本 | 成人福利在线观看 | 国产成人咱精品视频免费网站 | 亚洲高清在线天堂精品 | 黄a 大片a v 永久免费 | 午夜一个人在线观看完整版 | 视频在线观看高清免费看 | 欧乱色国产精品兔费视频 | 日本加勒比在线精品视频 | 91制片厂(果冻传媒)原档破解 | 欧美日韩一区二区综合 | 99热久久国产精品这里 | 99久久久久国产精品免费 | 亚洲香蕉综合在人在线视看 | caopren免费视频国产 | 美妇在线 | 饭冈加奈子乳喷cead144 | 国产成+人+亚洲+欧美综合 | 国产精品国色综合久久 | 三级欧美在线 | 91.prom在线观看国产 | 亚洲男gay | 国产成人在线免费视频 | 国产自拍视频网站 | 98色花堂永久地址国产精品 | 国产精品欧美日韩一区二区 | 视频亚洲一区 | 亚洲一级特黄特黄的大片 | 日韩欧美中文字幕一区 | 亚洲乱码一二三四区国产 | 天天gan| 99久久国产综合精品麻豆 | 秋霞717理论片在线观看 | 69pao强力打造免费高速 | 欧美日韩一区二区三区免费 | 欧美在线视频一区 | 天天操免费视频 | 国产精品久久一区 | 国产99视频精品免费视频7 | 亚洲欧美成人综合久久久 |