粒子不渲染

时间:2016-03-23 21:32:30

标签: java render particles

我放入了粒子系统,但是当我运行程序时,当我产生一些粒子时,它们不会渲染。我查看了ArrayList,即使我添加了一个粒子,它的值也总是为0。 下面是主类的代码:

package Main;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

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

import me.mango.rendering.Particle;

//do double buffering

public class Game extends JPanel {

    private static final long serialVersionUID = 1L;

    public static final int height = 400;
    public static final int width = height * 16 / 9;

    JPanel p;
    Game game;
    Graphics g;

    JFrame frame;
    KeyListener kl;
    MouseListener ml;

    public boolean running = true;

    private ArrayList<Particle> particles = new ArrayList<Particle>(500);

    public Game(){
        kl = new KeyListener(){

            public void keyPressed(KeyEvent e) {

            }

            public void keyReleased(KeyEvent e) {

            }

            public void keyTyped(KeyEvent e) {

            }
        };

        ml = new MouseListener(){

            public void mousePressed(MouseEvent e) {
                addParticle(true);addParticle(false);addParticle(true);
                addParticle(false);addParticle(true);addParticle(false);
            }

            public void mouseReleased(MouseEvent e) {

            }
            public void mouseClicked(MouseEvent e) {

            }

            public void mouseEntered(MouseEvent e) {

            }

            public void mouseExited(MouseEvent e) {

            }
        };
    }

    public void addParticle(boolean b){
        int dx,dy;
        int x = 100;
        int y = 100;
        if(b){
            dx = (int) (Math.random()*5);
            dy = (int) (Math.random()*5);
        }else{
            dx = (int) (Math.random()*-5);
            dy = (int) (Math.random()*-5);
        }
        int size = (int) (Math.random()*12);
        int life = (int) Math.random()*(120)+380;
        particles.add(new Particle(x,y,dx,dy,size,life,Color.blue));
    }

    public void update(double delta){
        for(int i = 0; i<= particles.size() - 1;i++){
            if(particles.get(i).update()) particles.remove(i);
        }
        System.out.println(particles.size());
    }

    @Override
    public void paint(Graphics g){
        g.clearRect(0, 0, getWidth(), getHeight());

        //render here
        renderParticles(g);

        g.dispose();
    }

    public void renderParticles(Graphics g){
        for(int i =0;i <= particles.size() - 1;i++){
            particles.get(i).render(g);
            System.out.println("spawned");
        }
    }

    public void run(){

        //initialize time loop variables
        long lastLoopTime = System.nanoTime();
        final int TARGET_FPS = 60;
        final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
        double lastFpsTime = 0;

        //Main game loop
        while(running)
        {
            //Calculate since last update
            long now = System.nanoTime();
            long updateLength = now - lastLoopTime;
            lastLoopTime = now;
            double delta = updateLength / ((double)OPTIMAL_TIME);

            //update frame counter
            lastFpsTime += updateLength;

            //update FPS counter
            if(lastFpsTime >= 1000000000)
            {
                lastFpsTime = 0;
            }

            //game updates
            game.update(delta);

            //graphics (gameState)
            game.repaint();

            try{
                Thread.sleep((Math.abs(lastLoopTime - System.nanoTime() + OPTIMAL_TIME)/1000000));
            }catch(Exception e){
                System.out.println("Error in sleep");
            }
        }
    }

    public void start(){
        frame = new JFrame("Game");

        game = new Game();

        frame.add(game);
        frame.pack();

        frame.setSize(width, height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        frame.addKeyListener(kl);
        frame.addMouseListener(ml);

        frame.setVisible(true);

        run();
    }

    public static void main(String[] args){
        new Game().start();
    }
}

和粒子类:

package me.mango.rendering;

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

public class Particle {

    private int x;
    private int y;
    private int dx;
    private int dy;
    private int size;
    private int life;
    private Color color;

    public Particle(int x, int y, int dx, int dy, int size, int life, Color c){
        this.x = x;
        this.y = y;
        this.dx = dx;
        this.dy = dy;
        this.size = size;
        this.life = life;
        this.color = c;
    }

    public boolean update(){
        x += dx;
        y += dy;
        life--;
        if(life <= 0){
            return true;
        }
        return false;
    }

    public void render(Graphics g){
        g.setColor(color);
        g.fillRect(x-(size/2), y-(size/2), size, size);

        g.dispose();
    }

}

谢谢!

1 个答案:

答案 0 :(得分:0)

你在课堂游戏中有一个叫做游戏的东西:根本就不是好设计。显然你不明白创建一个对象的意义。

在main()中你创建了一个对象游戏:这应该足够了。那件事你必须操纵。

因此,在类游戏中调用game.something()是一个卷积。摆脱它。

game = new Game();

Game game;

这些事情必须要去。

以及对game.someMethod()的任何引用

如果你在游戏中,

应该只用someMethod()替换。

另外你有run()和start()等东西:你觉得你在创建一些线程吗?只为你的方法使用这些名称?

没有