Java编写月亮程序

来源:百度知道 编辑:UC知道 时间:2024/09/21 08:13:48
画一个月亮,使月亮整体移动,先升起再落下。在JFrame上画就可以。我就是线程那一块搞不明白,类似的向画一面升起的国旗,希望有牛人帮帮忙。我分不多,回答好的我再加分。

给你写了个简单的测试,基本能实现你所描述的

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;
}