I have a problem with my java code, I think it´s caused by the repaint() method. The Info.version is just a string with "v1.0.0" or something, but it is not causing the problem.
What I am trying to do is to render a square by repainting and disposing it as long as the program is running. when i just run the program nothing happens, i will just see the red ugly background which I added to test if the component was even added.
public class MainWindow {
public static void main(String[] args) {
GamePanel gamepanel = new GamePanel();
JFrame window = new JFrame();
window.setDefaultCloseOperation(3);
window.setResizable(true);
window.setTitle("2D Adventure Game" + Info.version);
window.add(gamepanel);
gamepanel.startGameThread();
window.setVisible(true);
window.pack();
window.setVisible(true);
window.setLocationRelativeTo(null);
}
}public class GamePanel extends JPanel implements Runnable{
private static final long serialVersionUID = 1L;
//screen settings
public static final int originalPlayerSize = 16; //16 by default
public static final int scale = 3;
public static final int tileSize = originalPlayerSize * scale;
public static final int maxScreenCol = 40;
public static final int maxScreenRow = 22;
public static final int screenWidth = tileSize * maxScreenCol;
public static final int screenHeight = tileSize * maxScreenCol;
Thread gameThread;
public GamePanel() {
this.setPreferredSize(new Dimension(screenWidth, screenHeight));
this.setBackground(Color.red);
this.setDoubleBuffered(true);
}
public void startGameThread() {
gameThread = new Thread(this);
gameThread.start();
}
@Override
public void run() {
while(gameThread != null) {
repaint();
update(); //update game data
}
}
public static void update() {
}
public void drawComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.ORANGE);
g2d.fillRect(100, 100, tileSize, tileSize);
g2d.dispose();
g2d.drawRect(50, 50, 100, 100);
}
}