導航:首頁 > 編程語言 > 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畫月亮上升相關的資料

熱點內容
微信小程序按鈕顏色 瀏覽:69
長江大學網課用什麼app 瀏覽:431
華中系統圖紙編程哪個刀好 瀏覽:38
地方債務數據在哪裡查看 瀏覽:932
掃描文件怎麼設置格式 瀏覽:957
蘋果郵箱主機名填什麼 瀏覽:630
多張圖片同一個文件夾 瀏覽:798
win7怎麼打開shs文件 瀏覽:481
怎麼把文件夾做成iso 瀏覽:164
繽客網站上的房價怎麼在哪裡修改 瀏覽:406
單片機c51計數器實驗代碼 瀏覽:990
宏編程滑鼠代表什麼意思 瀏覽:753
別人撿到蘋果6有用嗎 瀏覽:829
word文件用wps打開 瀏覽:477
macbook修改文件格式軟體 瀏覽:757
美版s7edge那個版本好 瀏覽:529
視頻隱藏在文件夾里 瀏覽:144
網路通訊基礎是什麼 瀏覽:209
辦公電腦文件管理 瀏覽:222
火化費報銷文件有哪些 瀏覽:998

友情鏈接