1

I'm trying to add an ImageIcon to a panel through paintComponent, but it doesn't work. The panel i'm trying to add it to is set to a GridLayout. Could this be why? or is it being drawn over? Or my path could be set incorrectly... I've never

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Frame implements MouseListener, KeyListener {

JFrame f = new JFrame();
JPanel p = new JPanel();

JPanel[][] panel = new JPanel[10][10];

int k = 1;
Color previous;

ImageIcon icon = new ImageIcon("/Users/Admin/Desktop/stickFigure.jpg");

static String title = "Grid World";

public Frame(String s) {
    f.setTitle(s);
    p.setLayout(new GridLayout(10, 10));
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            panel[i][j] = new JPanel();
            p.add(panel[i][j], i, j);
            panel[i][j].addMouseListener(this);
            panel[i][j].setBackground(Color.WHITE);
        }
    }
    p.setSize(500, 500);
    f.add(p);
    f.setSize(490, 492);
    f.setVisible(true);
    f.setResizable(false);
    f.setDefaultCloseOperation(3);
    f.addKeyListener(this);
    f.setLocationRelativeTo(null);
}

public void paintComponent(Graphics g) {
    icon.paintIcon(f, g, 100, 100);
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Zyvo
  • 11
  • 1
  • 4
  • 4
    No, the component's layout has nothing to do with your problem. As for the cause of your problem, at this point it's anyone's guess. It could be because you're adding opaque components on to the JPanel, but again, who knows. If you still need our help, consider giving us more information, enough so that don't have to guess what you're doing. As a general guide, ask your question as if you were the one who had to answer it and had no clue as to the questioner's code. Consider things from our perspective. – Hovercraft Full Of Eels Sep 14 '12 at 03:56
  • Your class `Frame` is not a component so `paintComponent()` will not be invoked. – tenorsax Sep 14 '12 at 04:20
  • Agreed. Can you comment out the for loop and try running your code. The panel opacity might have made your image hidden. If a background is what you need, may be [this](http://stackoverflow.com/questions/2545214/how-to-set-a-transparent-background-of-jpanel) thread will help – javatarz Sep 14 '12 at 04:20

1 Answers1

3

You've got a paintComponent method in a class that does not extend JPanel, JComponent or any similar object, and thus it will never be called and serve no purpose. If you want paintComponent to work as intended, it must be in a class that extends JComponent or one of its children such as JPanel. And then you must use that JPanel in your GUI. Please read the Swing painting tutorials to see how to do this correctly.

Edit
Another way to display an ImageIcon is to simply add it to a JLabel and then display the JLabel in a Swing GUI.

Edit 2
Also, even if your class extended JPanel, it still wouldn't work since your icon is never added to anything. I've not seen graphics done as you're doing -- by calling the icon's paintIcon(...) method. I can't say that it's wrong; just that I haven't seen done this way.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Is there any other way to draw an image icon then? Besides making the class extend JPanel? – Zyvo Sep 14 '12 at 04:23
  • I think I got it but I still don't have the path set correctly :/ Idk if that looks like the right path for a Mac? – Zyvo Sep 14 '12 at 04:30
  • What way would you suggest to do it? – Zyvo Sep 14 '12 at 04:33
  • 2
    @Riff: I have no idea if your path is correct since all of the files reside on your computer and you are privy to the path information. Myself, I find that the simplest way to display an image is to place it into an ImageIcon, place the ImageIcon in a JLabel and then simply display the JLabel. If I need to manipulate the image then I store it as a BufferedImage and display it in the `paintComponent(...)` method of a JPanel using the Graphics object's `drawImage(...)` method. – Hovercraft Full Of Eels Sep 14 '12 at 04:36