球不跳

时间:2016-02-20 22:10:39

标签: java swing user-interface game-physics

我正在创建一个简单的程序,只是让一个球一直到底部,除非你按空格键,在这种情况下它会跳转但是在跳跃的过程中它不会重新绘制屏幕。因此,当球应该上升时(我可以确认它实际上是这样),它就会冻结。这是代码: 注意 - 尚未使用canBePressed布尔变量,因此您可以忽略它。

主要

import javax.swing.JFrame;

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        View view = new View();
        Jumper jumper = new Jumper();
        Model model = new Model(view, jumper);
        view.setJumper(jumper);

        frame.setSize(500, 460);
        frame.getContentPane().add(view);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

查看

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class View extends JPanel {
    private Jumper jumper;
    private Rectangle bounds;
    private Model model;

    public View() {
        setBackground(Color.BLACK);
        bounds = new Rectangle(0, 0, 400, 400);

        addKeyBinding("space.pressed", KeyEvent.VK_SPACE, new MoveAction(true, Key.SPACE), false);
        addKeyBinding("space.released", KeyEvent.VK_SPACE, new MoveAction(false, Key.SPACE), true);
    }

    public void addKeyBinding(String name, int keyEvent, AbstractAction action, boolean pressed) {
        InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = getActionMap();

        inputMap.put(KeyStroke.getKeyStroke(keyEvent, 0, pressed), name);
        actionMap.put(name, action);
    }

    public void setJumper(Jumper jumper) {
        this.jumper = jumper;
    }

    @Override
    public Rectangle getBounds() {
        return bounds;
    }

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

    public void setModel(Model model) {
        this.model = model;
    }
}

跳线

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;

public class Jumper {
    private Point location;
    private Dimension dimension;
    private Graphics graphics;

    public Jumper() {
        location = new Point(250, 250);
        dimension = new Dimension(20, 20);
    }

    public Dimension getSize() {
        return dimension;
    }

    public void setLocation(Point location) {
        this.location = location;
    }

    public void callPaint() {
        paint(graphics);
    }

    public void paint(Graphics g) {
        graphics = g;
        System.out.println("PAINT");
        g.setColor(Color.GREEN);
        g.fillOval(location.x, location.y, dimension.width, dimension.height);
    }

    public Point getLocation() {
        return location;
    }
}

密钥(ENUM)

public enum Key {
    SPACE
}

模型(可能出现问题的地方)

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Timer;

public class Model {

    private boolean canBePressed;
    private int ballX, ballY;
    private int timeCount;
    private Thread thread;
    private Jumper jumper;
    private View view;
    private Timer timer1;
    private Point point;
    static List<Key> key;

    public Model(View view, Jumper jumper) {
        this.view = view;
        view.setModel(this);
        canBePressed = true;
        key = new ArrayList<Key>(3);
        this.jumper = jumper;
        ballX = 250;
        ballY = 0;
        thread = new Thread();

        timer1 = new Timer(5, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                try {
                    update();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                view.repaint();
            }

        });
        timer1.start();
    }

    public void update() throws InterruptedException {

        if (!key.isEmpty()) {
            if (canBePressed) {
                timer1.stop();
                jump();
            }
        }

        if (ballY + jumper.getSize().height > view.getBounds().height) {
            canBePressed = true;
        } else {
            ballY++;
        }

        point = new Point(ballX, ballY);
        jumper.setLocation(point);
        point = null;
    }

    public void jump() throws InterruptedException {
        for (timeCount = 0; timeCount < 100; timeCount++) {
            ballY--;
            System.out.println(ballY);
            Thread.sleep(5);
            jumper.setLocation(new Point(ballX, ballY));
            view.repaint();
        }

        timer1.start();
    }

}

MoveAction

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

public class MoveAction extends AbstractAction {

    private boolean pressed;
    private Key direction;

    public MoveAction(boolean pressed, Key direction) {
        this.pressed = pressed;
        this.direction = direction;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (pressed) {
            if (Model.key.size() < 1)
                Model.key.add(direction);
        } else {
            Model.key.remove(direction);
        }

    }

}

有人有任何想法吗?

0 个答案:

没有答案