如何在Java中旋转屏幕上的组件? [英] How can I rotate an onscreen component in Java?

查看:81
本文介绍了如何在Java中旋转屏幕上的组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 import javax.swing.*;
 import java.awt.*;

 public class JFrameAnimationTest extends JFrame {
     public static void main(String[] args) throws Exception{
        AnimationPanel animation = new AnimationPanel();
        JFrameAnimationTest frame = new JFrameAnimationTest();
        frame.setSize(600, 480);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(animation);
        frame.setVisible(true);        
        for(int i = 0; i < 100; i++) {
            animation.incX(1);
            //animation.incY(1);
            animation.repaint();
            Thread.sleep(10);
        }
    }
}

class AnimationPanel extends JPanel {

    int x = 10;
    int y = 10;   

  public AnimationPanel() {        
  }

  @Override
  protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.setColor(Color.BLUE);
      g.drawRect(x, y, 20, 20);
      g.fillRect(x, y, 20, 20);
  }

  protected void incX(int X) {
      x += X;
  }

  protected void incY(int Y) {
      y += Y;
  }
}

所以无论如何,这里都有我的代码.由于我还不习惯于stackoverflow,所以它看起来似乎有些混乱,因此我深表歉意.

So anyways theres my code. It probably looks a bit jumbled as I am not used to stackoverflow just yet so I apologize.

这是我的问题:该程序使这个小矩形缓慢向右移动;在该时间段内如何向矩形运动添加旋转?

Here's my question: This program makes this small rectangle slowly move to the right; how can I add rotation to the rectangles movement during that time period?

推荐答案

注意:我实际上尚未编译此代码,但您能理解.

Note: I haven't actually compiled this code, but you get the gist.

public void paintComponent( Graphics g )
{
    super.paintComponent( g );
    Graphics2D g2d = (Graphics2D) g;

    // The 20x20 rectangle that you want to draw
    Rectangle2D rect = new Rectangle2D.Double( 0, 0, 20, 20 );

    // This transform is used to modify the rectangle (an affine
    // transform is a way to do operations like translations, rotations,
    // scalings, etc...)
    AffineTransform transform = new AffineTransform();

    // 3rd operation performed: translate the rectangle to the desired
    // x and y position
    transform.translate( x + 10, y + 10 );

    // 2nd operation performed: rotate the rectangle around the origin
    transform.rotate( rotation );

    // 1st operation performed: translate the rectangle such that it is
    // centered on the origin
    transform.translate( -10, -10 );

    // Apply the affine transform
    Shape s = transform.createTransformedShape( rect );

    // Fill the shape with the current paint
    g2d.fill( s );

    // Stroke the edge of the shape with the current paint
    g2d.draw( s );
}

还请注意,在修改 x y 旋转,以及调用 repaint()时.这样,所有这些都发生在事件分发线程上.

Also note that you should really be using something like a javax.swing.Timer when you modify x, y, and rotation and when you call repaint(). That way all of it happens on the event dispatch thread.

这篇关于如何在Java中旋转屏幕上的组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