生活随笔
收集整理的這篇文章主要介紹了
满天星_Java实例_源码+图片素材
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
運(yùn)行效果:
源代碼:
package star
;import java
.awt
.Color
;
import java
.awt
.Font
;
import java
.awt
.Graphics
;
import java
.awt
.image
.BufferedImage
;
import java
.io
.IOException
;
import java
.util
.Timer
;
import java
.util
.TimerTask
;import javax
.imageio
.ImageIO
;
import javax
.swing
.JFrame
;
import javax
.swing
.JPanel
;
public class Star2 extends JPanel {int[] x
= new int[300];int[] y
= new int[300];Color
[] color
= new Color[300];Star2() {for (int i
= 0; i
< 300; i
++) {x
[i
] = (int) (Math
.random() * 1024);y
[i
] = (int) (Math
.random() * 500);int R
= (int) (Math
.random() * 255);int G
= (int) (Math
.random() * 255);int B
= (int) (Math
.random() * 255);color
[i
] = new Color(R
, G
, B
);}}public static BufferedImage background
;static {try {background
= ImageIO
.read(Star
.class.getResource("background.jpg"));} catch (IOException e
) {e
.printStackTrace();}}public void paint(Graphics g
) {g
.drawImage(background
, 0, 0, null
);g
.setFont(new Font("微軟雅黑", Font
.BOLD
, 24));for (int i
= 0; i
< 300; i
++) {g
.setColor(color
[i
]);g
.drawString("*", x
[i
], y
[i
]);}}public void move() {Timer timer
= new Timer();timer
.schedule(new TimerTask() {public void run() {for (int i
= 0; i
< 300; i
++) {y
[i
]++;if (y
[i
] > 500) {y
[i
] = 0;}x
[i
]--;if (x
[i
] < 0) {x
[i
] = 1024;}}repaint();}}, 10, 10);}public static void main(String
[] args
) {JFrame frame
= new JFrame();Star2 panel
= new Star2();frame
.add(panel
);frame
.setSize(1024, 500);frame
.setLocationRelativeTo(null
);frame
.setTitle("滿(mǎn)天星");frame
.setDefaultCloseOperation(JFrame
.EXIT_ON_CLOSE
);frame
.setVisible(true);panel
.move();}}
圖片素材:
注:圖片須和代碼在一個(gè)包下
總結(jié)
以上是生活随笔為你收集整理的满天星_Java实例_源码+图片素材的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。