這篇文章主要為大家詳細(xì)介紹了使用Java怎么實(shí)現(xiàn)一個(gè)圖片翻轉(zhuǎn)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,發(fā)現(xiàn)的小伙伴們可以參考一下:

網(wǎng)站的建設(shè)創(chuàng)新互聯(lián)建站專(zhuān)注網(wǎng)站定制,經(jīng)驗(yàn)豐富,不做模板,主營(yíng)網(wǎng)站定制開(kāi)發(fā).小程序定制開(kāi)發(fā),H5頁(yè)面制作!給你煥然一新的設(shè)計(jì)體驗(yàn)!已為成都塑料袋等企業(yè)提供專(zhuān)業(yè)服務(wù)。
1、圖片的翻轉(zhuǎn),包括水平翻轉(zhuǎn)以及垂直翻轉(zhuǎn)
2、圖片的任意角度旋轉(zhuǎn)。因?yàn)楣こ绦枰a里面都直接寫(xiě)成了+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()方法來(lái)替代==
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中直接寫(xiě)成了90,可以根據(jù)具體需要?jiǎng)討B(tài)修改,以符合實(shí)際情況
int roteAngle = 0 ;
//縮放比例。默認(rèn)的比例0表示沒(méi)有翻轉(zhuǎn),具體的翻轉(zhuǎn)大小通過(guò)一個(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ì)算步驟,非常好用。
//不過(guò)記得用完了以后,一定要把原點(diǎn)轉(zhuǎn)換回來(lái),要不然其他地方就亂了
g2.translate(drawx,drawy);
if (roteAngle != 0)
g2.rotate(Math.toRadians(m_nFlipXScale * m_nFlipYScale * roteAngle),zoomw >> 1,zoomh >> 1);
//上面的m_nFlipXScale * m_nFlipYScale需要特殊說(shuō)明一下:因?yàn)閷?shí)際使用中,可能遇到各種組合的情況,比如
//先f(wàn)lipX或者flipY以后然后再旋轉(zhuǎn),這時(shí)候,圖片的旋轉(zhuǎn)方向就會(huì)出現(xiàn)錯(cuò)誤,加上這段代碼可以保證無(wú)論使用哪種組合
//操作方式,都保證在旋轉(zhuǎn)圖片的時(shí)候是按照順時(shí)針的方向進(jìn)行旋轉(zhuǎn)。
if (m_nFlipXScale == -1)
g2.scale(-1,1);//第一個(gè)值表示水平,-1表示等寬水平翻轉(zhuǎn),Math.abs(m_nFlipXScale)的值越大,出來(lái)的圖片就越寬
if (m_nFlipYScale == -1)
g2.scale(1,-1);//第二個(gè)值表示垂直,-1表示等高垂直翻轉(zhuǎn),Math.abs(m_nFlipYScale)的值越大,出來(lái)的圖片就越高
//顯示圖片
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í)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
本文標(biāo)題:使用Java怎么實(shí)現(xiàn)一個(gè)圖片翻轉(zhuǎn)功能
當(dāng)前鏈接:http://chinadenli.net/article18/ggjjgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、微信小程序、移動(dòng)網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、定制開(kāi)發(fā)、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
網(wǎng)頁(yè)設(shè)計(jì)公司知識(shí)