构造函数不执行

时间:2017-01-11 17:43:35

标签: java eclipse paintcomponent

我正在创建一个雨动画但由于某种原因我的构造函数没有执行。为什么这样,我该如何解决?

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Rain extends JPanel{

    int array[] = new int[100];
    int x, y;

    public Rain(){

        for(int i = 0; i < array.length; i++){

            x = (int) (Math.random() * 1001);
            y = (int) (Math.random() * 101);

            while(y < 500){
                y++;
                repaint();
                System.out.print("test");
            }
        }
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        this.setBackground(new Color(255, 155, 155));
        g.setColor(new Color(204, 64, 89));
        repaint();
        g.fillRect(x , y, 5, 10);
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setLocation(100, 100);
        f.setSize(700, 500);
        f.setTitle("Rain");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Rain Raindrop = new Rain();
        f.add(Raindrop);
        f.setResizable(true);
        f.setVisible(true);
    }
}

0 个答案:

没有答案