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

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

问题描述

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

这是我的一些代码:

包 net.chrypthic.Space;导入 javax.swing.*;导入 java.awt.*;导入 java.awt.event.*;公共类空间扩展 JPanel 实现 ActionListener{定时器时间;公共空间(){设置可见(真);setFocusable(true);addMouseMotionListener(new ML());addMouseListener(new ML());添加密钥侦听器(新 AL());时间=新定时器(5,这个);时间开始();}公共空心漆(图文g){super.paint(g);Graphics2D g2d = (Graphics2D)g;g2d.setColor(Color.WHITE);矩形rect2 = new Rectangle(100, 100, 20, 20);g2d.draw(rect2);g2d.fill(rect2);}public void actionPerformed(ActionEvent ae) {重绘();}公共类 AL 扩展了 KeyAdapter{public void keyPressed(KeyEvent e) {}public void keyReleased(KeyEvent e) {}}公共类 ML 扩展了 MouseAdapter{public void mouseMoved(MouseEvent e) {}public void mousePressed(MouseEvent e){}}}

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

这是我编辑的代码:

包 net.chrypthic.Space;导入 java.awt.*;导入 java.awt.event.*;导入 javax.swing.*;公共类空间扩展 JPanel 实现 ActionListener{定时器时间;公共空间(){设置可见(真);setFocusable(true);设置大小(640、480);设置背景(颜色.黑色);时间=新定时器(5,这个);时间开始();}public voidpaintComponent(Graphics g){super.paintComponent(g);Graphics2D g2d = (Graphics2D)g;矩形 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){重绘();}}

解决方案

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

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

public voidpaintComponent(Graphics g){super.paintComponent(g);Graphics2D g2d = (Graphics2D)g;g2d.setColor(Color.WHITE);矩形rect2 = new Rectangle(100, 100, 20, 20);g2d.rotate(Math.toRadians(45));g2d.draw(rect2);g2d.fill(rect2);}

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

引用 JComponent 的 API:

<块引用>

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

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

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

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

解决方案

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.

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

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