Search code examples
javapaintcomponent

Java class is despite new ...; not called up


I have a problem with call up a class. I'm programming in Eclipse and I have another Java Program, which works very well, but I can't see any difference. Here is the code of the Main class:

public class Main {
        public static void main(String[] args) {
                new Var();
                new GUI();
                new Label();
                new ActionHandler();
        }
}

If I place a System.out.println("Test");in the Label class, I don't get the output. Here is the Label class:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JLabel;

public class Label extends JLabel {
        private static final long serialVersionUID = 1L;
        protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g.drawImage(Var.ibackground, 0, 0, 1920, 1080, null);
        }
}

Thanks in advance, Lupus


Solution

  • Your label object is definitely created, but since it is not added to GUI view hierarchy it will be never used - so nobody is going to call paintComponent() on it