0

I'm creating a simple program that just makes a ball sink all the way to the bottom unless you press the space bar in which case it'll jump but it isn't repainting the screen when it's in the process of jumping. So when the ball is supposed to be going up (which I can confirm that it actually does), it just freezes. Here is the code: NOTE - The canBePressed boolean variable isn't in is use yet so you can just ignore it.

Main

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);
    }
}

View

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;
    }
}

Jumper

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;
    }
}

Key (ENUM)

public enum Key {
    SPACE
}

Model (Where the problem is probably occurring)

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);
        }

    }

}

Anyone got any ideas?

ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
Eames
  • 321
  • 2
  • 19
  • 1
    Never call `Thread.sleep()` or any long action from inside of an event handler. – RealSkeptic Feb 20 '16 at 22:25
  • It goes the 100 pixels up and then it freezes ? Or it freezes directly when you press to go up ? – king Feb 20 '16 at 22:42
  • Don't override `getBounds`, use `getPreferredSize` instead. `static List key;` is a bad idea, apart from the fact you should be using a `Set`, `static` is just a bad idea, you're going to have to change your design to enable you to remove it. Don't maintain a reference to `Graphics` context you did not create, this is dangerous. For some reason when the player can jump, you `stop` the `Timer`? – MadProgrammer Feb 21 '16 at 01:59
  • Maybe you should have a look at [this](http://stackoverflow.com/questions/19626338/japplet-creates-a-ball-that-bounces-and-gets-progressively-less-high-in-java/19626396#19626396) example – MadProgrammer Feb 21 '16 at 01:59
  • @RealSkeptic What should I do instead? – Eames Feb 21 '16 at 13:16
  • @user3549047 It freezes directly when I try to go up, but works fine as it's on its way down. – Eames Feb 21 '16 at 13:17
  • Post an [MCVE](http://stackoverflow.com/help/mcve). Be sure to copy-paste your code to a *new project* and make sure it compiles and runs before posting it here. – user1803551 Feb 21 '16 at 19:04

0 Answers0