SRainDrop.java:
codice:
package rain;
import java.awt.Graphics;
/*
public class SRainDrop {
int x, y, yStop;
int width = 1;
int height = 1;
int velocity;
private static int DELTA = 5;
boolean done = false;
public SRainDrop(int x, int y, int velocity, int yStop){
this.x = x;
this.y = y;
this.velocity = velocity;
this.yStop = yStop;
}
public void paint(Graphics g){
if(y < yStop){
g.drawLine(x, y, x, y+DELTA);
y = y+DELTA+velocity;
}
else{
g.drawOval(x, y, width, height);
width = width+1;
if(width%5 == 0){
height = height+1;
}
done = width>20? true:false;
}
}
}
SRainPanel.java:
codice:
package rain;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
*/
public class SRainPanel extends JPanel{
private static int DEFAULT_WIDTH = 500;
private static int DEFAULT_HEIGHT = 500;
private static int NUM_DROPS = 150;
private SRainDrop[] rainDrops = new SRainDrop[NUM_DROPS];
public SRainPanel(){
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()*50);
int randomV = (int) (Math.random()*10);
int stop = (int) (Math.random()*20) + 465;
rainDrops[index] = new SRainDrop(randomX, randomY, randomV, stop);
}
private void createPanel(){
this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
this.setBackground(Color.BLACK);
}
public void paint(Graphics g){
for(int i = 0; i < rainDrops.length; ++i){
if(rainDrops[i].done){
createRainDrop(i);
}
rainDrops[i].paint(g);
}
}
}
SRainFrame.java
codice:
package rain;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;/*
*/
public class SRainFrame extends JFrame implements ActionListener{
private static int DEFAULT_WIDTH = 500;
private static int DEFAULT_HEIGHT = 500;
private SRainPanel panel = new SRainPanel();
private Timer timer = new Timer(100, null);
public SRainFrame(){
super("Soothing Rain");
createFrame();
timer.addActionListener(this);
}
private void createFrame(){
this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBackground(Color.BLACK);
this.setContentPane(panel);
this.setResizable(false);
this.setVisible(true);
}
public void start(){
timer.start();
}
public void paint(Graphics g){
g.clearRect(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT);
g.setColor(Color.white);
panel.paint(g);
}
public void actionPerformed(ActionEvent e){
repaint();
}
public static void main(String[] args){
SRainFrame frf = new SRainFrame();
frf.start();
}
}
scusa ma sono nuovo non sapevo come fare...