Quantcast

Problem with Programming a little game for my Presentation

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Problem with Programming a little game for my Presentation

StyLzTF
Hello i got a problem. This is the code i used in netbeans and now i get a error after i wantet to get a backround picture in my game.:

package de.tanks;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 *
 * @author Fabian/Phillip
 */
public class panel extends JPanel{

  public static final String IMAGE_DIR = "Users\\Fabian\\Documents\\Java\\Tanks\\src\\de\\tanks\\maps";
   
  private final Dimension prefSize = new Dimension(1180, 780);
   
  private ImageIcon backgroundImage;
  private final String[] backgroundImages = new String [] {"bg_mud.jpg",
                                                          "bg_snow.jpg",
                                                          "bg_san};d.jpg"};
   
  private boolean gameOver = false;
  private int tanksDestroyedCounter = 0;
   
  private Timer t;  
   
   
  public panel() {        
    setFocusable(true);
    setPreferredSize(prefSize);
       
    initGame();        
    startGame();
  }
   
   
  public boolean isGameOver() {
    return gameOver;
  }

  public void setGameOver(boolean gameOver) {
    this.gameOver = gameOver;
  }
   
  private void initGame () {
    setBackgroundImage(1);
    createGameObjects();
       
    t = new Timer(20, (ActionEvent e) -> {
        doOnTick();
    });
  }
   
  private void createGameObjects() {
    // hier werden wir später die Spielobjekte erzeugen
  }
   
  private void initPlayersTank() {        
    // hier werden wir den Panzer des Spielers initialisieren
  }
   
  public void setBackgroundImage(int imageNumber) {
    String imagePath = IMAGE_DIR + backgroundImages[imageNumber];
    URL imageURL = getClass().getResource(imagePath);        
    backgroundImage = new ImageIcon(imageURL);
  }
   
  private void startGame() {
    t.start();
  }
   
  public void pauseGame() {
    t.stop();
  }
   
  public void continueGame() {
    if (!isGameOver()) t.start();
  }
   
  public void restartGame() {
    tanksDestroyedCounter = 0;
    setGameOver(false);
    createGameObjects();
    startGame();
  }
   
  private void endGame() {
    setGameOver(true);
    pauseGame();
  }
   
  private void doOnTick() {        
    tanksDestroyedCounter++;
    if (tanksDestroyedCounter > 2015) endGame();
       
    repaint();
  }
   
  @Override
  public void paintComponent (Graphics g) {
    super.paintComponent(g);
       
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
           
    backgroundImage.paintIcon(null, g, 0, 0);
               
    g.setFont(new Font(Font.MONOSPACED, Font.BOLD, 19));            
    g.setColor(Color.BLUE);
    g.drawString("Tanks destroyed: " + tanksDestroyedCounter, 22, prefSize.height-5);        
               
    if (isGameOver()) {
       g.setFont(new Font(Font.MONOSPACED, Font.BOLD, 50));
       g.setColor(Color.RED);
       g.drawString("GAME OVER!", prefSize.width/2 - 130, prefSize.height/5);
    }
  }
   
}

Error : Exception in thread "main" java.lang.NullPointerException
        at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
        at de.tanks.panel.setBackgroundImage(panel.java:73)
        at de.tanks.panel.initGame(panel.java:54)
        at de.tanks.panel.<init>(panel.java:40)
        at de.tanks.IngameWindow.<init>(IngameWindow.java:21)
        at de.tanks.tanks.main(tanks.java:7)


