Java實(shí)現(xiàn)圖片翻轉(zhuǎn)以及任意角度旋轉(zhuǎn)
最近幾天在做一個(gè)項(xiàng)目,因?yàn)樯婕暗搅藞D片(絕大部分都不是整圖,是把一張張的大圖切成小圖,也就是Title)的翻轉(zhuǎn)以及90°旋轉(zhuǎn),弄得焦頭爛額。在網(wǎng)上搜索好幾天,發(fā)現(xiàn)用到的方法都是比較公式化的,對(duì)于只是在繪圖的時(shí)候需要顯示翻轉(zhuǎn)而不需要另外生成圖片的情況,這些代碼用起來非常的麻煩。最后仔細(xì)的研究了一下JDK文檔,用Graphics2D很簡單的就實(shí)現(xiàn)了以下功能:
1、圖片的翻轉(zhuǎn),包括水平翻轉(zhuǎn)以及垂直翻轉(zhuǎn)
2、圖片的任意角度旋轉(zhuǎn)。因?yàn)楣こ绦枰?,代碼里面都直接寫成了+90,根據(jù)需要,可以對(duì)這個(gè)值進(jìn)行改動(dòng),以符合需求。
3、可以使用組合操作,比如水平翻轉(zhuǎn)+旋轉(zhuǎn),或者垂直+水平+旋轉(zhuǎn),任意。
以下是代碼:
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 { //添加幾個(gè)按鈕方便操作。 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(); //對(duì)于source == ???這種方法,在特殊的情況下出現(xiàn)錯(cuò)誤,所以,需要酌情使用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)的角度。因?yàn)楣こ绦枰?,代碼中直接寫成了90,可以根據(jù)具體需要?jiǎng)討B(tài)修改,以符合實(shí)際情況 int roteAngle = 0 ; //縮放比例。默認(rèn)的比例0表示沒有翻轉(zhuǎn),具體的翻轉(zhuǎn)大小通過一個(gè)方法: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é)省好多計(jì)算步驟,非常好用。 //不過記得用完了以后,一定要把原點(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需要特殊說明一下:因?yàn)閷?shí)際使用中,可能遇到各種組合的情況,比如 //先flipX或者flipY以后然后再旋轉(zhuǎn),這時(shí)候,圖片的旋轉(zhuǎn)方向就會(huì)出現(xiàn)錯(cuò)誤,加上這段代碼可以保證無論使用哪種組合 //操作方式,都保證在旋轉(zhuǎn)圖片的時(shí)候是按照順時(shí)針的方向進(jìn)行旋轉(zhuǎn)。 if (m_nFlipXScale == -1) g2.scale(-1,1);//第一個(gè)值表示水平,-1表示等寬水平翻轉(zhuǎn),Math.abs(m_nFlipXScale)的值越大,出來的圖片就越寬 if (m_nFlipYScale == -1) g2.scale(1,-1);//第二個(gè)值表示垂直,-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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring實(shí)現(xiàn)控制反轉(zhuǎn)和依賴注入的示例詳解
這篇文章主要為大家詳細(xì)介紹IoC(控制反轉(zhuǎn))和DI(依賴注入)的概念,以及如何在Spring框架中實(shí)現(xiàn)它們,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-08-08redis防止重復(fù)提交的實(shí)現(xiàn)示例
在開發(fā)中我們都需要處理重復(fù)提交的問題,本文主要介紹了redis防止重復(fù)提交的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06SpringBoot集成Druid監(jiān)控頁面最小化配置操作
這篇文章主要介紹了SpringBoot集成Druid監(jiān)控頁面最小化配置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09GraalVM和Spring Native嘗鮮一步步讓Springboot啟動(dòng)飛起來66ms完成啟動(dòng)
GraalVM是高性能的JDK,支持Java/Python/JavaScript等語言,它可以讓Java變成二進(jìn)制文件來執(zhí)行,讓程序在任何地方運(yùn)行更快,這篇文章主要介紹了GraalVM和Spring Native嘗鮮一步步讓Springboot啟動(dòng)飛起來66ms完成啟動(dòng),需要的朋友可以參考下2023-02-02springboot如何接收application/x-www-form-urlencoded類型的請(qǐng)求
這篇文章主要介紹了springboot如何接收application/x-www-form-urlencoded類型的請(qǐng)求,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11spring boot + mybatis如何實(shí)現(xiàn)數(shù)據(jù)庫的讀寫分離
這篇文章主要給大家介紹了關(guān)于spring boot + mybatis如何實(shí)現(xiàn)數(shù)據(jù)庫的讀寫分離的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09