Paint Component Method not working for JFrame

302
November 23, 2017, at 12:22 PM

I'm trying to paint an image on the screen after trying to use JLabel's and am now trying the paintComponent method. I tried inserting breakpoints after seeing no results and the method doesn't get called, and nothing appears. What should I do? Here is my important code-

`

     public void createWindow(){        
            frame.setVisible(true);  
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.pack();
    frame.setSize(xSize, ySize);
            frame.setLocation(0, 0);
            frame.addComponentListener(this);
            //frame.add(im);             
    }

public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(placeholder, 0, 0, getWidth(), getHeight(), null); g.drawString("Hello", 100, 100); }

Also I'm using a JFrame instead of JPanel or component if that makes a difference.

Answer 1

JFrame does not have a paintComponent method. You should avoid painting directly to a frame and instead use a JPanel and override its paintComponent method

You should also make use of the @Override annotation, which will raise a compiler exception if the parent class does not have the method you are trying to override...

Rent Charter Buses Company
READ ALSO
Bukkit Event gets registered twice

Bukkit Event gets registered twice

So I've been building some EventHandlers in Bukkit and now I am stuck on thisIt's probably a really dumb mistake, so forgive me for that

239
Runnables vs. Method References and Garbage

Runnables vs. Method References and Garbage

A relatively recent published article about replacing executors by actors in Java 8 stated that using an anonymous inner class Runnable like so:

214