Java编写月亮程序
来源:百度知道 编辑:UC知道 时间:2024/09/21 08:13:48
给你写了个简单的测试,基本能实现你所描述的
package com.bobo.thread;
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame implements Runnable {
static int i = 10;
static int j = 440;
public Test() {
this.setSize(500, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, 500, 500);
g.setColor(Color.white);
g.fillOval(i, j, 60, 60);
g.setColor(Color.BLACK);
g.fillOval(i - 20, j - 20, 60, 60);
}
public void run() {
while (true) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (i >= 155) {
i += 5;
j += 15;
}
if (i < 155) {
i += 5;
j -= 15;
}