handsomedan 0 Newbie Poster

I am having a couple of problems with my code. First, whenever I try to remove the JOptionPane from line 79 in my code, I get a NullPointerException from the line this.dbGraphics = this.dbImage.getGraphics(); a couple of lines down from the JOptionPane. Does anyone know why? Another problems is that I tried to convert this applet into a application program, and whenever I did so, I lost all of the score/info from the top of the screen. It's still there, but not all of it is visible for some reason. Why is that? Also, the main purpose of this project is to create a networked pong game. I want to create two other classes, hostpong and clientpong where the host holds all of the game logic, sends its paddle and ball location to the client, the client simply draws everything, and sends its paddle location back to the host (using udp datagram packets). Is this the best way to implement this? I will take any help, advice, code that you are willing to provide. Thank you for any help you may give!

*Need help resolving NullPointException, turning applet into proper application (w/out errors), networking.

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import java.io.*;
import java.awt.*;
import java.util.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;  
import javax.swing.event.*;
import javax.swing.JApplet;

public class SinglePlayer
extends JFrame 
implements KeyListener {
	// just a random variable to randomize some things
	static Random rand = new Random(System.currentTimeMillis());

	// the width & height for our applet
	public static int width = 500, height = 500;
	// will the user be playing against the computer or a person
	//public boolean twoPlayers = false;
	// is the game paused
	boolean paused = true;
	// what is the top boundary
	int top = 20;
	// information to be displayed
	static String info = "TECHNO PONG";
	static String pause1 = "blue player use a/z";
	static String pause2 = "red player use k/m";
	static String pause3 = "p to pause";
	// font to display normal info
	Font defFont = new Font("arial", 0, 12);
	
	// double buffering stuff
	Image dbImage;
	Graphics dbGraphics;
	
	// the ball
	Ball ball = new Ball(this, width/2, height/2, 2, 2, top, height);
	
	// the paddles
	Paddle leftPaddle = new Paddle(Paddle.TYPE_LEFT, 5, 
			height/2, top, height, 'a', 'z');
	Paddle rightPaddle = new Paddle(Paddle.TYPE_RIGHT, 
			width-Paddle.width-5, height/2, top, height, 'k', 'm');
	
	// scores
	int leftScore=0;
	int rightScore=0;
	// font used to display score
	Font scoreFont = new Font("Arial", 0, 18);
	
	/**
	 * Initialize method, it will start up pong
	 * and keep it going.
	 */
	
	public SinglePlayer()
	{
		init();
		setSize(width, height);
		setVisible(true);
		setResizable(false);
	}
	public void init() {
		// ask the user if they want to play against the computer
		int option = JOptionPane.showConfirmDialog(this, 
				"placeholder", 
				"Competition", JOptionPane.OK_OPTION);
		
		// set the size and background color
		this.setSize(width, height);
		this.getContentPane().setBackground(Color.black);
		
		// create the double buffering images/graphics
		this.dbImage = this.createImage(width, height);
		this.dbGraphics = this.dbImage.getGraphics();
		
		// give ability to catch keyboard input
		this.addKeyListener(this);
		
		// a new thread that keeps on updating our applet
		new Thread() {
			public void run() {
				while(true) {
					// do if paused
					if(!paused) {
						// update and paint our game often
						updatePong();
						paint(getGraphics());
						pause(5);
					} else {
						// paint our game only, we are paused
						paint(getGraphics());
						pause(100);
					}
				}
			}
		}.start();
	}

	/* updates the game */
	public void updatePong() {
		updateBall();
		updatePaddles();
	}
	
	/* update our ball */
	public void updateBall() {
		checkBallWithPaddle(ball, leftPaddle);
		checkBallWithPaddle(ball, rightPaddle);
		ball.update();
	}
	
	/* check to see if the given ball collided with the given paddle */
	public  void checkBallWithPaddle(Ball ball, Paddle paddle) {
		// make some temp variables for easy referencing
		int bx = ball.xLoc, by = ball.yLoc, bs = ball.size;
		int px = paddle.xLoc, py = paddle.yLoc;
		int pw = Paddle.width, ph = Paddle.height, sec = ph/5;
		
		boolean ballCollided=false;
		
		if(paddle.myType == Paddle.TYPE_LEFT) {
			if(isWithin(px, px+pw, bx, 0) && isWithin(py, py+ph, by, bs)) {
				ballCollided = true;
				ball.xLoc = px+pw;
			}
		} else {
			if(isWithin(px, px+pw, bx+bs, 0) && isWithin(py, py+ph, by, bs)) {
				ballCollided = true;
				ball.xLoc = px-bs;
			}
		}
		
		if(ballCollided) {
			// bounce the ball
			ball.bounceX();
			
			// randomize the bounce a little
			int change = rand.nextInt(4);
			int change2 = rand.nextInt(2)-1;
			
			// depending on where the ball hit the paddle
			// change the velocity a little
			if(by<=py+sec)
				ball.yVel -= change;
			else if(by<=py+(2*sec))
				ball.yVel -= (change/2);
			else if(by >= py+(3*sec))
				ball.yVel += (change/2);
			else if(by >= py+(4*sec))
				ball.yVel += change;
			else
				ball.yVel += change2*change;
		}
	}
	
	// check if the targed is within the given range with size buffer
	public static boolean isWithin(int min, int max, int target, int size) {
		if(min > max) {
			int temp = min;
			min = max;
			max = temp;
		}
		
		if(target+size>=min && target<=max)
			return true;
		return false;
	}
	
	public void updatePaddles() {
		leftPaddle.update();
		
		// pick which update to do, user or ai
		if(PongGui.twoplayers)
			rightPaddle.update();
		else
			rightPaddle.aiUpdate(ball);
	}
	
	/* paint function */
	public void paint(Graphics g) {
		super.paint(dbGraphics);
		
		drawBall();
		drawPaddles();
		drawScore();
		drawBoard();
		drawInfo();
		if(paused)
			drawPause();
				
		// update our screen
		update(g);
	}
	
	public void drawBall() {
		dbGraphics.setColor(Color.white);
		dbGraphics.fillOval(ball.xLoc, ball.yLoc, ball.size, ball.size);
	}
	
	public void drawPaddles() {
		dbGraphics.setColor(Color.blue);
		dbGraphics.fillRect(leftPaddle.xLoc, leftPaddle.yLoc, 
				Paddle.width, Paddle.height);
		
		dbGraphics.setColor(Color.red);
		dbGraphics.fillRect(rightPaddle.xLoc, rightPaddle.yLoc, 
				Paddle.width, Paddle.height);
	}
	
	public void drawScore() {
		dbGraphics.setFont(scoreFont);
		dbGraphics.setColor(Color.blue);
		dbGraphics.drawString(""+leftScore, 10, top-1);
		
		
		dbGraphics.setColor(Color.red);
		dbGraphics.drawString(""+rightScore, width-50, top-1);
	}
	
	public void drawBoard() {
		dbGraphics.setColor(Color.white);
		dbGraphics.drawLine(0, top, width, top);
	}
	
	public void drawInfo() {
		dbGraphics.setColor(Color.white);
		dbGraphics.setFont(scoreFont);
		dbGraphics.drawString(info, (width/2)-50, top-5);
	}
	
	public void drawPause() {
		dbGraphics.setFont(defFont);
		int x = width/2;
		int y = top+30;
		dbGraphics.setColor(Color.blue);
		dbGraphics.drawString(pause1, x-48, y-15);
		dbGraphics.setColor(Color.red);
		dbGraphics.drawString(pause2, x-48, y);
		dbGraphics.setColor(Color.white);
		dbGraphics.drawString(pause3, x-28, y+15);
	}
	
	public void update(Graphics g) {
		if(g == null)
			return;
		g.drawImage(this.dbImage, 0,0, this);
	}
	
	// gets called whena key is pressed
	public void keyPressed(KeyEvent e) {
		//send the key press to each of the paddles
		leftPaddle.handleKey(e.getKeyChar(), true);
		rightPaddle.handleKey(e.getKeyChar(), true);
	}
	
	// gets call when a key is released
	public void keyReleased(KeyEvent e) {
		// pause the game if need be
		if(e.getKeyChar() == 'p')
			paused = !paused;
		
		// send the signal to each of the paddles
		leftPaddle.handleKey(e.getKeyChar(), false);
		rightPaddle.handleKey(e.getKeyChar(), false);
	}
	
	// the ball went out
	public void ballOut(boolean outAtRight) {
		// on the left side
		if(ball.xLoc < 10)
			rightScore++;
		else// on the right side
			leftScore++;
		
		// reset the ball
		ball.reset();
		
		// pause the game
		paused = true;
	}
	
	// do nothing on type
	public void keyTyped(KeyEvent e) {;}
	
	// makes the program sleep for a little bit
	public static void pause(long millis) {
		try{Thread.sleep(millis);}
		catch(Exception e) {;}
	}
	
	public static void main(String[] args)
	{
		new SinglePlayer();
	}
}

*Note: Code was given to me to hack in certain new things.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.