These questions have been driving me mental so here goes. For clarification, this program works perfectly, I’d just like to know if I can write it in a different, possibly better way.
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
private static final int width = 400;
private static final int height = 400;
private BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
private boolean running = false;
public static void main (String[] args) {
Game game = new Game();
game.setPreferredSize(new Dimension(width,height));
game.setFocusable(true);
game.addKeyListener(new Input());
JFrame frame = new JFrame("Toast");
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.start();
}
public void start () {
running = true;
new Thread(this).start();
}
public void stop () {
running = false;
}
public void run () {
double before = System.currentTimeMillis();
long now;
double unprocessed = 0;
while (running) {
now = System.currentTimeMillis();
unprocessed += (now-before)*60/1000;
before = now;
while (unprocessed >= 0) {
update();
unprocessed --;
}
render();
}
}
public void update () {
Player.update();
}
public void render () {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.drawImage(image,0,0,getWidth(),getHeight(),null);
g.setColor(Color.WHITE);
g.fillRect(Math.round(Player.x),Math.round(Player.y),Player.w,Player.h);
g.dispose();
bs.show();
}
}
The code has been shortened to make it more read-able.
Question 1: I can remove the method stop with no noticeable change. Is the method ever called and if not - why bother?
Question 2: Changing the method start to private doesn’t affect my program. However, I receive an error message when doing the same to method run. Why does run have to be public?
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Input implements KeyListener {
static boolean left = false;
static boolean right = false;
static boolean up = false;
static boolean down = false;
public void keyPressed (KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT) {
left = true;
}
if (keyCode == KeyEvent.VK_RIGHT) {
right = true;
}
if (keyCode == KeyEvent.VK_UP) {
up = true;
}
if (keyCode == KeyEvent.VK_DOWN) {
down = true;
}
}
public void keyReleased (KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT) {
left = false;
}
if (keyCode == KeyEvent.VK_RIGHT) {
right = false;
}
if (keyCode == KeyEvent.VK_UP) {
up = false;
}
if (keyCode == KeyEvent.VK_DOWN) {
down = false;
}
}
public void keyTyped(KeyEvent e) {
}
}
This is less important and less about this specific class but if you’re feeling generous.
Question 3: The static variables in my input-handling class are used, but never changed, in other classes. This brings to my total noob question. Can’t public variables be accessed by all classes? Why does a static type do the job for me?
I’d gladly appreciate some help!