Search code examples
javaswingpaintcomponent

paintComponent(); is not working/ being called


here is my code. and i a little new to coding so please exsplan a lot.

public class main extends JPanel{


    public static Window f = new Window();
        public static int Width = 600;
        public static int Hight = 400;
        public static void main(String args[]){ 

            f.setSize(Width, Hight);
            f.setVisible(true);
            f.setResizable(false);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setTitle("FightForYourLife");
            f.setLocationRelativeTo(null);
        }

        public void main(){
            JPanel panel = this;
        }

        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setColor(Color.RED);
            g.fillRect(100, 100, 50, 30);
            System.out.println("IT IS PAINTING");
        }



}

Solution

  • Bunch of problems but number 1:

    • You never add your main object to your top-level Window, the GUI, and so since it is never added to the GUI, it is never shown nor rendered, and so paintComponent will never be called. Create a new Main object in your public static void main method and add it to your Window (why Window and not JFrame?).

    Other issues:

    • Your class as a "pseudo" constructor, public void main(){. Remember that constructors don't have return types, not void, not anything. So it should be public main() {.
    • Your pseudo constructor does nothing of use. JPanel panel = this; ???
    • You will want to follow Java naming rules: class names begin with an upper case letter and fields and methods with a lower case letter. So public class main { should be public class Main {
    • Again, why Window and not JFrame?

    So for example:

    import java.awt.*;
    import javax.swing.*;
    
    // rename main to Main
    public class Main extends JPanel{
    
            public static int WIDTH = 600;
            public static int HEIGHT = 400;
    
            public static void main(String args[]){ 
                Main main = new Main(); // *** create your main object ***
    
                JFrame f = new JFrame("FightForYourLife");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(main);  // *** add your main object to the top-level window
                f.pack();  
                f.setResizable(false);
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
    
            public Main(){
                // JPanel panel = this; // this is not helpful, delete it
            }
    
            @Override // let the JPanel size itself.
            public Dimension getPreferredSize() {
                return new Dimension(WIDTH, HEIGHT);
            }
    
            @Override  // paintComponent should be protected, not public
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.setColor(Color.RED);
                g.fillRect(100, 100, 50, 30);
                System.out.println("IT IS PAINTING");
            }
    }