2016-01-26 56 views
0

我在製作一個需要顯示圖像的程序。我正在使用ImageIcon和Image類來執行此操作。我在構造函數中聲明瞭ImageIcon,然後通過i.getImage()分配圖像值。除圖像外的所有內容似乎都加載正常。這是我的代碼:Java:圖像不顯示

更新:我有與代碼相同的目錄中的圖像。我正在使用mac,並嘗試過「image.png」,「./image.png」,「Users/myStuff/Documents/workspace/Game/src/image.png」。其他的這些都起作用了。

import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 

import javax.swing.ImageIcon; 
import javax.swing.JFrame; 

@SuppressWarnings("serial") 
public class Game extends JFrame { 

    int x = 100, y = 100; 

    private Image dbimage; 
    private Graphics dbg; 

    Image image; 

    class AL extends KeyAdapter { 

     public void keyPressed(KeyEvent e) { 
      int keyCode = e.getKeyCode(); 
      if (keyCode == e.VK_LEFT) { 
       if (x <= 20) { 
        x = 20; 
       } else { 
        x -= 5; 
       } 
      } else if (keyCode == e.VK_RIGHT) { 
       if (x >= 230) { 
        x = 230; 
       } else { 
        x += 5; 
       } 
      } else if (keyCode == e.VK_UP) { 
       if (y <= 20) { 
        y = 20; 
       } else { 
        y -= 5; 
       } 
      } else if (keyCode == e.VK_DOWN) { 
       if (y >= 230) { 
        y = 230; 
       } else { 
        y += 5; 
       } 
      } 
     } 

    } 

    public Game() { 

     //load up image 
     ImageIcon i = new ImageIcon("image.png"); 
     image = i.getImage(); 

     //set up properties 
     addKeyListener(new AL()); 
     setTitle("Game"); 
     setSize(250, 250); 
     setResizable(false); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBackground(Color.CYAN); 
     setVisible(true); 

    } 

    public void paint(Graphics g) { 
     dbimage = createImage(getWidth(), getHeight()); 
     dbg = dbimage.getGraphics(); 
     paintComponent(dbg); 
     g.drawImage(dbimage, 0, 0, this); 
    } 

    public void paintComponent(Graphics g) { 
     g.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 30)); 
     g.setColor(Color.MAGENTA); 
     g.drawString("Hello World!", 50, 50); 
     g.setColor(Color.RED); 
     g.drawImage(image, 100, 100, this); 
     repaint(); 
    } 

    public static void main(String[] args) { 
     new Game(); 
    } 

} 
+1

這取決於您放置圖像的位置。所以請確保你放置並使用正確的位置。 –

+1

是的,圖像的正確路徑是我開始的地方。因此,請在最簡單的程序中進行調試 - 在JOptionPane中顯示ImageIcon。你也應該看看類似的問題沒有這個網站,因爲你正在繪製你的圖像都是錯誤的,包括試圖直接在JFrame中繪製它。每個教程,以及本網站上任何正確的答案都會告訴你永遠不要這樣做。 –

+0

有關類似問題的列表,請查看右側相關問題列表,並查看[此Google搜索](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie = UTF-8#q = JFrame的+圖像+ +未顯示+站點:http:%2F%2Fstackoverflow.com%2F)。 –

回答

2

上面代碼中的重大問題...

首先,如果你的形象是你的類文件,然後不明白這一點作爲一個文件,而是把它作爲一種資源作爲MadProg節目(以及在該網站最類似的問題會告訴你):

所有的
// get your image as a resource 
    URL resource = Game.class.getResource(RESOURCE_PATH); 
    BufferedImage img = null; 
    try { 
     // read in using ImageIO 
     img = ImageIO.read(resource); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     System.exit(-1); 
    } 

接下來,從不直接JFrame中內的烤漆方法中借鑑,當然不是。相反,檢查出的擺動拉絲教程,並按照他們的領導:一個JPanel的paintComponent方法裏面內畫,但只調用超類的paintComponent方法裏面之後,這樣的畫可以繼續往下畫鏈:

// draw within the paintComponent method, not the paint method 
@Override 
protected void paintComponent(Graphics g) { 
    // call the super's method to all painting to chain down the line 
    super.paintComponent(g); 
    if (dbimage != null) { 
     g.drawImage(dbimage, imgX, imgY, this); 
    } 
} 

我更喜歡固定我的GUI的大小時,覆蓋JPanel的的getPreferredSize,是這樣的:

// set the preferred size of the main Game JPanel safely 
@Override 
public Dimension getPreferredSize() { 
    if (isPreferredSizeSet()) { 
     return super.getPreferredSize(); 
    } 
    return new Dimension(PREF_W, PREF_H); 
} 

放在一起,像這樣威力工作:

import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.net.URL; 

import javax.imageio.ImageIO; 
import javax.swing.*; 

public class Game extends JPanel { 
    public static final String RESOURCE_PATH = "image.png"; 
    private static final int PREF_W = 400; 
    private static final int PREF_H = PREF_W; 
    private BufferedImage dbimage = null; 
    private int imgX = 0; 
    private int imgY = 0; 

    public Game(BufferedImage dbimage) { 
     this.dbimage = dbimage; 
    } 

    // draw within the paintComponent method, not the paint method 
    @Override 
    protected void paintComponent(Graphics g) { 
     // call the super's method to all painting to chain down the line 
     super.paintComponent(g); 
     if (dbimage != null) { 
      g.drawImage(dbimage, imgX, imgY, this); 
     } 
    } 

    // set the preferred size of the main Game JPanel safely 
    @Override 
    public Dimension getPreferredSize() { 
     if (isPreferredSizeSet()) { 
      return super.getPreferredSize(); 
     } 
     return new Dimension(PREF_W, PREF_H); 
    } 

    private static void createAndShowGui() { 
     // get your image as a resource 
     URL resource = Game.class.getResource(RESOURCE_PATH); 
     BufferedImage img = null; 
     try { 
      // read in using ImageIO 
      img = ImageIO.read(resource); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      System.exit(-1); 
     } 

     // pass image into your Game JPanel 
     Game mainPanel = new Game(img); 

     // pass the JPanel into a JFrame 
     JFrame frame = new JFrame("Game"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); // and display it 
    } 

    public static void main(String[] args) { 
     // start your Swing GUI in a thread-safe manner 
     SwingUtilities.invokeLater(() -> { 
      createAndShowGui(); 
     }); 
    } 
} 

下一個查找鍵綁定可幫助您使用此GUI的鍵擊動畫