Java - 随机放置对象

时间:2017-03-08 17:59:01

标签: java swing object

我在雨模拟器上工作。我的想法是每一滴都应该是一个对象,但由于某种原因它们不会出现在JFrame上。如果我改变它们出现的矩形的坐标值,但是如果我随机化数字则不会。是什么导致物体不出现?

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class Rain extends RainDrop implements ActionListener{

    //Change the following 2 lines of variables to change the graphics
    static int width = 1000, height = 600;
    int dropAmount = 650, speed = 10;

    Timer tm = new Timer(speed, this);
    RainDrop[] RainDrop = new RainDrop[650];

    public Rain(){
        for(int i = 0; RainDrop.length > i; i++){
            RainDrop[i] = new RainDrop();
            RainDrop[i].generateRain();
            add(RainDrop[i]);
        }
        repaint();
    }

    public void paintComponent(Graphics g){
        tm.start();
    }

    public void actionPerformed(ActionEvent e){
        repaint();
    }

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

    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;

public class RainDrop extends JPanel{

//Drop [0] is used to store x values
//Drop [1] is used to store y values
//Drop [2] is used to store height values
//Drop [3] is used to store width values
//Drop [4] is used to store velocity values

private int[] Drop = new int [5];
private int width = 1000, height = 600;

public RainDrop(){
}

public int[] generateRain(){
        Drop [0] = (int) (Math.random() * width);       
        Drop [1] = (int) (Math.random() * height);
        Drop [2] = (int) (Math.random() * 13);
        Drop [3] = (int) (Math.random() * 4);
        Drop [4] = (int) (Math.random() * 6);

        if(Drop [2] < 5) Drop [2] += 9;

        if(Drop [3] < 3) Drop [3] += 3;

        if(Drop [3] == 5) Drop [3] = 4;

        if(Drop [4] < 3) Drop [4] += 3;

    return Drop;
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.red);

    g.fillRect(Drop [0], Drop [1], 5, 20);
}
}    

2 个答案:

答案 0 :(得分:2)

基本设计错了。 Rain不应延长RainDrop

Rain应该只是一个带有paintComponent()方法的JPanel,它绘制一个RainDrop对象。

创建ArrayList以保留RainDrop个对象。然后,您可以在Rain面板中启动计时器。当Timer触发时,您可以遍历ArrayList中的所有RainDrop个对象并更改每个RainDrop的位置,然后重新绘制()面板。

以下是显示此基本方法的示例。它展示了如何在面板周围移动球:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;

public class BallAnimation4
{
    private static void createAndShowUI()
    {
        BallPanel panel = new BallPanel();

        JFrame frame = new JFrame("BallAnimation4");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( panel );
        frame.setSize(800, 600);
        frame.setLocationRelativeTo( null );
        //frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setVisible( true );

        panel.addBalls(5);
        panel.startAnimation();
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

class BallPanel extends JPanel implements ActionListener
{
    private ArrayList<Ball> balls = new ArrayList<Ball>();

    public BallPanel()
    {
        setLayout( null );
        setBackground( Color.BLACK );
    }

    public void addBalls(int ballCount)
    {
        Random random = new Random();

        for (int i = 0; i < ballCount; i++)
        {
            Ball ball = new Ball();
            ball.setRandomColor(true);
            ball.setLocation(random.nextInt(getWidth()), random.nextInt(getHeight()));
            ball.setMoveRate(32, 32, 1, 1, true);
//          ball.setMoveRate(16, 16, 1, 1, true);
            ball.setSize(32, 32);
            balls.add( ball );
        }
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        for (Ball ball: balls)
        {
            ball.draw(g);
        }
    }

    public void startAnimation()
    {
        Timer timer = new Timer(75, this);
        timer.start();
    }

    public void actionPerformed(ActionEvent e)
    {
        move();
        repaint();
    }

    private void move()
    {
        for (Ball ball : balls)
        {
            ball.move(this);
        }
    }


    class Ball
    {
        public Color color = Color.BLACK;

        public int x = 0;
        public int y = 0;
        public int width  = 1;
        public int height = 1;

        private int moveX = 1;
        private int moveY = 1;
        private int directionX = 1;
        private int directionY = 1;
        private int xScale = moveX;
        private int yScale = moveY;

        private boolean randomMove = false;
        private boolean randomColor = false;
        private Random myRand = null;

        public Ball()
        {
            myRand = new Random();
            setRandomColor(randomColor);
        }

        public void move(JPanel parent)
        {
            int iRight = parent.getSize().width;
            int iBottom = parent.getSize().height;

            x += 5 + (xScale * directionX);
            y += 5 + (yScale * directionY);

            if (x <= 0)
            {
                x = 0;
                directionX *= (-1);
                xScale = randomMove ? myRand.nextInt(moveX) : moveX;
                if (randomColor) setRandomColor(randomColor);
            }

            if (x >= iRight - width)
            {
                x = iRight - width;
                directionX *= (-1);
                xScale = randomMove ? myRand.nextInt(moveX) : moveX;
                if (randomColor) setRandomColor(randomColor);
            }

            if (y <= 0)
            {
                y = 0;
                directionY *= (-1);
                yScale = randomMove ? myRand.nextInt(moveY) : moveY;
                if (randomColor) setRandomColor(randomColor);
            }

            if (y >= iBottom - height)
            {
                y = iBottom - height;
                directionY *= (-1);
                yScale = randomMove ? myRand.nextInt(moveY) : moveY;
                if (randomColor) setRandomColor(randomColor);
            }
        }

        public void draw(Graphics g)
        {
            g.setColor(color);
            g.fillOval(x, y, width, height);
        }

        public void setColor(Color c)
        {
            color = c;
        }

        public void setLocation(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        public void setMoveRate(int xMove, int yMove, int xDir, int yDir, boolean randMove)
        {
            this.moveX = xMove;
            this.moveY = yMove;
            directionX  = xDir;
            directionY  = yDir;
            randomMove  = randMove;
        }

        public void setRandomColor(boolean randomColor)
        {
            this.randomColor = randomColor;

            switch (myRand.nextInt(3))
            {
                case 0:  color = Color.BLUE;
                         break;
                case 1:  color = Color.GREEN;
                         break;
                case 2:  color = Color.RED;
                         break;
                default: color = Color.BLACK;
                         break;
            }
        }

        public void setSize(int width, int height)
        {
            this.width  = width;
            this.height = height;
        }
    }
}

答案 1 :(得分:0)

如果我把randoms拿出来,我无法让你的代码显示任何东西,但我确实看到你实际上并没有画出雨滴。

在Rain.java中:

public void paintComponent(Graphics g) {
    for (int i = 0; RainDrop.length > i; i++) {
        RainDrop[i].generateRain();
        RainDrop[i].paintComponent(g);
    }
}

并将tm.start();放入构造函数中。每次重新绘制时都不需要启动计时器