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很簡(jiǎn)單的就實(shí)現(xiàn)了以下功能:
1、圖片的翻轉(zhuǎn),包括水平翻轉(zhuǎn)以及垂直翻轉(zhuǎn)
2、圖片的任意角度旋轉(zhuǎn)。因?yàn)楣こ绦枰a里面都直接寫成了+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)楣こ绦枰a中直接寫成了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;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Retrofit+Rxjava實(shí)現(xiàn)文件上傳和下載功能
這篇文章主要介紹了Retrofit+Rxjava實(shí)現(xiàn)文件上傳和下載功能,文中提到了單文件上傳和多文件上傳及相關(guān)參數(shù)的請(qǐng)求,需要的朋友參考下吧2017-11-11
SpringBoot配置文件中數(shù)據(jù)庫密碼加密兩種方案(推薦)
SpringBoot項(xiàng)目經(jīng)常將連接數(shù)據(jù)庫的密碼明文放在配置文件里,安全性就比較低一些,尤其在一些企業(yè)對(duì)安全性要求很高,因此我們就考慮如何對(duì)密碼進(jìn)行加密,文中給大家介紹加密的兩種方式,感興趣的朋友一起看看吧2019-10-10
SpringCloud Gateway HttpWebHandlerAdapter鏈路調(diào)用請(qǐng)求流程介
Spring Cloud Gateway旨在為微服務(wù)架構(gòu)提供一種簡(jiǎn)單有效的、統(tǒng)一的 API 路由管理方式。Spring Cloud Gateway 作為 Spring Cloud 生態(tài)系中的網(wǎng)關(guān),它不僅提供統(tǒng)一的路由方式,并且基于 Filter 鏈的方式提供了網(wǎng)關(guān)基本的功能,例如:安全、監(jiān)控/埋點(diǎn)和限流等2022-10-10
SpringBoot中數(shù)據(jù)傳輸對(duì)象(DTO)的實(shí)現(xiàn)
本文主要介紹了SpringBoot中數(shù)據(jù)傳輸對(duì)象(DTO)的實(shí)現(xiàn),包括了手動(dòng)創(chuàng)建DTO、使用ModelMapper和Lombok創(chuàng)建DTO的示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07
Java notify和notifyAll的區(qū)別和相同
本文主要介紹Java notify和notifyAll的知識(shí),這里整理詳細(xì)的資料來說明notify 和NotifAll的區(qū)別,有需要的小伙伴可以參考下2016-09-09
Spring Cache相關(guān)知識(shí)總結(jié)
今天帶大家學(xué)習(xí)Spring的相關(guān)知識(shí),文中對(duì)Spring Cache作了非常詳細(xì)的介紹,對(duì)正在學(xué)習(xí)Java Spring的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05

