1
public class GraphicsBoard extends LayerUI {
    String picPath = "pictures/";
    String[] fileName = { "cards.png", "BlackJackBoard.png" };
    ClassLoader cl = GraphicsBoard.class.getClassLoader();
    URL imgURL[] = new URL[2];
    Toolkit tk = Toolkit.getDefaultToolkit();
    Image imgCards, imgBG;

    public GraphicsBoard() throws Exception {
        for (int x = 0; x < 2; x++)
            imgURL[x] = cl.getResource(picPath + fileName[x]);
        imgCards = tk.createImage(imgURL[0]);
        imgBG = tk.createImage(imgURL[1]);
    }

    public void paintComponent(Graphics g) {
        g.drawImage(imgBG, 0, 0, 550, 450, 0, 0, 551, 412, this);
    }
}

So thats my code for the underpart of a blackjac game im making. However, in eclipse drawImage in the paintComponent is underlined and I'm not really sure how to fix it. when i hover over it, it says

The method drawImage(Image, int, int, int, int, int, int, int, int, ImageObserver) in the type Graphics is not applicable for the arguments (Image, int, int, int, int, int, int, int, int, GraphicsBoard)

and the option given to me are

Cast argument 'this' to 'ImageObserver'

and

Let 'GraphicsBoard' implement 'ImageObserver'

If I run it, the layer on top (which is basically a JPanel with a button) is not transparent.

This is what i use to add the JLayer to my Frame

OverBoard overLay = new OverBoard();
GraphicsBoard graphicsBG = new GraphicsBoard();
add(new JLayer(overLay, graphicsBG));
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Exikle
  • 1,155
  • 2
  • 18
  • 42

2 Answers2

3

The final parameter to drawImage() must implement the ImageObserver interface. If LayerUI does so, you can specify this. Alternatively, consider using ImageIO.read() to read images synchronously, and specify null as the final parameter.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • See also [`Toolkit.getDefaultToolkit().createImage()` vs `ImageIO.read()`](http://stackoverflow.com/q/12041474/230513) and this [Q&A](http://stackoverflow.com/q/7045214/230513). – trashgod Jan 03 '13 at 00:44
  • I know that, Im asking on how to fix that, I dont want to implement ImageObserver because i dont even know how to use it – Exikle Jan 03 '13 at 09:56
  • What is `LayerUI`? What happens when you try `this` or `null`. – trashgod Jan 03 '13 at 10:00
  • 2
    trashgod: LayerUI must be javax.swing.plaf.LayerUI (which appeared in Java 7) – lbalazscs Jan 08 '13 at 18:10
2

As trashgod said, use ImageIO.read() to read your image, and then you can set the ImageObserver parameter to null, and you don't need to implement ImageObserver.

If I run it, the layer on top (which is basically a JPanel with a button) is not transparent.

That is the expected behavior. Notice how in the JLayer tutorial they use the line

g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));

to achieve transparency.

lbalazscs
  • 17,474
  • 7
  • 42
  • 50