Search code examples
javaimageswingjava-7jlayer

LayerUI, g.drawImage underlined


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));

Solution

  • 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.