用Java编程Rain

时间:2018-02-25 19:18:20

标签: java windows

我是一名初中程序员,在高中读java。我不是很好,但我喜欢编码,我想改进。在这段代码中,我只是试图让雨水颗粒下降,但他们不会这样做。我也没有添加我的导入

public class RainDrop
{

    private int x, y;
    private int width, height;
    private int vx;

    public RainDrop()
    {
        //x = (int) (Math.random()*640); 
        //y = (int) (Math.random() *500) - 500;
        x= (int) (Math.random()*640);
        y = 50;
        width = 3;
        height = 25;
        vx = 1;
    }
    public void draw(Graphics g)
    {
        g.setColor(Color.blue);
        g.fillRect(x, y, width, height);
    }
    public void fall()
    {
        y += vx;
        if(y >= 480)
        {
            y = (int) (Math.random() *500) - 500;
            vx = 1;

        }
    }

    public int getY()
    {
        return y;
    }
}



public class Panel extends JPanel implements Runnable

{

    public static final int drops = 1;
    public RainDrop[] d = new RainDrop[drops];
    public Panel()
    {
        for(int i = 0; i < drops; i++)
        {
            d[i] = new RainDrop();
        }
    }

    @Override
    public void run() 
    {
        update();
        repaint();
    }

    public void update()
    {
        for(int i = 0; i < drops; i++)
        {
            d[i].fall();
            repaint();
        }
    }

    public void paint(Graphics g)
    {
        for(int i = 0; i < drops; i++)
        {
            d[i].draw(g);
            repaint();
        }
    }
}



public class Runner extends JFrame
{

    public static void main(String[] args) 
    {

        JFrame obj = new JFrame();
        Panel j = new Panel();
        obj.setSize(640, 480);
        obj.setVisible(true);
        obj.setResizable(false);
        obj.setDefaultCloseOperation(EXIT_ON_CLOSE);
        obj.setTitle("Rain");
        obj.add(j);
        obj.setLocationRelativeTo(null);
    }
}

任何人都可以帮助我

0 个答案:

没有答案