2013-02-19 79 views
0

我真的不知道我做错了什么。我正在制作一款状态游戏,所以我可以帮助人们学习,但是当我运行它时它可以工作,但它不会将图像返回到屏幕上。这是代码的主要位,我有我的阵列...我不断收到一个字符串返回null ...

public void checkCorrectValue(String guess){ 
    if(guess.equalsIgnoreCase(current_state)){ 
     correct_value = true; 
    }else{ 
     correct_value = false; 
    } 
} 
public void setRandomState(){ 
    r = new Random(); 
    int i; 
    i = r.nextInt(50); 
    System.out.println(i); 
    current_state = states[i]; 
    System.out.println(current_state + " was randomly selected"); 
} 
public String getCurrentState(){ 
    return current_state; 
} 
public boolean isStateCorrect(){ 
    return correct_value; 
} 

我添加了我的数组称为状态并填充所有状态。它确实成功输出正确的状态,所以我知道这个工作。这是我的面板代码...

States_Images us_img; 
USA_States us_state; 

public Menu(){ 
    setLayout(null); 
    setBackground(Color.WHITE); 

    us_img = new States_Images(); 
    us_state = new USA_States(); 

    us_state.setRandomState(); 
    System.out.println(us_state.getCurrentState()); 

    JLabel l = new JLabel(); 
    l.setIcon(us_img.getCurrentStateImage()); 
    l.setBounds(0,0,500,500); 
    add(l); 
} 

它所做的就是把我的东西面板然后尝试了通过将随机状态首先调用setRandomState()把图像,它出来把正确那里。但是当us_img.getCurrentStateImage()运行时...

private ImageIcon currentstate_image; 
private String current_state; 
USA_States us_states; 

public States_Images(){ 
    us_states = new USA_States(); 
} 

public ImageIcon getCurrentStateImage(){ 
    setStateName(); 
    currentstate_image = new ImageIcon("graphics\\" + current_state + ".png"); 
    System.out.println(current_state + " image loaded"); 
    return currentstate_image; 
} 
private void setStateName(){ 
    current_state = us_states.getCurrentState(); 
} 

它打印出“null image loaded”。然后在我的框架上没有任何显示我真的不知道我做错了什么,因为我以前做过这样的事情。我知道这是基本的Java所以任何帮助将真正appriciated!提前致谢。

+0

这取决于图像的存储位置。从你的代码中,它说图像存储在磁盘上的'。\ graphics \ {name} .png'上。 1-它们存在吗? 2-他们是否想嵌入? – MadProgrammer 2013-02-19 04:34:20

+0

我知道图像存在,因为我已经在菜单代码中将它们“手动”直接称为JLabel。所以我知道图像可以加载和显示。 – tyty5949 2013-02-19 04:41:17

+0

然后,我会确保'String'' current_state'与图像名称的匹配,并且变量设置正确 – MadProgrammer 2013-02-19 05:04:47

回答

0

看起来您在创建ImageIcon的新实例时没有传递有效的文件名。

currentstate_image = new ImageIcon("graphics\\" + current_state + ".png"); 

按照ImageIcon的javadoc,您需要通过您要创建的ImageIcon的文件名(路径)。

您可能希望确保该文件确实存在下./graphics/{current_state}.png

另外,javadoc中说,尽可能使用斜杠。

编辑

System.out.println(current_state + " image loaded"); 

你检查,如果你的current_state不为空在这一点? 如果您正在获取“空图像加载”,这意味着current_state变量是null。检查你的us_states.getCurrentState();方法。

发布您的USA_States类也可能有帮助。

如果您尝试使用相同的us_states实例从Menu类以上, 你将不得不从Menu类的us_states传递给States_Images类。

下面是修改后的代码: 菜单类:

public Menu(){ 
    setLayout(null); 
    setBackground(Color.WHITE); 

    us_state = new USA_States(); 
    us_state.setRandomState(); 

    us_img = new States_Images(us_state); //Pass us_state to States_Images 
    System.out.println(us_state.getCurrentState()); 

    JLabel l = new JLabel(); 
    l.setIcon(us_img.getCurrentStateImage()); 
    l.setBounds(0,0,500,500); 
    add(l); 
} 

States_Images类:

private ImageIcon currentstate_image; 
private String current_state; 
USA_States us_states; 

public States_Images(USA_States us_state){ 
    this.us_states = us_state 
} 

public ImageIcon getCurrentStateImage(){ 
    setStateName(); 
    currentstate_image = new ImageIcon("graphics\\" + current_state + ".png"); 
    System.out.println(current_state + " image loaded"); 
    return currentstate_image; 
} 
private void setStateName(){ 
    current_state = us_states.getCurrentState(); 
} 
+0

我这样做,当我运行它仍然说在getCurrentStateImage()方法中加载的空图像。我得到了图像部分的权利,因为我可以“手动”将图像加载到菜单类中的jlabel – tyty5949 2013-02-19 04:46:55

相关问题