如何在Java中旋转屏幕上的组件? import javax.swing.*; 导入java.awt.*; 公共类JFrameAnimationTest扩展了JFrame{ 公共静态void main(字符串[]args)引发异常{ AnimationPanel animation=新建AnimationPanel(); JFrameAnimationTest frame=新的JFrameAnimationTest(); 框架。设置尺寸(600480); frame.setLocationRelativeTo(空); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 帧。添加(动画); frame.setVisible(true); 对于(int i=0;i
总之,这是我的密码。它可能看起来有点混乱,因为我还不习惯stackoverflow,所以我道歉如何在Java中旋转屏幕上的组件? import javax.swing.*; 导入java.awt.*; 公共类JFrameAnimationTest扩展了JFrame{ 公共静态void main(字符串[]args)引发异常{ AnimationPanel animation=新建AnimationPanel(); JFrameAnimationTest frame=新的JFrameAnimationTest(); 框架。设置尺寸(600480); frame.setLocationRelativeTo(空); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 帧。添加(动画); frame.setVisible(true); 对于(int i=0;i,java,swing,graphics2d,paintcomponent,Java,Swing,Graphics2d,Paintcomponent,总之,这是我的密码。它可能看起来有点混乱,因为我还不习惯stackoverflow,所以我道歉 我的问题是:这个程序使这个小矩形慢慢向右移动;在这段时间内,如何将旋转添加到矩形运动中?注意:我实际上还没有编译这段代码,但您已经了解了要点 import javax.swing.*; import java.awt.*; public class JFrameAnimationTest extends JFrame { public static void main(String[
我的问题是:这个程序使这个小矩形慢慢向右移动;在这段时间内,如何将旋转添加到矩形运动中?注意:我实际上还没有编译这段代码,但您已经了解了要点
import javax.swing.*;
import java.awt.*;
public class JFrameAnimationTest extends JFrame {
public static void main(String[] args) throws Exception{
AnimationPanel animation = new AnimationPanel();
JFrameAnimationTest frame = new JFrameAnimationTest();
frame.setSize(600, 480);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(animation);
frame.setVisible(true);
for(int i = 0; i < 100; i++) {
animation.incX(1);
//animation.incY(1);
animation.repaint();
Thread.sleep(10);
}
}
}
class AnimationPanel extends JPanel {
int x = 10;
int y = 10;
public AnimationPanel() {
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.drawRect(x, y, 20, 20);
g.fillRect(x, y, 20, 20);
}
protected void incX(int X) {
x += X;
}
protected void incY(int Y) {
y += Y;
}
}
还要注意,当您修改
x
、y
和rotation
以及调用repaint()
时,实际上应该使用类似于javax.swing.Timer的东西。这样,所有这些都发生在事件调度线程上。@EW添加了一些注释。。。我希望他们能帮忙。请记住,使用AffineTransform时,操作基本上是以相反的顺序执行的(如图所示创建时)。当调用rotate方法时,我有两个问题:1)“rotation”从何而来?它是常数还是什么?2) 旋转对象的角度是多少?@EW旋转将在动画面板
中保持-就像您在维护x
和y
一样。我只是为你需要的变量使用了一个示例名称。好的,谢谢!我打算读一些关于仿射变换的书,但这确实帮了我很大的忙。大人物@我很高兴这有帮助。我也没有注意到您还需要在事件调度线程上创建和初始化面板和框架。只需用SwingUtilities.invokeLater(new Runnable(){public void run(){/*您的代码*/}}})
包围您的代码即可。总的来说,阅读这篇文章会很好:另请参见本文。
public void paintComponent( Graphics g )
{
super.paintComponent( g );
Graphics2D g2d = (Graphics2D) g;
// The 20x20 rectangle that you want to draw
Rectangle2D rect = new Rectangle2D.Double( 0, 0, 20, 20 );
// This transform is used to modify the rectangle (an affine
// transform is a way to do operations like translations, rotations,
// scalings, etc...)
AffineTransform transform = new AffineTransform();
// 3rd operation performed: translate the rectangle to the desired
// x and y position
transform.translate( x + 10, y + 10 );
// 2nd operation performed: rotate the rectangle around the origin
transform.rotate( rotation );
// 1st operation performed: translate the rectangle such that it is
// centered on the origin
transform.translate( -10, -10 );
// Apply the affine transform
Shape s = transform.createTransformedShape( rect );
// Fill the shape with the current paint
g2d.fill( s );
// Stroke the edge of the shape with the current paint
g2d.draw( s );
}