Results 1 to 3 of 3

Thread: Learning Java

  1. #1
    jjoshua2 is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default Learning Java

    I completed a year long 3 quarters regiment in C# at our local community college. I am going to be transferring to a 4 year college majoring in CS, and they will be using Java. In order to get up to snuff, I am "doing" Stanford's CS106a class online. Watching the lectures, and trying most of the assignments. I am working on breakout right now, and am having some trouble. I've got a very good start, but I'm getting a Exception in thread "main" java.lang.NullPointerException on my remove(...) line. I'm figuring I'm creating the blocks and removing them completely the wrong way? Thanks so much for your help!!!

    Java Code:
    /*
     * File: Breakout.java
     * -------------------
     * Name:
     * Section Leader:
     * 
     * This file will eventually implement the game of Breakout.
     */
    
    import acm.graphics.*;
    import acm.program.*;
    import acm.util.*;
    
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Breakout extends GraphicsProgram {
    
    /** Width and height of application window in pixels */
    	public static final int APPLICATION_WIDTH = 400;
    	public static final int APPLICATION_HEIGHT = 600;
    
    /** Dimensions of game board (usually the same) */
    	private static final int WIDTH = APPLICATION_WIDTH;
    	private static final int HEIGHT = APPLICATION_HEIGHT;
    
    /** Dimensions of the paddle */
    	private static final int PADDLE_WIDTH = 60;
    	private static final int PADDLE_HEIGHT = 10;
    
    /** Offset of the paddle up from the bottom */
    	private static final int PADDLE_Y_OFFSET = 30;
    
    /** Number of bricks per row */
    	private static final int NBRICKS_PER_ROW = 10;
    
    /** Number of rows of bricks */
    	private static final int NBRICK_ROWS = 10;
    
    /** Separation between bricks */
    	private static final int BRICK_SEP = 4;
    
    /** Width of a brick */
    	private static final int BRICK_WIDTH =
    	  (WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;
    
    /** Height of a brick */
    	private static final int BRICK_HEIGHT = 8;
    
    /** Radius of the ball in pixels */
    	private static final int BALL_RADIUS = 10;
    
    /** Offset of the top brick row from the top */
    	private static final int BRICK_Y_OFFSET = 70;
    	
    /** Offset of the left brick column from the side */
    	private static final int BRICK_X_OFFSET = (WIDTH - ((BRICK_WIDTH + BRICK_SEP) * NBRICKS_PER_ROW - BRICK_SEP)) / 2;
    
    /** Number of turns */
    	private static final int NTURNS = 3;
    	
    	private static GRect paddle = new GRect(WIDTH / 2 - (PADDLE_WIDTH / 2), HEIGHT - (PADDLE_Y_OFFSET + PADDLE_HEIGHT), PADDLE_WIDTH, PADDLE_HEIGHT);
    	
    	private static GOval ball = new GOval(WIDTH / 2 - BALL_RADIUS, HEIGHT / 2, 2* BALL_RADIUS, 2* BALL_RADIUS);
    
    	private double vx, vy; 
    	
    	private RandomGenerator rgen = RandomGenerator.getInstance(); 
    	
    	public void init() {
    		addMouseListeners();
    	}
    	
    /* Method: run() */
    /** Runs the Breakout program. */
    	public void run() {
    		setUp();
    		gameRunning();
    	}
    	
    	public void mouseMoved(MouseEvent e) {
    		if (e.getX() > PADDLE_WIDTH / 2 && e.getX() < WIDTH - (PADDLE_WIDTH / 2)) {
    			paddle.move(e.getX()-paddle.getX() - PADDLE_WIDTH / 2, 0);
    		}
    	}	
    
    	private void setUp() {
    		createBricks();
    		createPaddle();
    		createBall();
    	}
    
    	private void createBricks() {
    		for (int j = 0; j < NBRICK_ROWS; j++) {
    			for (int i = 0; i < NBRICKS_PER_ROW; i++) {
    				GRect rect = new GRect(BRICK_X_OFFSET + (BRICK_SEP + BRICK_WIDTH) * i, BRICK_Y_OFFSET + (BRICK_SEP + BRICK_HEIGHT) * j, BRICK_WIDTH, BRICK_HEIGHT);
    				rect.setFilled(true);
    				if (j < 2) rect.setColor(Color.RED);				
    				else if (j >= 2 && j < 4) rect.setColor(Color.ORANGE);				
    				else if (j >= 4 && j < 6) rect.setColor(Color.YELLOW);
    				else if (j >= 6 && j < 8) rect.setColor(Color.GREEN);				
    				else if (j >= 8) rect.setColor(Color.CYAN);				
    				add(rect);
    			}
    		}
    	}
    	private void createPaddle() {		
    		paddle.setFilled(true);
    		add(paddle);		
    	}
    	
    	private void createBall() {		
    		ball.setFilled(true);
    		add(ball);		
    	}
    	
    	private void gameRunning() {
    		vx = rgen.nextDouble(1.0, 3.0); 
    		if (rgen.nextBoolean(0.5)) vx = -vx; 
    		vy = rgen.nextDouble(1.0, 3.0); 
    		if (rgen.nextBoolean(0.5)) vy = -vy;
    		
    		while(ball.getY() < (HEIGHT - 2 * BALL_RADIUS))  {
    			ball.move(vx, vy);
    			if (ball.getX() < 0) vx = -vx; 
    			if (ball.getX() > (WIDTH - 2 * BALL_RADIUS)) vx = -vx;
    			if (ball.getY() < 0) vy = -vy;
    			checkCollisions();			
    			pause(10);
    		}
    	}
    	
    	private void checkCollisions() {
    		if (getCollidingObject(ball.getX(), ball.getY()) == paddle) vy = -vy;
    		else if (getCollidingObject(ball.getX() + 2 * BALL_RADIUS, ball.getY()) == paddle) vy = -vy;
    		else if (getCollidingObject(ball.getX(), ball.getY() - 2 * BALL_RADIUS) == paddle) vy = -vy;
    		else if (getCollidingObject(ball.getX() + 2 * BALL_RADIUS, ball.getY() - 2 * BALL_RADIUS) == paddle) vy = -vy;		
    		else remove(getCollidingObject(ball.getX(), ball.getY()));
    	}
    	
    	private GObject getCollidingObject(double x, double y) {
    		return getElementAt(x,y); 
    	}
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Try this to see what's going on
    Java Code:
        private void checkCollisions() {
            if (getCollidingObject(ball.getX(), ball.getY()) == paddle) vy = -vy;
            else if (getCollidingObject(ball.getX() + 2 * BALL_RADIUS,
                                        ball.getY()) == paddle) vy = -vy;
            else if (getCollidingObject(ball.getX(),
                        ball.getY() - 2 * BALL_RADIUS) == paddle) vy = -vy;
            else if (getCollidingObject(ball.getX() + 2 * BALL_RADIUS,
                        ball.getY() - 2 * BALL_RADIUS) == paddle) vy = -vy;
            else if(getCollidingObject(ball.getX(), ball.getY()) != null)
                remove(getCollidingObject(ball.getX(), ball.getY()));
            else
                System.out.printf("getCollidingObject(%.1f, %.1f) = null%n",
                                   ball.getX(), ball.getY());
        }
    acm package available at ACM Java Task Force
    javadocs are here.

  3. #3
    jjoshua2 is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default Thanks!

    Thanks so much. That code made it work. I guess what was happening is it was returning null and trying to remove that. It's working now. I'm going to try and clean it up while having it test all four corners of the box/ball.

Similar Threads

  1. How to start learning Java
    By hiranya in forum New To Java
    Replies: 12
    Last Post: 01-13-2014, 09:47 AM
  2. Interested in learning Java
    By amarelv in forum New To Java
    Replies: 9
    Last Post: 11-10-2008, 10:11 AM
  3. Java Learning Curve
    By mwildam in forum New To Java
    Replies: 13
    Last Post: 10-17-2008, 11:09 AM
  4. how to begin with learning to develope in JAVA
    By Triji in forum New To Java
    Replies: 1
    Last Post: 08-11-2008, 02:25 PM
  5. Java Machine Learning Library 0.0.12
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 10-20-2007, 05:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •