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游戏即时通讯的底层编写的代码。但是,在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));
最佳答案
如垃圾桶所说,使用ImageIO.read()读取图像,然后可以将ImageObserver参数设置为null,而无需实现ImageObserver。
如果我运行它,则最上面的层(基本上是带有按钮的JPanel)不是透明的。
那是预期的行为。注意他们在JLayer tutorial中如何使用该行
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));
实现透明度。