-1

I know there are stack overflow questions on here about this but I am not extending canvas in my class. Is there any other way to just draw an image on the background?

//setting up a JFrame with other stuff
Canvas c = new Canvas();
Image img;
try {
    img = ImageIO.read(new URL("https://bitterli.us/namelogo.png").openStream());
    img = img.getScaledInstance(700, 145, 0);
} catch (IOException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
}
c.setBackground(Color.black);
JPanel p = new JPanel();
c.setBounds(100, 500, 1050, 500);
p.setLayout(new BorderLayout());
p.add(c, BorderLayout.CENTER);
p.setBounds(100, 50, 1050, 600);
frame.add(p, BorderLayout.NORTH);
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Bitterli
  • 23
  • 3

1 Answers1

0

Don't used canvas, just use a JPanel and override paintComponent().

public void paintComponent(Graphics g) {
  super.paintComponent(g);
  g.drawImage(img, 0, 0, null);
}
WJS
  • 36,363
  • 4
  • 24
  • 39