旋转 Java Graphics2D 矩形?
我到处搜索,但找不到答案。
如何在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于图像,您必须使用 drawImage 相对于 AffineTransform。
对于形状,您可以旋转 Graphics2D 本身:
顺便说一句,您应该重写 PaintComponent 方法而不是 Paint。
引用 JComponent< /a> 的 API:
还要记住,当您执行仿射变换(如旋转)时,对象会隐式围绕轴原点旋转。因此,如果您的目的是围绕任意点旋转它,您应该在将其平移回原点之前旋转它,然后将其重新平移到所需的点。
For images you have to use drawImage method of Graphics2D with the relative AffineTransform.
For shape you can rotate Graphics2D itself:
And btw, you should override paintComponent method instead of paint.
Citing JComponent's API:
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.
Graphics.create()
和Graphics.dispose()
允许您保存当前的变换参数(以及当前的字体、描边、绘画等),并恢复他们稍后。它相当于 OpenGL 中的glPushMatrix()
和glPopMatrix()
。绘制矩形后,您还可以应用反向旋转,将变换矩阵恢复到其初始状态。然而,减法过程中的浮点近似可能会导致错误的结果。
Graphics.create()
andGraphics.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 ofglPushMatrix()
andglPopMatrix()
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.
另一种方法是使用 Path2D,使用它您可以仅旋转路径而不是整个图形对象:
Another way is by using
Path2D
, with it you can rotate the path only and not the entire graphics object: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.