1
我一直在關注學習圖形的教程,並且在一個程序中作者使用圖像來製作紋理塗料。我已經複製了他的代碼,但我不知道在哪裏實際放置圖像供它閱讀。我已經嘗試在eclipse中創建一個資源文件夾,並將其設置爲源文件夾構建路徑,但這沒有奏效。代碼如下:我在哪裏把圖像的Java程序輸入?
編輯: 好吧,我發現它是從班級的源頭拍攝圖像。但是,可以說我想從我的桌面或硬盤上的其他位置拉出圖像,我該怎麼做?
class Surface extends JPanel {
private BufferedImage slate;
private BufferedImage java;
private BufferedImage pane;
private TexturePaint slatetp;
private TexturePaint javatp;
private TexturePaint panetp;
public Surface() {
loadImages();
}
private void loadImages() {
try {
slate = ImageIO.read(new File("slate.png"));
java = ImageIO.read(new File("java.png"));
pane = ImageIO.read(new File("pane.png"));
} catch (IOException ex) {
Logger.getLogger(Surface.class.getName()).log(
Level.SEVERE, null, ex);
}
}
private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
slatetp = new TexturePaint(slate, new Rectangle(0, 0, 90, 60));
javatp = new TexturePaint(java, new Rectangle(0, 0, 90, 60));
panetp = new TexturePaint(pane, new Rectangle(0, 0, 90, 60));
g2d.setPaint(slatetp);
g2d.fillRect(10, 15, 90, 60);
g2d.setPaint(javatp);
g2d.fillRect(130, 15, 90, 60);
g2d.setPaint(panetp);
g2d.fillRect(250, 15, 90, 60);
g2d.dispose();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}
http://stackoverflow.com/questions/17912041/java-swing-imageicon-where-to-put-images-solved這解決你的問題? –
ahh yes它確實是gmk!謝謝!具有諷刺意味的是,這段代碼也是來自zetcode的教程。儘管我可能仍然保持這個問題的開放性並對其進行一些改變,但是如何選擇不同的位置作爲圖像的來源? –
只需使用絕對'PATH'來存檔。 linux:/ home/user/... widndows:C:/ Users/.. –