旋转Java Graphics2D矩形? [英] Rotate a Java Graphics2D Rectangle?

查看:186
本文介绍了旋转Java Graphics2D矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处搜索,我找不到答案。

如何在java中旋转矩形?

I have searched everywhere and I just cant find the answer.
How do I rotate a Rectangle in 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);但它没有用。
提前致谢。

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

这是我编辑过的代码:

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


推荐答案

您必须使用的图像 drawImage 方法的Graphics2D与相对 AffineTransform

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

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

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

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

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

引用 JComponent 的API:


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

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.

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

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.

这篇关于旋转Java Graphics2D矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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