java 怎么让一个图形绕一个点旋转360度 100
2个回答
展开全部
- import java.awt.Canvas;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.image.BufferedImage;
- /**
- * @author ZhengYesheng
- */
- public class RotateImageCanvas extends Canvas implements Runnable
- {
- private static final long serialVersionUID = -1997487731464495923L;
- BufferedImage img;
- BufferedImage rotatedImg;
- int degress = 0;
- public RotateImageCanvas(BufferedImage img)
- {
- super();
- this.img = img;
- new Thread(this).start();
- }
- @Override
- public void run()
- {
- while (true)
- {
- //A,与B的代码配合决定旋转的速度
- degress += 1;
- degress %= 360;
- repaint();
- try
- {
- if (degress == 0)
- {
- //绕一周后等待的时间在这里设置
- Thread.sleep(3 * 1000);
- }
- else
- {
- //考虑到视觉平滑,这里不应大约40。
- Thread.sleep(30);
- }
- }
- catch (InterruptedException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- @Override
- public void paint(Graphics graphics)
- {
- super.paint(graphics);
- //获取旋转指定角度后的图片。为了避免累计误差,这里是用原始图像旋转的
- rotatedImg = rotateImage(img, degress);
- //绘制旋转后的图片
- graphics.drawImage(rotatedImg, 0, 0, this);
- }
- /**
- * 旋转图片为指定角度。
- * 注意:1、这个方法实现了图像的基于中点的旋转,要想绕指定点,需要配合Matrix类
- * 2、为避免图像被裁切,结果图片的尺寸也需要动态计算
- * 3、现在旋转后有黑色背景,如果不需要这个效果,需要设置结果图片的Alpha模式
- *
- * @param bufferedimage
- * 目标图像
- * @param degree
- * 旋转角度
- * @return
- */
- private BufferedImage rotateImage(BufferedImage bufferedimage, int degree)
- {
- int w = bufferedimage.getWidth();
- int h = bufferedimage.getHeight();
- int type = bufferedimage.getColorModel().getTransparency();
- BufferedImage img;
- Graphics2D graphics2d;
- (graphics2d = (img = new BufferedImage(w, h, type)).createGraphics())
- .setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
- graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
- graphics2d.drawImage(bufferedimage, 0, 0, null);
- graphics2d.dispose();
- return img;
- }
- }
展开全部
import java.awt.Canvas;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.RenderingHints;import java.awt.image.BufferedImage; /** * @author yzw */public class RotateImageCanvas extends Canvas implements Runnable{ private static final long serialVersionUID = -1997487731464495923L; BufferedImage img; BufferedImage rotatedImg; int degress = 0; public RotateImageCanvas(BufferedImage img) { super(); this.img = img; new Thread(this).start(); } @Override public void run() { while (true) { //A,与B的代码配合决定旋转的速度 degress += 1; degress %= 360; repaint(); try { if (degress == 0) { //绕一周后等待的时间在这里设置 Thread.sleep(3 * 1000); } else { //考虑到视觉平滑,这里不应大约40。 Thread.sleep(30); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } @Override public void paint(Graphics graphics) { super.paint(graphics); //获取旋转指定角度后的图片。为了避免累计误差,这里是用原始图像旋转的 rotatedImg = rotateImage(img, degress); //绘制旋转后的图片 graphics.drawImage(rotatedImg, 0, 0, this); } /** * 旋转图片为指定角度。 * 注意:1、这个方法实现了图像的基于中点的旋转,要想绕指定点,需要配合Matrix类 * 2、为避免图像被裁切,结果图片的尺寸也需要动态计算 * 3、现在旋转后有黑色背景,如果不需要这个效果,需要设置结果图片的Alpha模式 * * @param bufferedimage * 目标图像 * @param degree * 旋转角度 * @return */ private BufferedImage rotateImage(BufferedImage bufferedimage, int degree) { int w = bufferedimage.getWidth(); int h = bufferedimage.getHeight(); int type = bufferedimage.getColorModel().getTransparency(); BufferedImage img; Graphics2D graphics2d; (graphics2d = (img = new BufferedImage(w, h, type)).createGraphics()) .setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2); graphics2d.drawImage(bufferedimage, 0, 0, null); graphics2d.dispose(); return img; }}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询