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);
}
}

すべてが正常に機能し、フレームを作成して 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)

誰かが何かを言う前に、イメージがそこにあり、うまく機能します。どうしたの?

Frog と Fly クラスはわかりやすいです。

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(){

}
 }
4

1 に答える 1

2

環境パネルをローカル変数として作成しました。クラス変数はnullです。

コードは次のようになります。

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

また、nullレイアウトを使用しないでください。適切なレイアウトマネージャーを使用して、コンポーネントをレイアウトします。

于 2013-02-11T16:51:24.797 に答える