gpt4 book ai didi

java - 旋转 Java Graphics2D 矩形?

转载 作者:太空狗 更新时间:2023-10-29 22:51:34 26 4
gpt4 key购买 nike

我到处找,就是找不到答案。
如何在 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();
}
}

最佳答案

对于图像,您必须使用 drawImage Graphics2D 的相关方法 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的 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.

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

关于java - 旋转 Java Graphics2D 矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7517688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com