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);
}
}
这就是我正在制作的二十一点游戏的底层代码。但是,在 Eclipse 中,paintComponent 中的 drawImage 带有下划线,我不确定如何修复它。当我将鼠标悬停在它上面时,它说
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)
给我的选择是
Cast argument 'this' to 'ImageObserver'
和
Let 'GraphicsBoard' implement 'ImageObserver'
如果我运行它,顶部的层(基本上是一个带有按钮的 JPanel)是不透明的。
这就是我用来将 JLayer 添加到我的框架的方法
OverBoard overLay = new OverBoard();
GraphicsBoard graphicsBG = new GraphicsBoard();
add(new JLayer(overLay, graphicsBG));