旋转Java Graphics2D矩形?

chr*_*hic 15 java swing rotation graphics2d

我到处搜寻,我找不到答案.
如何在java中旋转Rectangle?

这是我的一些代码:

package net.chrypthic.Space;

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

public class Space extends JPanel implements ActionListener{
    Timer time;
    public Space()
    {
        setVisible(true);
        setFocusable(true);
        addMouseMotionListener(new ML());
        addMouseListener(new ML());
        addKeyListener(new AL());
        time=new Timer(5, this);
        time.start();
    }
    public void paint(Graphics g)
    {
        super.paint(g);
        Graphics2D g2d = (Graphics2D)g;
        g2d.setColor(Color.WHITE);
        Rectangle rect2 = new Rectangle(100, 100, 20, 20);

        g2d.draw(rect2);
        g2d.fill(rect2);
    }
    public void actionPerformed(ActionEvent ae) {
        repaint();
    }
    public class AL extends KeyAdapter
    {
        public void keyPressed(KeyEvent e) {
        }

        public void keyReleased(KeyEvent e) {
        }
    }
    public class ML extends MouseAdapter
    {
        public void mouseMoved(MouseEvent e) {
        }

        public void mousePressed(MouseEvent e){
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我试过g2d.rotate(100D); 但它没有用.提前致谢.

这是我编辑的代码:

package net.chrypthic.Space;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Space extends JPanel implements ActionListener{
    Timer time;
    public Space()
    {
        setVisible(true);
        setFocusable(true);
        setSize(640, 480);
        setBackground(Color.BLACK);
        time=new Timer(5, this);
        time.start();
    }
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        Rectangle rect1 = new Rectangle(100, 100, 20, 20);
        g2d.setColor(Color.WHITE);
        g2d.translate(rect1.x+(rect1.width/2), rect1.y+(rect1.height/2));
        g2d.rotate(Math.toRadians(90));
        g2d.draw(rect1);
        g2d.fill(rect1);
    }
    public void actionPerformed(ActionEvent e) 
    {
        repaint();
    }
}
Run Code Online (Sandbox Code Playgroud)

Hei*_*bug 14

对于图像,您必须使用Graphics2D的drawImage方法和相对的AffineTransform.

对于形状,您可以旋转Graphics2D本身:

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D)g;
    g2d.setColor(Color.WHITE);
    Rectangle rect2 = new Rectangle(100, 100, 20, 20);

    g2d.rotate(Math.toRadians(45));
    g2d.draw(rect2);
    g2d.fill(rect2);
}
Run Code Online (Sandbox Code Playgroud)

顺便说一下,你应该覆盖paintComponent方法而不是paint.

引用JComponent的API:

由Swing调用以绘制组件.应用程序不应直接调用绘制,而应使用重绘方法来调度组件以进行重绘.

此方法实际上将绘制工作委托给三个受保护的方法:paintComponent,paintBorder和paintChildren.它们按列出的顺序调用,以确保子项出现在组件本身之上.一般来说,组件及其子组件不应在分配给边框的insets区域中绘制.子类可以像往常一样覆盖此方法.一个只想专门化UI(外观)委托的paint方法的子类应该只覆盖paintComponent.

还要记住,当您执行仿射变换(如旋转)时,对象会围绕轴原点隐式旋转.因此,如果您的意图是围绕任意点旋转它,您应该在将其转换回原点之前,旋转它,然后将其重新转换到所需的点.

  • “还要记住,当您执行仿射变换(如旋转)时,对象会隐式围绕轴原点旋转。因此,如果您的意图是围绕任意点旋转它,则应该在将其平移回原点之前旋转它,然后将其重新平移到所需的点。” 我是怎么做到的? (2认同)

Aur*_*bon 10

public void draw(Graphics2D g) {
    Graphics2D gg = (Graphics2D) g.create();
    gg.rotate(angle, rect.x + rect.width/2, rect.y + rect.height/2);
    gg.drawRect(rect.x, rect.y, rect.width, rect.height);
    gg.dispose();

    gg = (Graphics2D) g.create();
    ... other stuff
}
Run Code Online (Sandbox Code Playgroud)

Graphics.create()Graphics.dispose()允许您保存当前转换参数(以及当前字体,笔触,绘画等),并在以后恢复它们.它glPushMatrix()glPopMatrix()OpenGL 相当.

绘制矩形后,您还可以应用反向旋转,以将变换矩阵恢复为初始状态.但是,在减法期间的浮点近似可能导致错误的结果.


Dan*_*eón 6

另一种方法是使用Path2D它,你可以只旋转路径而不是整个图形对象:

Rectangle r = new Rectangle(x, y, width, height);
Path2D.Double path = new Path2D.Double();
path.append(r, false);

AffineTransform t = new AffineTransform();
t.rotate(angle);
path.transform(t);
g2.draw(path);
Run Code Online (Sandbox Code Playgroud)