2014-01-27 5 views
0

Im 이미지를 Java로로드하려고하지만 코드를 실행할 때 내 JFrame에 아무 것도 나타나지 않습니다.Java (BlueJ)에서 캔트로드 이미지

방식의 메신저 내 주에서 내 이미지 함수를 호출하고 : 다음

import java.awt.*;    // Graphics stuff from the AWT library, here: Image 
    import java.io.File;   // File I/O functionality (for loading an image) 
    import javax.swing.ImageIcon; // All images are used as "icons" 



    public class GameImage 
    { 
    public static Image loadImage(String imagePathName) { 

    // All images are loades as "icons" 
    ImageIcon i = null; 

    // Try to load the image 
    File f = new File(imagePathName); 


    if(f.exists()) { // Success. Assign the image to the "icon" 
     i = new ImageIcon(imagePathName); 
    } 
    else {   // Oops! Something is wrong. 
     System.out.println("\nCould not find this image: "+imagePathName+"\nAre file  name and/or path to the file correct?");    
     System.exit(0); 
    } 

    // Done. Either return the image or "null" 
    return i.getImage(); 

} // End of loadImages method 

} 

를 그리고 여기 전화 :

 GI_Background = GameImage.loadImage("Images//background.jpg"); 
    GI_DuskyDolphin = GameImage.loadImage("Images//DuskyDolphin.jpg"); 

을이 나는 기꺼이를 제공 할 것이다 충분한 정보가없는 경우 코드의 나머지 부분 :) 감사합니다

+0

이미지로 수행 할 작업을 표시합니다. 이미지 표시 방법을 보여줍니다. (아마) 프로그램이이 코드를 종료하지 않을 것이기 때문에 아마 괜찮을 것입니다. – Radiodef

+0

좋아,이 이미지는'GI_Background'이지만, 어떻게 할 것인가? 레이블에 추가합니까, 페인트합니까? 네가 그걸로 아무 것도하지 않는다는 것을 나는 안다. –

+0

배포 시점까지는 해당 리소스가 [태그 : 포함 리소스]가 될 수 있습니다. 그렇다면 리소스는'File' 대신'URL'에 의해 접근되어야합니다. 'URL'을 구성하는 방법은 태그에 대한 [info page] (http://stackoverflow.com/tags/embedded-resource/info)를 참조하십시오. –

답변

0

나는 당신이 달성하려고 시도하는 것이 확실하지 않습니다 ... 방법으로 이미지를로드하려면이 것입니다 충분 :

private Image loadImage(String fileName){ 
    return (new ImageIcon(fileName)).getImage(); 
} 

그런 다음 이미지가 배경 인 JLabel을 만듭니다.

ImageIcon image = getImage(filename);  

JLabel imageLabel = new JLabel(image); 
imageLabel.setSize(image.getIconHeight, image.getIconWidth); 

JFrame frame = new JFrame(); 
frame.add(imageLabel); 

이 시도하고 내가 당신을 위해 작동하지 않는 경우 다시 AKS 부담없이, 또는 이미지가 응용 프로그램의 일부인 경우 그게 당신이

1

:) 싶어하지,는 사용하지 마십시오 파일하지만 자바 리소스 메커니즘을 사용하십시오.

URL imageUrl = getClass().getResource("/Images/background.jpg"); 
    return new ImageIcon(imageURL).getImage(); 

리소스 URL이 없으면 null을 반환합니다. 응용 프로그램이 .jar로 압축 된 경우 7zip/WinZip 등으로 열 수 있으며 경로를 확인할 수 있습니다. 대/소문자를 구분해야하며 / (백 슬래시 아님)을 사용해야합니다.

관련 문제