導航:首頁 > 編程語言 > java畫月亮上升

java畫月亮上升

發布時間:2023-03-20 08:28:26

1. java繪圖

要移動就要Thread

你要實現一個移動的話就要用到線程,移動實際上就是每隔一段時間改變一個圖形在畫板上開始的坐標,然後再重畫畫板,用一個線程可以使用繼承Thread 或者實現Runnable介面
例如下面這個是個移動的月亮

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.setResizable(false);
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.gray);
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;
}
if (i >= 305) {
i = 10;
j = 440;
}
System.out.println(i + " " + j);
this.repaint();
}
}

public static void main(String args[]) {
new Thread(new Test()).start();
}
}

閱讀全文

與java畫月亮上升相關的資料

熱點內容
專題學習網站源碼 瀏覽:163
jsphead什麼 瀏覽:88
gps串口數據怎麼發送 瀏覽:968
win10文件主頁共享查看 瀏覽:411
中國聯通有哪些app是免流的 瀏覽:176
邊做邊保存的文件找不到了 瀏覽:858
win10照片應用文件夾名稱 瀏覽:966
編程如何解決資金的原子性 瀏覽:638
如何製作廣角鏡頭矯正文件 瀏覽:513
在網頁開發中應該選用哪個資料庫 瀏覽:742
iphone5移動卡貼 瀏覽:990
電腦文件的格式 瀏覽:127
extjs的xtype 瀏覽:959
suse11iso文件要u盤安裝 瀏覽:153
如何將報表統計數據轉化為圖形 瀏覽:444
如何寄快遞材料文件 瀏覽:265
java構造方法private 瀏覽:475
手機文件找回恢復 瀏覽:516
word怎麼把u盤里的文件拔掉 瀏覽:976
港版蘋果用的插排 瀏覽:1000

友情鏈接