旋转 Java Graphics2D 矩形?

发布于 2024-12-06 03:45:37 字数 2103 浏览 0 评论 0原文

我到处搜索,但找不到答案。
如何在java中旋转矩形?

这是我的一些代码:

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){
        }
    }
}

我尝试了 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();
    }
}

I have searched everywhere and I just cant find the answer.
How do I rotate a Rectangle in java?

Here is some of my code:

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){
        }
    }
}

I tried g2d.rotate(100D); but it didnt work.
Thanks in advance.

Here's my edited code:

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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秋叶绚丽 2024-12-13 03:45:37

对于图像,您必须使用 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);
}

顺便说一句,您应该重写 PaintComponent 方法而不是 Paint。

引用 JComponent< /a> 的 API:

由 Swing 调用来绘制组件。应用程序不应调用
直接绘制,但应该使用 repaint 方法来调度
用于重绘的组件。

这个方法实际上将绘画的工作委托给了三个
受保护的方法:paintComponent、paintBorder 和paintChildren。
他们按照列出的顺序被调用,以确保孩子们出现在
组件本身的顶部。一般来说,组件及其
儿童不应在分配给边框的插图区域中绘画。
子类可以像往常一样重写此方法。一个子类
只是想专门化 UI(外观和感觉)委托的绘制
方法应该只重写paintComponent。

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

For images you have to use drawImage method of Graphics2D with the relative AffineTransform.

For shape you can rotate Graphics2D itself:

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);
}

And btw, you should override paintComponent method instead of paint.

Citing JComponent's API:

Invoked by Swing to draw components. Applications should not invoke
paint directly, but should instead use the repaint method to schedule
the component for redrawing.

This method actually delegates the work of painting to three
protected methods: paintComponent, paintBorder, and paintChildren.
They're called in the order listed to ensure that children appear on
top of component itself. Generally speaking, the component and its
children should not paint in the insets area allocated to the border.
Subclasses can just override this method, as always. A subclass that
just wants to specialize the UI (look and feel) delegate's paint
method should just override paintComponent.

Remember also than when you perform an affine transformation, like a rotation, the object is implicitly rotated around the axis origin. So if your intent is to rotate it around an arbitrary point, you should before translating it back to the origin, rotate it, and then re-traslating it to the desired point.

暖阳 2024-12-13 03:45:37
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
}

Graphics.create()Graphics.dispose() 允许您保存当前的变换参数(以及当前的字体、描边、绘画等),并恢复他们稍后。它相当于 OpenGL 中的 glPushMatrix()glPopMatrix()

绘制矩形后,您还可以应用反向旋转,将变换矩阵恢复到其初始状态。然而,减法过程中的浮点近似可能会导致错误的结果。

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
}

Graphics.create() and Graphics.dispose() allow you to save the current transformation parameters (as well as current font, stroke, paint, etc), and to restore them later. It is the equivalent of glPushMatrix() and glPopMatrix() in OpenGL.

You can also apply an inverse rotation once you drew the rectangle to revert the transformation matrix back to its initial state. However, floating point approximations during substractions may lead to a false result.

澉约 2024-12-13 03:45:37

另一种方法是使用 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);

Another way is by using Path2D, with it you can rotate the path only and not the entire graphics object:

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);
柠檬心 2024-12-13 03:45:37

g2d.rotate 的唯一问题是它不会围绕特定点旋转。它主要会弄乱您想要图像的位置,然后迫使您移动图像的 x 和 y 坐标。我不会使用它,特别是用于游戏。你应该研究的是在java中旋转一个点。

The only problem with g2d.rotate is that it doesn't rotate it around a specific point. It will mostly mess up where you want your Image and then force you to move the x and y coordinates of the image. I would not use it,expecially for a game. What you should look into is rotating a point in java.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文