Here is a little program that should (in theory) draw an image of a ball on screen. The problem is that paintComponent seems to not get called. The program consists of two classes.
import java.awt.*; import java.io.*; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import javax.swing.*; public class ScreenSaver extends JPanel { private static final long serialVersionUID = 001; public static void main(String[] args) { new ScreenSaver(); } public ScreenSaver() { new Window(1600, 900, "ScreenSaver", this); } //---------------------------------------------------------------------------------- private static BufferedImage ball; public static BufferedImage getBallSprite() { try { File pathToBall = new File("ball.png"); ball = ImageIO.read(pathToBall); } catch (IOException ex) { ex.printStackTrace(); } return ball; } }
import java.awt.*; import java.io.*; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import javax.swing.*; public class Window extends Canvas { private static final long serialVersionUID = 002; public Window(int width, int height, String title, ScreenSaver ScreenSaver) { JFrame frame = new JFrame(title); frame.setPreferredSize(new Dimension(width, height)); frame.setMaximumSize(new Dimension(width, height)); frame.setMinimumSize(new Dimension(width, height)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.setLocationRelativeTo(null); frame.setVisible(true); repaint(); } public void paintComponent(Graphics g) { System.out.println("Painting..."); BufferedImage ball = ScreenSaver.getBallSprite(); g.drawImage(ball, 0, 0, 100, 100, this); } }
As you can see, I tested if paintComponent was called using a console message. Sadly this was not the case. Can someone explain?
Advertisement
Answer
It’s no wonder that paintComponent
is not called, because Canvas
has no implementation of paintComponent
which you can override. With a canvas
you have to overwrite paint
for your purposes. In your code you use both a JPanel
and a Canvas
, which is not necessary at all. Use either of the two.
The following is an example with a Canvas
:
import java.awt.*; import javax.swing.*; public class ScreenSaver extends Canvas{ public static void main(String[] args) { JFrame window = new JFrame("Screensaver"); window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); window.setResizable(false); ScreenSaver canvas = new ScreenSaver(); canvas.setPreferredSize(new Dimension(1600, 900)); canvas.setBackground(Color.BLACK); window.add(canvas); window.pack(); window.setLocationRelativeTo(null); window.setVisible(true); } @Override public void paint(Graphics g){ g.setColor(Color.GREEN); g.fillOval(100, 100, 100, 100); } }
The annotation Override
above the method to be overwritten ensures that the compiler can issue a warning message if the overwritten method does not exist or there is a typo. I hope this helps you further.