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

熱點內容
蘋果6p跳屏是什麼原因 瀏覽:383
下載文件路徑是什麼 瀏覽:852
linux下o文件多重定義 瀏覽:135
為什麼在人多的地方沒有網路 瀏覽:170
華為g7有多少個版本 瀏覽:949
實名寶app哪個好 瀏覽:1
微雲單個文件可以傳多少 瀏覽:843
計算機連成網路的最重要優勢是 瀏覽:411
優盤打開後文件夾為空 瀏覽:495
實時數據寫入量大如何優化 瀏覽:76
哪裡能學程序編程 瀏覽:647
微信裡面的文件儲存在哪個目錄 瀏覽:745
高仿蘋果5s屏幕顯示清楚嗎 瀏覽:897
若有以下程序void 瀏覽:432
大數據主體有哪些 瀏覽:961
如何學習編程的優點 瀏覽:906
最新版本手機qq 瀏覽:463
簡述在word 瀏覽:528
qq怎麼清楚歷史記錄防止被盜 瀏覽:263
發送手機里的錄音文件在哪裡 瀏覽:866

友情鏈接