gpt4 book ai didi

java - 如何在我的 swing 应用程序中旋转正方形

转载 作者:行者123 更新时间:2023-11-29 05:53:27 32 4
gpt4 key购买 nike

我开发了一个小的 swing 应用程序,我在其中绘制了一个正方形。现在我想使用 Thread 在其中心旋转这个正方形。我遇到的问题是如何在我的 rotateSquare() 中获取对该正方形的引用方法。(实际上,如果可能的话,我需要一种方法来旋转同一个方 block ,而不是删除整个内容 Pane 并在其位置绘制另一个旋转的方 block )。

这是我的代码:

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;


public class Rotation extends JFrame implements Runnable{
Thread t;
Rotation()
{
super("Animation of rotation about center");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);

setContentPane(new Container(){
public void paint(Graphics g)
{
Graphics2D g2=(Graphics2D)g.create();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, getWidth(), getHeight());

g2.setColor(Color.GRAY);
g2.fillRect(100, 100, 100, 100);
}
});

t=new Thread(this,"pr");
t.start();


setVisible(true);
//setOpacity(0.8f);
}
public void run()
{
try{
for(;;)
{
Thread.sleep(100);
SwingUtilities.invokeLater(new Runnable(){public void run(){
rotateSquare();
}});
}
}catch(InterruptedException e){System.out.println("Thread interrupted");}
}
public void rotateSquare();
{

}
public static void main(String args[])
{
//setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable(){public void run(){new Rotation();}});
}

}

最佳答案

关于java - 如何在我的 swing 应用程序中旋转正方形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13048701/

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