I am using the acm library(acm.jar), same library Stanford students used for their Java course CS106A to create graphical application easier.
acm.jar can be found here: http://jtf.acm.org/
The following code adds a character sprite to a graphics window. If I press z , the character animates itself moving his bow and arrows starts to spawn moving vertically through the use of Java Threads. There is no errors so far.
Now I want to be able to able to perform collision detection on a threaded GImage object(linkArrow). An error happened in my program when I tried doing this:
arrowPoint = new GPoint(linkArrow.getX(),linkArrow.getY());
arrowObject = getElementAt(arrowPoint);
I get this error:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
You can ctrl+f and type bug to see the error location of my program.
I have done collision detection before using graphical rectangles and oval using the GPoint and getElementAt for my game “BreakOut” and never had a problem using GPoint and getElementAt.
Here is my code: first class is main program that runs the thread. The second class is the thread.
import java.awt.event.KeyEvent;
import acm.graphics.GImage;
import acm.program.GraphicsProgram;
import acm.util.RandomGenerator;
public class Link extends GraphicsProgram {
private static final double GRAVITY = 1;
public void init(){
setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
addLink();
addKeyListeners();
}
private void addLink(){
linkCharacter = new GImage("link sprites/linkFaceRight/link_frame_1_face_right.png");
add(linkCharacter,link_Location_XCoord,link_Location_YCoord);
}
public void keyPressed(KeyEvent e){
/* Link's Movement
*
*/
char linkMoveRightKey = e.getKeyChar();
if(linkMoveRightKey == 'z')
{
xSpeed = 0;
ySpeed = 0;
pause(arrowDELAY);
linkCharacter.move(xSpeed, ySpeed);
linkCharacter.setImage(linkAttackWithBow[linkFrame]);
linkFrame++;
callArrow();
// BUG
arrowPoint = new GPoint(linkArrow.getX(),linkArrow.getY());
arrowObject = getElementAt(arrowPoint);
}
//
/*
* summon link's arrow
*/
}
if(linkFrame>=linkAttackWithBow.length){
linkFrame = 0;
}
}
private void callArrow(){
if(linkFrame == 2){
linkArrow = new ArrowThread(SIZE, rgen.nextColor());
add(linkArrow,linkCharacter.getX(),linkCharacter.getY());
Thread arrowThread = new Thread(linkArrow);
arrowThread.start();
}
}
private ArrowThread linkArrow;
private int SIZE = 400;
private RandomGenerator rgen = RandomGenerator.getInstance();
private GImage gameBackgroundImage;
private GImage linkCharacter;
private double arrowDELAY = 28;
private int link_Location_XCoord = 50;
private int link_Location_YCoord = 50 ;
private final int APPLICATION_WIDTH = 1200;
private final int APPLICATION_HEIGHT = 800;
private String[] linkAttackWithBow = {"link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_1.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_2.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_3.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_1.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_2.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_3.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_1.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_2.png"};
private int linkFrame = 0;
private int xSpeed =5; //the number of pixels to move in x
private int ySpeed = 0; //0 so you only move horizontally
}
Here is my Thread class to spawn the arrows
import java.awt.Color;
import acm.graphics.GImage;
public class ArrowThread extends GImage implements Runnable{
public ArrowThread(int SIZE,Color color){
super("link sprites/linkAttackWithBow/arrow.png", SIZE,SIZE);
}
public void run(){
for(int i = 0; i < 1000/STEP;i++){
pause(60);
move(arrowSpeedX,arrowSpeedY);
}
}
private static final int arrowSpeedX = 0;
private static final int arrowSpeedY = -5;
private static final double STEP = 5;
}
It looks like the problem is that the member
linkArrowis null on the first frame. See the check: