Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
527 views
in Technique[技术] by (71.8m points)

swing - Rotate a Java Graphics2D Rectangle?

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();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...