我可以問你的幫助嗎?我在JPanel內部繪製圖像時遇到了一些麻煩。我用來創建擴展的JPanel類,這樣做,使用drawImage在JPanel中繪製圖像
public class Example extends JPanel {
BufferedImage background;
public Example() {
background = loadImage();
}
private BufferedImage loadImage(){
URL imagePath = getClass().getResource("Immagini/Board.png");
BufferedImage result = null;
try {
result = ImageIO.read(imagePath);
} catch (IOException e) {
System.err.println("Errore, immagine non trovata");
}
return result;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension size = getSize();
g.drawImage(background, 0, 0,size.width, size.height,0, 0, background.getWidth(), background.getHeight(), null);
}
}
它是完美的,現在我不能這樣做,因爲我的newExample類擴展genericExample所以不能擴展JPanel過。在我想在上面的代碼中進行繪製的newExample中有JPanel panel = new JPanel()
,我該如何適應它以適應這種不同的情況?
我會檢查出java接口。 [這裏](https://docs.oracle.com/javase/tutorial/java/concepts/interface.html)是關於 – mrob