-2

Possible Duplicate:
Cannot make a static reference to the non-static method

I am trying to tile a background, but now I am stuck. I have read the documentation for createImage(), but for some reason something is static, and I can't figure out how or why.

Here is the code I have:

Paint paint;

    if (paint == null) {
        try {
            // Create TexturePaint instance the first time
            Component c;

            Image image = c.getToolkit().getImage("Background.png");

            int height = image.getHeight(null);
            int width = image.getWidth(null);

            BufferedImage bi = (BufferedImage) Component.createImage(width, height);
            Graphics2D biG2d = (Graphics2D) bi.getGraphics();

            biG2d.drawImage(image, 0, 0, Color.black, null);

            paint = new TexturePaint(bi, new Rectangle(0, 0, width, height));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

I couldn't find a single answer on the internet, so I don't know what's going wrong. :(

Thank you very much for your support.

Community
  • 1
  • 1
Sean Heiss
  • 780
  • 2
  • 9
  • 25
  • http://stackoverflow.com/questions/4969171/cannot-make-a-static-reference-to-the-non-static-method – NominSim Dec 31 '12 at 17:50
  • I'm just going to have to find a different way to tile a background. This way is turning out to be impossible. – Sean Heiss Dec 31 '12 at 17:57

5 Answers5

2

It seems createImage is not a static method, so you can't directly access that method using class name.

createImage is instance method, so you need to instantiate Component and call createImage from that instance.

Example:

Component comp = new Component(..);
comp.createImage(...);
kosa
  • 65,990
  • 13
  • 130
  • 167
1

This line:

BufferedImage bi = (BufferedImage) Component.createImage(width, height);

is incorrect. You are making a static call on the Component class that does not exist. Not sure what you are trying to accomplish, given that you have a Component instance declared, but not initialized, higher up in your code. If you did this:

BufferedImage bi = (BufferedImage) c.createImage(width, height);

You would no longer get the compiler warning, but running the code would net you an NPE. You might want to better define what you are trying to accomplish.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • But if I remove it, I get this error: `The method createImage(int, int) is undefined for the type DisplayMap` – Sean Heiss Dec 31 '12 at 17:51
  • I am not sure what your outer class is, since you didn't post it in the code sample. But you have to make the `createImage` call on a properly initialized subclass of `Component`. Where is the `c` variable initialized, in your code? – Perception Dec 31 '12 at 17:53
0

Try this, which invokes the method on your instance of Component:

BufferedImage bi = (BufferedImage) c.createImage(width, height);
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

Probably instead of this line

BufferedImage bi = (BufferedImage) Component.createImage(width, height);

you should use your Component object, c, instead:

BufferedImage bi = (BufferedImage) c.createImage(width, height);

However, I don't see how c is going to be anything other than null with the code as it currently stands. But perhaps that is a separate problem that you will be able to address yourself.

jfrank
  • 723
  • 3
  • 9
0

Presumably you're going to set this TexturePaint object as the Paint attribute of a Graphics2D object that you obtained from a Component. You will need to create your BufferedImage using the same component.

Neil
  • 54,642
  • 8
  • 60
  • 72