我的問題很容易解釋:我想要一個/一些JPanels,添加到JFrame中,用圖像繪製自己。可悲的是,最後一件事情是行不通的。對於信息:圖像路徑是正確的,並且JPanel大小與圖像大小相同。 THX求助:PJPanel paintComponent(...)does not work
package frames;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import world.Terrain;
public class PanelTerrain extends JPanel {
private Image img;
private int x;
private int y;
private Image imga;
public PanelTerrain(Terrain terra, int x, int y) {
imga = new ImageIcon(terra.getPath()).getImage();
this.x = x;
this.y = y;
this.setBounds(x, y, 8, 8);
//this.setBackground(terra.getColor());
}
public void changeTerrain(Terrain t)
{
this.setVisible(false);
this.setBackground(t.getColor());
this.setVisible(true);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(imga, x, y, this);
}
}
你能看到jframe中的jpanel嗎?什麼是x,y值?如果你的面板和圖像大小相同,那麼x,y應該是0 –
正如@FastSnail所述:你如何使用這個類?你如何測試圖像實際上是正確讀取的?您是否嘗試將圖像顯示爲JOptionPane中顯示的ImageIcon?如果是這樣,它工作嗎?考慮在JPanel周圍放置一個臨時邊界,以便清楚地看到它的實際大小。請發佈[mcve]。 –
像仍然在代碼中呈現,我使用顏色測試面板,所以面板的作品。 :P 我使用它們: yArray [i] = new PanelTerrain(new Grass(),150 +(i * 8),50 + 8 * ycord); 面板應該出現的位置的x/y re。所以,用顏色進行測試,一切都像它應該的樣子。通過一個簡單的system.out.print()測試圖像(路徑)。但thx的提示,將嘗試一個JOptionPane() –