最近幾天在做一個項目,因為涉及到了圖片(絕大部分都不是整圖,是把一張張的大圖切成小圖,也就是title)的翻轉(zhuǎn)以及90°旋轉(zhuǎn),弄得焦頭爛額。在網(wǎng)上搜索好幾天,發(fā)現(xiàn)用到的方法都是比較公式化的,對于只是在繪圖的時候需要顯示翻轉(zhuǎn)而不需要另外生成圖片的情況,這些代碼用起來非常的麻煩。最后仔細(xì)的研究了一下jdk文檔,用graphics2d很簡單的就實(shí)現(xiàn)了以下功能:
1、圖片的翻轉(zhuǎn),包括水平翻轉(zhuǎn)以及垂直翻轉(zhuǎn)
2、圖片的任意角度旋轉(zhuǎn)。因為工程需要,代碼里面都直接寫成了+90,根據(jù)需要,可以對這個值進(jìn)行改動,以符合需求。
3、可以使用組合操作,比如水平翻轉(zhuǎn)+旋轉(zhuǎn),或者垂直+水平+旋轉(zhuǎ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
170
171
172
173
174
175
176
177
|
package demo628; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class imagerote { public static void main(string[] args) { jframe frame = new transformframe(); frame.setvisible( true ); } } class transformframe extends jframe implements actionlistener { //添加幾個按鈕方便操作。 jbutton rote = new jbutton( "旋轉(zhuǎn)" ) ; jbutton flipx= new jbutton( "水平翻轉(zhuǎn)" ); jbutton flipy= new jbutton( "垂直翻轉(zhuǎn)" ); jbutton zoomin = new jbutton( "放大" ) ; jbutton zoomout = new jbutton( "縮小" ) ; public transformframe() { settitle( "transformtest" ); setsize( 400 , 400 ); addwindowlistener( new windowadapter() { public void windowclosing(windowevent e) { system.exit( 0 ); } }); container contentpane = getcontentpane(); canvas = new transpanel(); contentpane.add(canvas, "center" ); jpanel buttonpanel = new jpanel(); buttonpanel.add(rote); rote.addactionlistener( this ); buttonpanel.add(flipx); flipx.addactionlistener( this ); buttonpanel.add(flipy); flipy.addactionlistener( this ); buttonpanel.add(zoomin) ; zoomin.addactionlistener( this ) ; buttonpanel.add(zoomout) ; zoomout.addactionlistener( this ) ; contentpane.add(buttonpanel, "north" ); } public void actionperformed(actionevent event) { object source = event.getsource(); //對于source == ???這種方法,在特殊的情況下出現(xiàn)錯誤,所以,需要酌情使用event.getsource().equals()方法來替代== if (source == rote) { canvas.setrotate(); } else if (source == flipx) { canvas.flipx(); } else if (source == flipy) { canvas.flipy(); } else if (source == zoomin) { canvas.zoomin(); } else if (source == zoomout) { canvas.zoomout(); } } private transpanel canvas; } class transpanel extends jpanel { //水平翻轉(zhuǎn)比例的標(biāo)志。-1表示需要進(jìn)行水平翻轉(zhuǎn) int m_nflipxscale = 1 ; //垂直翻轉(zhuǎn)比例的標(biāo)志。-1表示需要進(jìn)行垂直翻轉(zhuǎn) int m_nflipyscale = 1 ; //旋轉(zhuǎn)的角度。因為工程需要,代碼中直接寫成了90,可以根據(jù)具體需要動態(tài)修改,以符合實(shí)際情況 int roteangle = 0 ; //縮放比例。默認(rèn)的比例0表示沒有翻轉(zhuǎn),具體的翻轉(zhuǎn)大小通過一個方法:getzoomsize()獲取 int zoomlevel = 0 ; public transpanel() { //首先載入一張圖片。 img = new imageicon( "d000.gif" ).getimage(); } public void paintcomponent(graphics g) { super .paintcomponent(g); g.drawimage(img, 0 , 0 , this ) ; drawtransimage(g,img.getwidth( this ),img.getheight( this ),zoomlevel) ; } public void drawtransimage(graphics g, int drawx, int drawy, int zoom) { int x = 0 ; int y = 0 ; int w = img.getwidth( this ) ; int h = img.getheight( this ) ; int zoomw = getzoomsize(w,zoom) ; int zoomh = getzoomsize(h,zoom) ; int xpos = 0 ; int ypos = 0 ; if (m_nflipxscale == - 1 ) xpos = -zoomw ; if (m_nflipyscale == - 1 ) ypos = -zoomh ; graphics2d g2 = (graphics2d)g ; //轉(zhuǎn)換坐標(biāo)原點(diǎn)。這步不要也成,但是將當(dāng)前位置轉(zhuǎn)換為坐標(biāo)原點(diǎn)后,可以節(jié)省好多計算步驟,非常好用。 //不過記得用完了以后,一定要把原點(diǎn)轉(zhuǎn)換回來,要不然其他地方就亂了 g2.translate(drawx,drawy); if (roteangle != 0 ) g2.rotate(math.toradians(m_nflipxscale * m_nflipyscale * roteangle),zoomw >> 1 ,zoomh >> 1 ); //上面的m_nflipxscale * m_nflipyscale需要特殊說明一下:因為實(shí)際使用中,可能遇到各種組合的情況,比如 //先flipx或者flipy以后然后再旋轉(zhuǎn),這時候,圖片的旋轉(zhuǎn)方向就會出現(xiàn)錯誤,加上這段代碼可以保證無論使用哪種組合 //操作方式,都保證在旋轉(zhuǎn)圖片的時候是按照順時針的方向進(jìn)行旋轉(zhuǎn)。 if (m_nflipxscale == - 1 ) g2.scale(- 1 , 1 ); //第一個值表示水平,-1表示等寬水平翻轉(zhuǎn),math.abs(m_nflipxscale)的值越大,出來的圖片就越寬 if (m_nflipyscale == - 1 ) g2.scale( 1 ,- 1 ); //第二個值表示垂直,-1表示等高垂直翻轉(zhuǎn),math.abs(m_nflipyscale)的值越大,出來的圖片就越高 //顯示圖片 g2.drawimage(img,xpos,ypos,xpos + zoomw,ypos + zoomh,x,y,w,h, null ) ; g2.translate(-drawx,-drawy); } public void setrotate() { roteangle += 90 ; roteangle %= 360 ; repaint(); } public void flipx() { m_nflipxscale = -m_nflipxscale ; repaint(); } public void flipy() { m_nflipyscale = -m_nflipyscale ; repaint(); } public void zoomin() { zoomlevel++ ; repaint(); } public void zoomout() { zoomlevel-- ; repaint(); } public static final int getzoomsize( int sourcesize, int zoomlevel) { if (zoomlevel == 0 ) return sourcesize ; else if (zoomlevel < 0 ) return sourcesize / (math.abs(zoomlevel) + 1 ) ; else return sourcesize * (zoomlevel + 1 ) ; } private image img; } |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/wyg_vip/article/details/4109261