0

Hello I'm trying to add an Image that I have on my desktop to my JFrame that I have created i have imported all the necessary functions and the correct variables the only trouble i have is with Image observer I set my x and y values for my image but it causes an error in my drawImage component and it asks for an Image observer which i don't know what it is and if i auto fill something my Image doesn't appear on my JFrame. If one of you can look at my code or answer what an Image observer does i would be greatly appreciated

public class Window2 extends JPanel {

    // Image Import 
    ImageIcon i = new ImageIcon("C: / Class Pokemon Game/ src / GameTitle (1).psd"); 
    Image title = i.getImage();


public void paintComponent(Graphics g)
{
    super.paintComponent(g);

    this.setBackground(Color.BLACK);

    g.setColor(Color.RED);
    g.fillRect(0, 40, 5000, 20);

    g.**drawImage**(title, 500, 500);

}

}

the error is add argument to match 'drawImage(Image, int, int, ImageObserver)'

  • "it causes an error [...] and it asks for an Image observer" - Please edit your question to include the full error message – avojak Dec 12 '18 at 17:56

2 Answers2

0

ImageObserver is an interface that has methods for handling notification of state of image loading. It can use this for redisplay as needed. JFrame and Applet both implement ImageObserver interface.

To keep users informed regarding the loading of an image

  • ImageObserver interface – Enables the monitoring of the loading process so that users can be informed and the image can be used asap once it is loaded.

  • Loading an image asynchronously – how to know when the image is ready.

    • An image is ready – getImage() method returns, long before anything is known about the image.

      imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
      
  • Note: java.awt.Component implements ImageObserver, all the subclasses do as well!

  • g.drawImage(imge, 0,0, this) -- this refers to the ImageObserver instance.

  • imageUpdate() – Called by the ImageObserver whenever necessary. You do not call it explicitly!

    • If the image is complete, returns false.
    • If the image is not complete and needs to be updated, returns true.
  • ImageObserver.ALLBITS = 32

  • Various constants are combined to form the infoflags argument, which indicates whether all information is available or not.

    info flag table

Badr Kacimi
  • 64
  • 10
0

You can skip using an ImageObserver by putting it as null for example (Using Graphics2D)

g2.drawImage(Image texture, x, y, width, height, null);
Linus
  • 1
  • 3