本文實例講述了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