2015-10-27 222 views
1

我試圖在JPanel上繪製.png。我使用ImageIcon構造函數將其導入,並將其繪製在我的自定義面板的paintComponent中。ImageIcon.paintIcon不會在JPanel上繪製圖像

sscce

package mypackage; 

import java.awt.Graphics; 

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

public class MyPanel extends JPanel { 

    static JFrame frame; 
    static MyPanel panel; 
    static ImageIcon icon; 

    public static void main(String[] args) { 
     icon = new ImageIcon(MyPanel.class.getResource("MyImage.png")); 
     frame = new JFrame(); 
     panel = new MyPanel(); 
     frame.setSize(500, 500); 
     frame.add(panel); 
     frame.setVisible(true); 
     frame.repaint(); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     icon.paintIcon(panel, g, 100, 100); 
    } 
} 

我預想的形象,這僅僅是在白色背景上一對夫婦的形狀,在(100, 100)面板上顯示。相反,一個空白屏幕:

Blank panel where I expected my image to be drawn

沒有發生錯誤的事實意味着該程序正確查找文件。

的形象在我的Eclipse項目在同一個包中的類:

Project setup

這究竟是爲什麼?我如何解決它?

+1

不要建立在你的paintComponent方法的ImageIcon。也可以嘗試在面板中繪製一個圓圈或其他東西。 – matt

+0

@matt將'icon = new ImageIcon(MyPanel.class.getResource(「MyImage.png」));'移到'main'並在'main'上面添加'靜態ImageIcon圖標'並不能解決問題。 – snickers10m

+0

@matt此外,添加'((Graphics2D)g).draw(新的Ellipse2D.Double(100,100,100,100));''paintComponent'成功繪製一個圓圈。 – snickers10m

回答

3

由於代碼看起來正確,我建議資源未被正確加載。

將png文件放在你的類路徑中。例如。我會有一個目錄:

~/ProjectRoot/resources/mypackage/ 

然後在類路徑中包含資源。 In eclipse you can setup the classpath via

項目 - >屬性 - > Java構建路徑 - >添加類文件夾

BufferedImage img = 
    ImageIO.read(MyPanel.class.getResourceAsStream("MyImage.png")); 

,如果未找到該圖片引發異常。您可以使用它來製作一個ImageIcon。

2

當您使用ImageIcon從文件讀取圖像時,不會指示讀取是否成功。

這是你的GUI,我讀出的圖像使用的ImageIO:

Image on GUI

而這裏的形象,在同一個目錄中的Java源:

enter image description here

這裏是你的代碼,我使用ImageIO讀取來讀取圖像,並在paintComponent方法中繪製圖像。

package com.ggl.testing; 

import java.awt.Graphics; 
import java.awt.Image; 
import java.io.IOException; 

import javax.imageio.ImageIO; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class MyPanel extends JPanel { 

    private static final long serialVersionUID = -9008812738915944216L; 

    private static JFrame frame; 
    private static MyPanel panel; 
    private static Image image; 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       image = getImage(); 
       frame = new JFrame(); 
       panel = new MyPanel(); 
       frame.setSize(500, 500); 
       frame.add(panel); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.drawImage(image, 100, 100, MyPanel.this); 
    } 

    private static Image getImage() { 
     Image image = null; 
     try { 
      image = ImageIO.read(MyPanel.class.getResource("maze.jpg")); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return image; 
    } 
}