0

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);
        
    }
    
}
  • Oracle has a helpful tutorial, [Creating a GUI With Swing](https://docs.oracle.com/javase/tutorial/uiswing/index.html). Skip the Learning Swing with the NetBeans IDE section. Pay particular attention to the [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) section. – Gilbert Le Blanc Jun 11 '22 at 17:57
  • 1) Swing is double buffered by default. There is no need to set the double buffered property. 2) your update method does nothing. It should also NOT be a static method. 3) you have a while loop that just keeps looping and never ends. For animation use a Swing Timer. 4) You are "disposing" the Graphics object before you attempt to paint the Rectangle. You should dispose AFTER you are finished painting. 5) check out: https://stackoverflow.com/questions/54028090/get-width-and-height-of-jpanel-outside-of-the-class/54028681#54028681 for some code that uses animation. – camickr Jun 11 '22 at 18:56

0 Answers0