图片正(逆)旋转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文件夹下



嘿嘿,各位写代码也有点累了吧,特别是深夜的时候是不是觉得应该有点吃的就更好了?可以到本人开


的小店“小灰灰休闲美食”店买点休闲食品哦,既满足食欲又可以放松哈子,当然平时也要吃是吧,也


可以买来送朋友哦!

 

你可能感兴趣的:(java小程序)