Forum Stats

  • 3,816,047 Users
  • 2,259,134 Discussions
  • 7,893,369 Comments

Discussions

GetGraphics() in Java Application

PandaStyle
PandaStyle Member Posts: 1
edited Sep 4, 2017 11:57AM in Java Programming

Hey Guys,

I am trying to better myself in Java. So i basically porgrammed the snake game as an applet and transfered it to an application just to understand the differences between applets and applications. I manage now to do the change from applet to application but now i have a nullpointerexception at the line where I call getGraphics();

So you can see and understand better what i mean i have copied my come code here.

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Frame;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.awt.event.WindowEvent;

import java.awt.event.WindowListener;

public class SnakeGame extends Frame implements WindowListener, Runnable, KeyListener {

/**

*

*/

private static final long serialVersionUID = 1L;

Graphics gfx;

Image img;

Thread thread;

Snake snake;

Token token;

boolean gameOver;

public static void main(String[] args){

SnakeGame sg = new SnakeGame();

sg.setSize(400, 400);

sg.setVisible(true);

sg.setLayout(new FlowLayout());

sg.setResizable(false);

}

public SnakeGame() {

this.resize(400, 400);

gameOver = false;

img = createImage(400, 400);

gfx = img.getGraphics();

this.addKeyListener(this);

this.addWindowListener(this);

snake = new Snake();

token = new Token(snake);

thread = new Thread(this);

thread.start();

}

public void paint(Graphics g){

gfx.setColor(Color.black);

gfx.fillRect(0, 0, 400, 400);

if(!gameOver){

snake.draw(gfx);

token.draw(gfx);

gfx.drawString("Score " + token.getScore(), 2, 12);

}

else{

gfx.setColor(Color.red);

gfx.drawString("Game Over", 180, 150);

gfx.drawString("Score: " + token.getScore(), 180, 170);

}

g.drawImage(img, 0, 0, null);

}

public void update(Graphics g) {

paint(g);

}

public void repaint(Graphics g) {

paint(g);

}

public void run() {

for (;;) {

if (!gameOver) {

snake.move();

this.checkGameOver();

token.snakeCollision();

}

this.repaint();

try {

Thread.sleep(40);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

public void checkGameOver() {

if (snake.getX() < 0 || snake.getX() > 396) {

gameOver = true;

}

if (snake.getY() < 0 || snake.getY() > 396) {

gameOver = true;

}

if (snake.snakeCollision()) {

gameOver = true;

}

}

public void keyTyped(KeyEvent e) {

}

public void keyPressed(KeyEvent e) {

if (!snake.isMoving()) {

if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_RIGHT

|| e.getKeyCode() == KeyEvent.VK_DOWN) {

snake.setIsMoving(true);

}

}

if (e.getKeyCode() == KeyEvent.VK_UP) {

if (snake.getYDir() != 1) {

snake.setYDir(-1);

snake.setXDir(0);

}

}

if (e.getKeyCode() == KeyEvent.VK_DOWN) {

if (snake.getYDir() != -1) {

snake.setYDir(1);

snake.setXDir(0);

}

}

if (e.getKeyCode() == KeyEvent.VK_LEFT) {

if (snake.getXDir() != 1) {

snake.setXDir(-1);

snake.setYDir(0);

}

}

if (e.getKeyCode() == KeyEvent.VK_RIGHT) {

if (snake.getXDir() != -1) {

snake.setXDir(1);

snake.setYDir(0);

}

}

}

public void keyReleased(KeyEvent e) {

}

@Override

public void windowOpened(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowClosing(WindowEvent e) {

dispose();

System.exit(0);

}

@Override

public void windowClosed(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowIconified(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowDeiconified(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowActivated(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowDeactivated(WindowEvent e) {

// TODO Auto-generated method stub

}

}

I hope you guys can help me and tell me where my error is.

Thanx in Advance,

Ricci

Answers

  • Unknown
    edited Sep 4, 2017 11:57AM

    Welcome to the forums!

    I am trying to better myself in Java.

    Good - that is going to be a lifelong process.

    The place to start learning Java is by taking a tutorial or finding a good book.

    The Java Tutorials has trails on ALL of the basic Java functionality including graphics.

    https://docs.oracle.com/javase/tutorial/2d/index.html

     So i basically porgrammed the snake game as an applet and transfered it to an application just to understand the differences between applets and applications. I manage now to do the change from applet to application but now i have a nullpointerexception at the line where I call getGraphics();

    Well - thanks for posting the code but if you look at what you posted and you will see that you did NOT POST ANYTHING that shows an exception occurred or post the actual stacktrace.

    When you post you need to SHOW US, not just tell us:

    1. WHAT you do

    2. HOW you do it

    3. WHAT results you get

    The stacktrace will show you where the exception occurred and other useful info

    Did you look at the lines where you said the exception occurred?

    img = createImage(400, 400);gfx = img.getGraphics();

    If 'img.getGraphics' throws the NPE then 'img' is likely NULL. So look at the line above where it says 'createImage'. That means you need to RTFM to see what 'createImage' does:

    https://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#createImage(int,%20int)

    Returns:
    an off-screen drawable image, which can be used for double buffering. The return value may be null if the component is not displayable. This will always happen if GraphicsEnvironment.isHeadless() returns true.
    See where it says 'return value may be null'? See where your exception says you are getting 'null'.
    Put the two together and what do you have?
    Use an IDE such as NetBeans when you test. Step through the code ONE LINE AT A TIME so you can see what every line of code does and the results you get.
    What do you see after you execute that 'img = createImage(400,400)?
This discussion has been closed.