2010-11-25 5 views
1

나는 다음과 같은 코드가 있습니다JPanel의

class ImagePanel extends JPanel { 

      private Image img; 

      public ImagePanel(String img) { 
      this(new ImageIcon(img).getImage()); 
      } 

      public ImagePanel(Image img) { 
      this.img = img; 
      Dimension size = new Dimension(img.getWidth(null), img.getHeight(null)); 
      setPreferredSize(size); 
      setMinimumSize(size); 
      setMaximumSize(size); 
      setSize(size); 
      setLayout(null); 
      } 

      public void paintComponent(Graphics g) { 
      g.drawImage(img, 0, 0, null); 
      } 
     } 

다음과 같은 : 나는이 코드를 실행하면

public GUIVenDetails() throws MalformedURLException, IOException{ 

     mapPanel = new ImagePanel("http://www.netstate.com/states/symb/gamebirds/images/wild_turkey.jpg"); 
     mapPanel.setPreferredSize(new Dimension(200,200)); 
     mapPanel.setMinimumSize(mapPanel.getPreferredSize()); 
     mapPanel.setMaximumSize(mapPanel.getPreferredSize()); 


     add(mapPanel); 
    } 

public static void main(String[] args) throws MalformedURLException, IOException, XPathExpressionException, SAXException, ParserConfigurationException { 


     GUIVenDetails gui = new GUIVenDetails(); 

     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     Dimension windowSize = gui.getSize(); 

     int windowX = Math.max(0, (screenSize.width - windowSize.width)/2); 
     int windowY = Math.max(0, (screenSize.height - windowSize.height)/2); 

     JFrame f=new JFrame(); 
     f.setSize(new Dimension(400,800)); 
     f.setLocation(windowX, windowY); 

     f.add(gui); 
     f.setVisible(true); 
    } 

, 나는 ... 왜 흰색을 제외한 아무것도 표시되지 않습니다를 이거 야?

답변

2

인터넷에서 파일을 읽는 방법이 아닙니다. 같은 시도 :

String imagePath = "http://duke.kenai.com/misc/Bullfight.jpg"; 
Image image = null; 

try 
{ 
    URL url = new URL(imagePath); 
    image = ImageIO.read(url); 
} 
catch (IOException e) 
{ 
    System.out.println(e); 
} 

을 또는 당신은 여전히 ​​

new ImageIcon(new URL(...)); 

을 사용할 수 있습니다하지만 "HTTP"로 시작해서 문자열이 URL이되지 않습니다.

관련 문제