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?