Can pls someone help me out i have to do this till sunday :([/img]




Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Problem with Programming a little game for my Presentation

tkellerer

StyLzTF wrote:
> Hello i got a problem. This is the code i used in netbeans and now i get a error after i wantet to get a backround picture in my game

This is not the right place to ask for programming help.
This forum is about the usage of the NetBeans IDE, not for general programming questions.


> Error : Exception in thread "main" java.lang.NullPointerException
> at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
> at de.tanks.panel.setBackgroundImage(panel.java:73)
> at de.tanks.panel.initGame(panel.java:54)
> at de.tanks.panel.<init>(panel.java:40)
> at de.tanks.IngameWindow.<init>(IngameWindow.java:21)
> at de.tanks.tanks.main(tanks.java:7)
>

See here: http://stackoverflow.com/q/218384/330315




Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with Programming a little game for my Presentation

Sean Carrick-4
In reply to this post by StyLzTF
Looking at your code:

public static final String IMAGE_DIR =
"Users\\Fabian\\Documents\\Java\\Tanks\\src\\de\\tanks\\maps";

You would want to change that to:

public static final String IMAGE_DIR = "\\de\\tanks\\maps\\";

Stay within your package structure, because you don't know where the
application will be installed on a user's machine. Also, you are
appending the file name to the end of your path, but you're not putting
in the file separator between the final folder and the file.

As a piece of advice, I wouldn't use the `\\` for the file separator.
The best practice is to have an effectively final variable that holds
the system's file separator character:

public final String FILE_SEP = System.getProperty("file.separator");

By doing this, your application will be using the system file separator
("\" on Windows, "/" on Linux, Solaris and Mac OS-X).


On 04/12/2017 08:20 AM, StyLzTF wrote:

> Hello i got a problem. This is the code i used in netbeans and now i get a error after i wantet to get a backround picture in my game.:
>
> package de.tanks;
>
> import java.awt.Color;
> import java.awt.Dimension;
> import java.awt.Font;
> import java.awt.Graphics;
> import java.awt.Graphics2D;
> import java.awt.RenderingHints;
> import java.awt.event.ActionEvent;
> import java.net.URL;
> import javax.swing.ImageIcon;
> import javax.swing.JPanel;
> import javax.swing.Timer;
>
> /**
>   *
>   * @author Fabian/Phillip
>   */
> public class panel extends JPanel{
>
>    public static final String IMAGE_DIR = "Users\\Fabian\\Documents\\Java\\Tanks\\src\\de\\tanks\\maps";
>      
>    private final Dimension prefSize = new Dimension(1180, 780);
>      
>    private ImageIcon backgroundImage;
>    private final String[] backgroundImages = new String [] {"bg_mud.jpg",
>                                                            "bg_snow.jpg",
>                                                            "bg_san};d.jpg"};
>      
>    private boolean gameOver = false;
>    private int tanksDestroyedCounter = 0;
>      
>    private Timer t;
>      
>      
>    public panel() {
>      setFocusable(true);
>      setPreferredSize(prefSize);
>          
>      initGame();
>      startGame();
>    }
>      
>      
>    public boolean isGameOver() {
>      return gameOver;
>    }
>
>    public void setGameOver(boolean gameOver) {
>      this.gameOver = gameOver;
>    }
>      
>    private void initGame () {
>      setBackgroundImage(1);
>      createGameObjects();
>        
>      t = new Timer(20, (ActionEvent e) -> {
>          doOnTick();
>      });
>    }
>      
>    private void createGameObjects() {
>      // hier werden wir später die Spielobjekte erzeugen
>    }
>      
>    private void initPlayersTank() {
>      // hier werden wir den Panzer des Spielers initialisieren
>    }
>      
>    public void setBackgroundImage(int imageNumber) {
>      String imagePath = IMAGE_DIR + backgroundImages[imageNumber];
>      URL imageURL = getClass().getResource(imagePath);
>      backgroundImage = new ImageIcon(imageURL);
>    }
>      
>    private void startGame() {
>      t.start();
>    }
>      
>    public void pauseGame() {
>      t.stop();
>    }
>      
>    public void continueGame() {
>      if (!isGameOver()) t.start();
>    }
>      
>    public void restartGame() {
>      tanksDestroyedCounter = 0;
>      setGameOver(false);
>      createGameObjects();
>      startGame();
>    }
>      
>    private void endGame() {
>      setGameOver(true);
>      pauseGame();
>    }
>      
>    private void doOnTick() {
>      tanksDestroyedCounter++;
>      if (tanksDestroyedCounter > 2015) endGame();
>          
>      repaint();
>    }
>      
>    @Override
>    public void paintComponent (Graphics g) {
>      super.paintComponent(g);
>          
>      Graphics2D g2d = (Graphics2D) g;
>      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
>            
>      backgroundImage.paintIcon(null, g, 0, 0);
>                  
>      g.setFont(new Font(Font.MONOSPACED, Font.BOLD, 19));
>      g.setColor(Color.BLUE);
>      g.drawString("Tanks destroyed: " + tanksDestroyedCounter, 22, prefSize.height-5);
>                  
>      if (isGameOver()) {
>         g.setFont(new Font(Font.MONOSPACED, Font.BOLD, 50));
>         g.setColor(Color.RED);
>         g.drawString("GAME OVER!", prefSize.width/2 - 130, prefSize.height/5);
>      }
>    }
>      
> }
>
> Error : Exception in thread "main" java.lang.NullPointerException
> at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
> at de.tanks.panel.setBackgroundImage(panel.java:73)
> at de.tanks.panel.initGame(panel.java:54)
> at de.tanks.panel.<init>(panel.java:40)
> at de.tanks.IngameWindow.<init>(IngameWindow.java:21)
> at de.tanks.tanks.main(tanks.java:7)
>
>
> Can pls someone help me out i have to do this till sunday :([/img]
>
>
>
>

Loading...