Java 图片的旋转

Java 图片的旋转_第1张图片

 

package expression;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;

import javax.naming.InitialContext;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

import org.omg.CORBA.PUBLIC_MEMBER;

public class Text extends JFrame{
	private  int num = 0;
	public  Text(){
	init();//初始化方法
	}
	private void init(){
		setBounds(200, 200, 500, 500);
		setVisible(true);
		setTitle("图片旋转");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		mycanvas canvas = new mycanvas();
		add(canvas);
		canvas.addMouseListener(new MouseListener() {//添加鼠标监听 点击变成动态
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
			
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
			
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				num++;
				canvas.repaint();//不要忘了重绘
			}
		});
		
	}
	public static void main(String[] args) {
		new Text().setVisible(true);
	}
	
	class mycanvas extends Canvas{//内部类
		
	 public void paint(Graphics g){
		 Graphics2D g2 = (Graphics2D) g;
		 Image image = new ImageIcon("src/ycy.jpg").getImage();//获取图片资源
		 g2.rotate(Math.toRadians(num));//参数:弧度  Math.toRandians角度转弧度
		 //旋转图片 Graphics2D 独有方法
		 g2.drawImage(image, 100, 100, this);//绘制图像
		
	}


 }
}

 

你可能感兴趣的:(java)