求一个java 用JFrame做的 下流星雨的小案例
在线等
多谢,
------解决思路----------------------
flash效果的么?
------解决思路----------------------
这个不难的吧 循环改变位置 判断到底了.重新开始....
会点spring 能写的.
------解决思路----------------------
手动画吧。用Griphcs2D定时重新画线。
------解决思路----------------------
Java 2D Graphics + 你的艺术创意
------解决思路----------------------
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
public class DrawSquare3 extends JFrame implements Runnable {
Canvas canvas = new Canvas();
public DrawSquare3() {
this.setSize(800, 800);
canvas.setSize(this.getSize().width, this.getSize().height);
this.getContentPane().add(canvas, BorderLayout.CENTER);
this.pack();
canvas.setBackground(Color.BLACK);
this.setVisible(true);
}
ArrayList<Rain> rainlist = new ArrayList<DrawSquare3.Rain>();
Random random = new Random();
public void paint(){
Image image = createImage(this.getSize().width, this.getSize().height);
Graphics g = image.getGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, this.getSize().width, this.getSize().height);
for(int i = 0; i < 10; i++){
rainlist.add(new Rain(getSize().width/4+random.nextInt()%(getSize().width+getSize().width/4)));
}
for(int i = 0; i < rainlist.size(); ){
rainlist.get(i).paint(g);
if(rainlist.get(i).y > this.getSize().height){
rainlist.remove(i);
} else {
i++;
}
}
canvas.getGraphics().drawImage(image, 0, 0, null);
}
class Rain {
public int x, y;
public Rain(int x){
this.x = x;
this.y = 0;
}
public void paint(Graphics g){
g.setColor(Color.yellow);
g.drawLine(x+20, y-50, x, y);
x-=5;
y+=20;
}
}
public void run(){
while(true){
try {
paint();
Thread.sleep(30);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
(new Thread(new DrawSquare3())).start();
}
}
雨的效果改Rain的paint
背景的效果改DrawSquare3的paint