重绘只发生在我的循环结束 - java

时间:2017-11-15 14:13:56

标签: java frame repaint

基本上我正在制作TRON,我的第一步是做一个基本的蛇游戏,我的第一步是在屏幕上做一个方形移动,然后实现关键的听众。我希望看到广场在每个舞台阶段向下移动,但我似乎只是得到最终的油漆图像,所以我认为它确实移动,我只是看不到框架正在更新,任何人都可以帮忙吗?

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


public class Multiplayer extends JPanel { //implements ActionListener {


    public static Multiplayer multiplayer;
    public static ArrayList<Point> bikeParts = new ArrayList<>();
    public static final int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3, SCALE = 10;
    public static int ticks = 0, direction = DOWN;
    public static Point bike;
    public boolean over = false;
    public JFrame frame;
    public renderPanel rp;

    public Multiplayer() {
        frame = new JFrame();
        //JPanel panel = new JPanel();
        rp = new renderPanel();
        //add(panel, BorderLayout.CENTER);
        frame.getContentPane().add(rp, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(900, 700);
        frame.setVisible(true);
        frame.setResizable(false);
        bike = new Point(0, 0);

        for (int i = 0; i < 1000; i++) {
            animate(null);
            rp.repaint();
        }
    }

    //@Override
    public void animate(ActionEvent ef) {
        rp.repaint();
        ticks++;
        System.out.println("yw");
        if (ticks % 10 == 0 && bike != null && over != true) {
            System.out.println("did");
            rp.repaint();
            bikeParts.add(new Point(bike.x, bike.y));
            if (direction == UP) {
                if (bike.y - 1 > 0) {

                    bike = new Point(bike.x, bike.y - 1);
                } else {
                    over = true;
                }
            }
            if (direction == DOWN) {
                if (bike.y + 1 < 700) {
                    bike = new Point(bike.x, bike.y + 1);
                } else {
                    over = true;
                }
            }
            if (direction == LEFT) {
                if (bike.x - 1 > 0) {
                    bike = new Point(bike.x - 1, bike.y);
                } else {
                    over = true;
                }
                bike = bikeParts.get(bikeParts.size() - 1);
                bikeParts.remove(0);
            }
            if (direction == RIGHT) {
                if (bike.x + 1 < 900) {
                    bike = new Point(bike.x + 1, bike.y);
                } else {
                    over = true;
                }
            }
        }
    }
}

然后:

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

public class renderPanel extends JComponent {

    @Override
    public void paintComponent(Graphics g) {
        // super.paintComponent(g);
        System.out.println("painting");
        Multiplayer mp = Multiplayer.multiplayer;

        g.setColor(Color.black);
        g.drawRect(0, 0, 900, 700);
        g.fillRect(0, 0, 900, 700);  //arena

        g.setColor(Color.WHITE);
        for (Point point : mp.bikeParts) {
            g.fillRect(point.x * Multiplayer.SCALE, point.y * Multiplayer.SCALE, Multiplayer.SCALE, Multiplayer.SCALE);
        }
        g.fillRect(mp.bike.x * Multiplayer.SCALE, mp.bike.y * Multiplayer.SCALE, Multiplayer.SCALE, Multiplayer.SCALE);
    }
}

0 个答案:

没有答案
相关问题