rainpanel.java
来自「一个动态的下雨程序 是用java 写的」· Java 代码 · 共 60 行
JAVA
60 行
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
/*
* Created on Jul 3, 2003
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
/**
* @author Fabian Jones AD Summer Intern 2003
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class RainPanel extends JPanel{
private static int DEFAULT_WIDTH = 500;
private static int DEFAULT_HEIGHT = 500;
private static int NUM_DROPS = 500;
private RainDrop[] rainDrops = new RainDrop[500];
public RainPanel(){
createRainDrops();
createPanel();
}
private void createRainDrops(){
for(int i = 0; i < rainDrops.length; ++i){
createRainDrop(i);
}
}
private void createRainDrop(int index){
int randomX = (int) (Math.random()*500);
int randomY = (int) (Math.random()*300);
int randomV = (int) (Math.random()*10);
rainDrops[index] = new RainDrop(randomX, randomY, randomV);
}
private void createPanel(){
this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
this.setBackground(Color.WHITE);
}
public void paint(Graphics g){
for(int i = 0; i < rainDrops.length; ++i){
rainDrops[i].paint(g);
if(rainDrops[i].x > DEFAULT_WIDTH || rainDrops[i].y > DEFAULT_HEIGHT){
createRainDrop(i);
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?