图片正(逆)旋转90度java小程序

import java.awt.BorderLayout; import java.awt.Container; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class RotateImage { public static void main(String[] args){ JFrame frame=new RotateImgFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class RotateImgFrame extends JFrame{ public RotateImgFrame(){ setTitle("RotateImgTest"); setSize(WIDTH,HEIGHT); setAlwaysOnTop(true); final ImgPanel imgPanel=new ImgPanel(); imgPanel.setBounds(0, 0, 400, 400); Container con=getContentPane(); con.add(imgPanel); antiRotateBtn=new JButton("逆转"); rotateBtn=new JButton("正转"); btnPanel=new JPanel(); btnPanel.add(rotateBtn); btnPanel.add(antiRotateBtn); con.add(btnPanel,BorderLayout.SOUTH); antiRotateBtn.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub imgPanel.setRotate(imgPanel.getRotate()-Math.PI/2); imgPanel.repaint(); } }); rotateBtn.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub imgPanel.setRotate(imgPanel.getRotate()+Math.PI/2); imgPanel.repaint(); } }); } public static final int WIDTH=400; public static final int HEIGHT=450; private JPanel btnPanel; private JButton antiRotateBtn; private JButton rotateBtn; } class ImgPanel extends JPanel{ public void paintComponent(Graphics g){ Graphics2D g2=(Graphics2D) g; g.fillRect(0, 0, 400, 400); g2.rotate(rotate, 200, 200); image=new ImageIcon("src/test.jpg").getImage(); int imgW=image.getWidth(this); int imgH=image.getHeight(this); g2.drawImage(image, (400-imgW)/2, (400-imgH)/2,this); g2.dispose(); } public Image getImage(){ return image; } public double getRotate(){ return rotate; } public void setRotate(double rotate){ this.rotate=rotate; } private Image image; private double rotate=0; }

test.jpg图片放在src文件夹下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值