我正在尝试编写一个程序来使用 Timer 和 JFrame 和 JComponent 来设计日出动画。Graphics2D 的对象是要在 JFrame 上移动的太阳。我的问题是我不确定在哪里放置 Timer 并移动 Graphicc2D!到目前为止,这是我在 JFrame 中放置图像然后将太阳放置在该图像上所做的工作。请告诉我在哪里可以设法移动太阳。我应该在哪里定义 Timer 类?在 JFrame 或 JComponent 或主类中?
public class Main(){
public static void main(String[] args){
myFrame frame = new myFrame();
}
}
class myFrame extends JFrame
{
public myFrame()
{
Draw component = new Draw();
add(component);
}
}
class Draw extends JComponent
{
public Draw()
{
//Read the image here
//set the newImage
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(newImage, 0, 0, null);
g2.fill(new Ellipse2D.Double(x,y,20,20));
//For the sunrise I need to change x,y during the Timer class!!
}
}