Search code examples
javaswingrepaint

Why won't the background color be filled with repaint(e)?


So I'm not sure what the problem is with the repaint method. It won't fill in a new background colour. It doesn't seem to print out a text within that method whenever I tried to debug it. I have 3 classes: GameInterface, Renderer, and RepaintConfiguration. The RepaintConfiguration extends from the GameInterface class and implements ActionListener.

public class GameInterface {

public static GameInterface gameInterface;   

public static JFrame jframe;

private String title;

private Container container;

public GameInterface() {
    gameInterface = this;
    jframe = new JFrame();
    jframe.setSize(1500, 800);
    jframe.setResizable(false);
    jframe.setTitle("Jetpack");
   
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jframe.setVisible(true);
  } 
}
   

public class Renderer extends JPanel {

  
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        RepaintConfiguration.repaintConfiguration.repaint(g);
      }
}


public class RepaintConfiguration extends GameInterface implements ActionListener
{

    public static RepaintConfiguration repaintConfiguration;
    public static Renderer renderer;
   
    public static GameInterface gameInterface;
    

    public RepaintConfiguration() {
        super();
        repaintConfiguration = this;
        renderer = new Renderer();
        super.jframe.add(renderer);
   
        
        
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        renderer.repaint();
    }
    
    
    
    public void repaint(Graphics g) {
        g.setColor(Color.red);
        g.fillRect(0, 0, 1500, 800);
    }

    
}

Solution

  • I managed to get your code to compile and run. Public classes have to be inner classes.

    I added a main method. I added a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.

    I reordered the JFrame method calls. The JFrame methods must be called in a specific order. This is the order I use for all my Swing applications.

    You don't size the JFrame. You size the drawing JPanel. The JFrame includes decorations that take up some space.

    I left your convoluted drawing code. You're going to have great difficulties extending this code to actually create a game.

    Here's the complete runnable code.

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class GameInterface {
    
        private JFrame jframe;
    
        private Renderer renderer;
    
        private RepaintConfiguration repaintConfiguration;
        
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new GameInterface();
                }
            });
        }
    
        public GameInterface() {
            this.repaintConfiguration = new RepaintConfiguration(this);
    
            jframe = new JFrame();
            jframe.setTitle("Jetpack");
            jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jframe.setResizable(false);
    
            this.renderer = new Renderer(repaintConfiguration);
            jframe.add(renderer, BorderLayout.CENTER);
    
            jframe.pack();
            jframe.setLocationByPlatform(true);
            jframe.setVisible(true);
        }
    
        public Renderer getRenderer() {
            return renderer;
        }
    
        public class Renderer extends JPanel {
    
            private static final long serialVersionUID = 1L;
            
            private RepaintConfiguration repaintConfiguration;
    
            public Renderer(RepaintConfiguration repaintConfiguration) {
                this.repaintConfiguration = repaintConfiguration;
                this.setPreferredSize(new Dimension(1500, 800));
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                repaintConfiguration.repaint(g);
            }
        }
    
        public class RepaintConfiguration implements ActionListener {
    
            private GameInterface gameInterface;
    
            public RepaintConfiguration(GameInterface gameInterface) {
                this.gameInterface = gameInterface;
            }
    
            @Override
            public void actionPerformed(ActionEvent event) {
                gameInterface.getRenderer().repaint();
            }
    
            public void repaint(Graphics g) {
                g.setColor(Color.red);
                g.fillRect(0, 0, 1500, 800);
            }
    
        }
    
    }