Reputation: 1
I'm working on an indie game project and am having an issue with graphics not being displayed when I move my project from my Windows computer to my macbook. I've updated both my Java version and my Eclipse but am still having this issue. Here are the main two files that handle the JFrame and Panel:
MainGameLoop
package hara;
import javax.swing.JFrame;
public class MainGameLoop {
public static final String gameName = "HARA v0.0";
public static void main(String[] args) {
JFrame f = new JFrame(gameName);
f.setContentPane(new MainWindow());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
and MainWindow
package hara;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import hara.gamestate.GameStateManager;
public class MainWindow extends JPanel implements Runnable, KeyListener{
public static final int WIDTH = 320;
public static final int HEIGHT = 240;
public static double wSCALE = 4;
public static double hSCALE = 4;
private Thread thread;
private boolean running;
private int FPS = 60;
private long targetTime = 1000/FPS;
private BufferedImage image;
private Graphics2D g;
private GameStateManager gsm;
public MainWindow(){
super();
setPreferredSize(new Dimension((int)(WIDTH * wSCALE), (int)(HEIGHT * hSCALE)));
setFocusable(true);
requestFocus();
}
public void addNotify(){
super.addNotify();
if(thread == null){
thread = new Thread(this);
addKeyListener(this);
thread.start();
}
}
public void init(){
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
g = (Graphics2D) image.getGraphics();
running = true;
gsm = new GameStateManager();
gsm.setState(gsm.MENUSTATE);
}
public void run(){
init();
long start;
long elapsed;
long wait;
while(running){
start = System.nanoTime();
update();
draw();
drawToScreen();
elapsed = System.nanoTime() - start;
wait = targetTime - elapsed / 1000000;
if(wait < 0){
wait = 5;
}
try {
Thread.sleep(wait);
}catch(Exception e){
e.printStackTrace();
}
}
}
private void update(){
gsm.update();
}
private void draw(){
gsm.draw(g);
}
private void drawToScreen(){
Graphics g2 = getGraphics();
g2.drawImage(image, 0, 0, (int) (WIDTH * wSCALE), (int) (HEIGHT * hSCALE), null);
g2.dispose();
}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent key) {
gsm.keyPressed(key.getKeyCode());
}
public void keyReleased(KeyEvent key) {
gsm.keyReleased(key.getKeyCode());
}
}
It runs perfectly on my PC and there are no problems with the drawing but once I moved the files to my Macbook, it just opens a blank JFrame and does nothing. I tested to see if the methods were properly being called and they were. The draw methods are called, the keyboard input works but nothing shows up. If I set the window to be resizable, and I resize it, I can very briefly see the graphics working. I've tried a lot of solutions and none of them worked. I've looked through a lot of questions on this site and nothing seemed to work for me. Would love any ideas. Thanks!
Upvotes: 0
Views: 757
Reputation: 324098
Don't use getGraphics() to do painting.
Instead, custom painting is done by overriding the paintComponent()
method of the JPanel
. Read the section from the Swing tutorial on Custom Painting for more information and working examples.
When you want to repaint the component because a property of the panel changes then you invoke repaint() on the panel. So get rid of your update() method and simply invoke repaint() to paint the panel.
Upvotes: 1