2013-02-11 38 views
0

所以我做了這個類揮杆添加圖片不能正常工作

public class ImagePanel extends JPanel { 
BufferedImage lionImage; 

public ImagePanel(){ 
    try { 
     lionImage = ImageIO.read (new File ("imgres.jpg")); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

@Override 
public void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    if (lionImage != null) { 
     g.drawImage(lionImage, 0, 0, this); 
    } 
} 

public BufferedImage getLionImage() { 
    return lionImage; 
} 

public void setLionImage(BufferedImage lionImage) { 
    this.lionImage = lionImage; 
} 
} 

然後我做了一個測試類

public class test { 

public static void main (String [] args) throws Exception { 
ImagePanel test = new ImagePanel(); 
JFrame frame = new JFrame ("Image"); 

frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
frame.getContentPane().setLayout (new BorderLayout()); 

frame.add(test); 

frame.pack(); 
frame.setVisible (true); 
} 
} 

一切正常,簡單的make框架做出ImagePanel,將其添加到幀。

然後我嘗試了我的實際工作。

public class Enviroment implements Runnable, ActionListener{ 
private JFrame frame; 
private JPanel enviromentPanel,totalGUI,enviromentButtonPanel; 
private JButton newFrogButton, resetButton, hungryButton; 
private JTextField enterName; 
private JLabel hungryLabel; 
private ArrayList<Frog> frogs = new ArrayList<Frog>(); 
private ArrayList<Fly> flys = new ArrayList<Fly>(); 



public Enviroment(){ 
JFrame.setDefaultLookAndFeelDecorated(true); 
    JFrame frame = new JFrame("[=] Hungry Cyber Pet [=]"); 

    frame.setContentPane(runEnviroment()); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(740, 800); 
    frame.setVisible(true); 

} 



public JPanel runEnviroment(){ 

JPanel totalGUI = new JPanel(); 
totalGUI.setLayout(null); 

JPanel enviromentPanel = new JPanel(); 
enviromentPanel.setLayout(null); 
enviromentPanel.setLocation(10, 10); 
enviromentPanel.setSize(700, 700); 
enviromentPanel.setBackground(Color.WHITE); 
totalGUI.add(enviromentPanel); 


JPanel enviromentButtonPanel = new JPanel(); 
FlowLayout experimentLayout = new FlowLayout(); 
enviromentButtonPanel.setLayout(experimentLayout); 
enviromentButtonPanel.setLocation(10, 710); 
enviromentButtonPanel.setSize(700, 50); 
totalGUI.add(enviromentButtonPanel); 

newFrogButton = new JButton("New Frog"); 
newFrogButton.setLocation(0, 0); 
newFrogButton.setSize(120, 30); 
newFrogButton.addActionListener(this); 
enviromentButtonPanel.add(newFrogButton); 

enterName = new JTextField("Enter name"); 
enterName.setLocation(140,0); 
enterName.setSize(120,30); 
enviromentButtonPanel.add(enterName); 

hungryButton = new JButton("Hungry!"); 
hungryButton.setLocation(280, 0); 
hungryButton.setSize(120, 30); 
hungryButton.addActionListener(this); 
enviromentButtonPanel.add(hungryButton); 

resetButton = new JButton("Reset"); 
resetButton.setLocation(420, 0); 
resetButton.setSize(120, 30); 
resetButton.addActionListener(this); 
enviromentButtonPanel.add(resetButton); 


totalGUI.setOpaque(true); 

return totalGUI; 
} 
public void draw(){ 
ImagePanel frogImage = new ImagePanel(); 
enviromentPanel.add(frogImage); 
} 
public void actionPerformed(ActionEvent e) { 
if(e.getSource() == newFrogButton){ 
    Frog frog = new Frog(enterName.getText()); 
    frogs.add(frog); 
    draw(); 
    System.out.println(frogs); 
    Fly fly = new Fly(); 
    flys.add(fly); 
    System.out.println(flys); 
    } 
    else if(e.getSource() == hungryButton){ 
    } 
    else if(e.getSource() == resetButton){ 
     frogs.clear(); 
     flys.clear(); 
     System.out.println(frogs); 
     System.out.println(flys); 


    } 
} 

這在執行draw()方法時顯示錯誤。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
at Enviroment.draw(Enviroment.java:91) 
at Enviroment.actionPerformed(Enviroment.java:97) 
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
at java.awt.Component.processMouseEvent(Unknown Source) 
at javax.swing.JComponent.processMouseEvent(Unknown Source) 
at java.awt.Component.processEvent(Unknown Source) 
at java.awt.Container.processEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Window.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$000(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.run(Unknown Source) 

在任何人說什麼之前,圖像在那裏,它工作正常。怎麼了?

青蛙和蒼蠅的類是平原。

public class Fly { 
private int xPosition; 
private int yPosition; 
private boolean eaten; 

public Fly(){ 
    Random randomGenerator = new Random(); 
    xPosition = randomGenerator.nextInt(100); 
    yPosition = randomGenerator.nextInt(100); 
    eaten = false; 
} 

public int getxPosition() { 
    return xPosition; 
} 

public void setxPosition(int xPosition) { 
    this.xPosition = xPosition; 
} 

public int getyPosition() { 
    return yPosition; 
} 

public void setyPosition(int yPosition) { 
    this.yPosition = yPosition; 
} 

public boolean isEaten() { 
    return eaten; 
} 

public void setEaten(boolean eaten) { 
    this.eaten = eaten; 
} 

public void move(){ 
     Random randomGenerator = new Random(); 

     int xChange = -10 + randomGenerator.nextInt(20); 
     int yChange = -10 + randomGenerator.nextInt(20); 
     xPosition = xPosition + xChange; 
     yPosition = yPosition + yChange; 
     move(); 
} 

@Override 
public String toString() { 
    return "Fly [xPosition=" + xPosition + ", yPosition=" + yPosition 
      + ", eaten=" + eaten + "]"; 
} 
} 

public class Frog { 
@Override 
public String toString() { 
    return "Frog [xPosition=" + xPosition + ", yPosition=" + yPosition 
      + ", name=" + name + ", hungry=" + hungry + "]"; 
} 
private int xPosition; 
private int yPosition; 
private BufferedImage lionImage=null; 
private String name; 
private boolean hungry; 

public Frog(String newName){ 
    Random randomGenerator = new Random(); 
    xPosition = randomGenerator.nextInt(100); 
    yPosition = randomGenerator.nextInt(100); 
    name = newName; 
    hungry = false; 
    getImage(); 
} 
public void getImage(){ 
    try{ 
    lionImage =ImageIO.read(new File("imgres.jpg")); 
    }catch (IOException e){} 
    } 
public void move(){ 
    Random randomGenerator = new Random(); 

    int xChange = -10 + randomGenerator.nextInt(20); 
    int yChange = -10 + randomGenerator.nextInt(20); 
    xPosition = xPosition + xChange; 
    yPosition = yPosition + yChange; 
    move(); 
} 
public void moveHungry(){ 

} 
public void eat(){ 

} 
public int getxPosition() { 
    return xPosition; 
} 
public void setxPosition(int xPosition) { 
    this.xPosition = xPosition; 
} 
public int getyPosition() { 
    return yPosition; 
} 
public void setyPosition(int yPosition) { 
    this.yPosition = yPosition; 
} 
public BufferedImage getLionImage() { 
    return lionImage; 
} 
public void setLionImage(BufferedImage lionImage) { 
    this.lionImage = lionImage; 
} 
public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public boolean isHungry() { 
    return hungry; 
} 
public void setHungry(boolean hungry) { 
    this.hungry = hungry; 
} 
public void bounce(){ 

} 
} 
+0

請發帖青蛙和飛行課也得到更多幫助 – 2013-02-11 16:31:19

+0

做青蛙和飛行都起來了。 – user1817988 2013-02-11 16:36:14

+0

我讓你的代碼工作。如果你仍然感興趣,那麼我可以給你答案? – 2013-02-12 07:32:54

回答

2

您將環境面板創建爲局部變量,類變量爲null。

你的代碼應該是:

// JPanel enviromentPanel = new JPanel(); 
enviromentPanel = new JPanel(); 
enviromentPanel.setLayout(null); 
enviromentPanel.setLocation(10, 10); 
enviromentPanel.setSize(700, 700); 

此外,不要使用空佈局。使用適當的佈局管理器來佈置組件。

+0

哦,這該死的簡單。非常感謝你,但是我希望你明白,當你累了,你整天都在看代碼時,很容易錯過這樣的東西。 – user1817988 2013-02-11 16:53:57

+0

好,所以現在沒有錯誤信息,但它沒有繪製圖像... – user1817988 2013-02-11 16:57:57

+0

哦,該面板只有1塊,所以是否有任何佈局需要?圖片將隨機添加 – user1817988 2013-02-11 17:04:21