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);
}
}
这就是我正在制作的 blackjac 游戏的底层代码。但是,在 paintComponent 中的 eclipse 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));
最佳答案
drawImage()
的最后一个参数必须实现ImageObserver
接口(interface)。如果 LayerUI
这样做,您可以指定 this
。或者,考虑使用 ImageIO.read()
同步读取图像,并指定 null
作为最终参数。
关于java - LayerUI, g.drawImage 下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14129111/