I am learning java from videos online, I wont post the link because i'm not sure if I would be allows but I can do if anybody wants.
From the video I have copied the code exactly. My code compiles but when I run it, it doesn't work. The person online runs his and it works. No idea what's wrong:
I am using practicing using sprites:
Sprite class:
import java.awt.Image; public class Sprite { private Animation a; private float x; private float y; private float vx; private float vy; //Constructor public Sprite(Animation a) { this.a = a; } //Chnage position public void update(long timePassed) { x += vx * timePassed; y += vy * timePassed; a.update(timePassed); } //Get x position public float getX() { return x; } //Get y position public float getY() { return y; } //Set sprite x position public void setX(float x) { this.x = 0; } //Set sprite y position public void setY(float y) { this.y = 0; } //Get sprite width public int getWidth() { return a.getImage().getWidth(null); } //Get sprite height public int getHeight() { return a.getImage().getHeight(null); } //get horizontal velocity public float getVelocityX() { return vx; } //get vertical velocity public float getVelocityY() { return vy; } //set horizontal velocity public void setVelocityX(float vx) { this.vx = vx; } //set vertical velocity public void setVelocityY(float vy) { this.vy = vy; } //get sprite / image public Image getImage() { return a.getImage(); } }
Animation class:
import java.awt.Image; import java.util.ArrayList; public class Animation{ private ArrayList scenes; private int sceneIndex; private long movieTime; private long totalTime; //CONSTRUCTOR public Animation(){ scenes = new ArrayList(); totalTime = 0; start(); } //Add scene to ArrayList and set time for each scene public synchronized void addScene(Image i, long t) { totalTime += t; scenes.add(new oneScene(i, totalTime)); } //Start aimation from begining public synchronized void start() { movieTime = 0; sceneIndex = 0; } //Change scenes public synchronized void update(long timePassed) { if(scenes.size()>1){ movieTime += timePassed; if(movieTime >= totalTime){ movieTime = 0; sceneIndex = 0; } while(movieTime > getScene(sceneIndex).endTime){ sceneIndex++; } } } //get animations current scene AKA image public synchronized Image getImage() { if(scenes.size()==0){ return null; }else{ return getScene(sceneIndex).pic; } } //get scene private oneScene getScene(int x) { return (oneScene) scenes.get(x); } //Private inner class// private class oneScene{ Image pic; long endTime; public oneScene(Image pic, long endTime) { this.pic = pic; this.endTime = endTime; } } }
Screen Manager class:
import java.awt.*; import java.awt.image.BufferStrategy; import java.awt.image.BufferedImage; import java.lang.reflect.InvocationTargetException; import javax.swing.JFrame; public class ScreenManager { private GraphicsDevice vc; //Give vc acces to moniter screen public ScreenManager() { GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); vc = e.getDefaultScreenDevice(); } //Get all compatible display modes public DisplayMode[] getCompatibleDisaplyModes() { return vc.getDisplayModes(); } //Compares display modes passed in to vc display modes and see if they match public DisplayMode findFirstCompatibleMode(DisplayMode modes[]) { DisplayMode goodModes[] = vc.getDisplayModes(); for(int x=0; x<modes.length; x++){ for(int y = 0; y<goodModes.length; y++){ if(displayModesMatch(modes[x], goodModes[y])){ return modes[x]; } } } return null; } //Get current displayMode public DisplayMode getCurrentDisplayMode() { return vc.getDisplayMode(); } //Checks if two modes match each other public boolean displayModesMatch(DisplayMode m1, DisplayMode m2) { if(m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight()) { return false; } if(m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth()) { return false; } if(m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate()) { return false; } return true; } //Make frame full screen public void setFullScreen(DisplayMode dm) { JFrame f = new JFrame(); f.setUndecorated(true); f.setIgnoreRepaint(true); f.setResizable(false); vc.setFullScreenWindow(f); if(dm != null && vc.isDisplayChangeSupported()) { try{ vc.setDisplayMode(dm); }catch(Exception ex) {} } f.createBufferStrategy(2); } //Set graphics object = to this public Graphics2D getGraphics() { Window w = vc.getFullScreenWindow(); if(w != null){ BufferStrategy s = w.getBufferStrategy(); return (Graphics2D)s.getDrawGraphics(); } else{ return null; } } //Updates display public void update() { Window w = vc.getFullScreenWindow(); if(w != null){ BufferStrategy s = w.getBufferStrategy(); if(!s.contentsLost()){ s.show(); } } } //Return full screen window public Window getFullScreenWindow() { return vc.getFullScreenWindow(); } //Get width of window public int getWidth() { Window w = vc.getFullScreenWindow(); if(w != null){ return w.getWidth(); }else{ return 0; } } //Get hieght of window public int getHeight() { Window w = vc.getFullScreenWindow(); if(w != null){ return w.getHeight(); }else{ return 0; } } //Get out of full screen public void restoreScreen() { Window w = vc.getFullScreenWindow(); if(w != null){ w.dispose(); } vc.setFullScreenWindow(null); } //Create image compatible with monitor public BufferedImage createCompatibleImage(int w, int h, int t) { Window win = vc.getFullScreenWindow(); if(win != null){ GraphicsConfiguration gc = win.getGraphicsConfiguration(); return gc.createCompatibleImage(w,h,t); } return null; } }
bucky class:
import java.awt.*; import javax.swing.JFrame; import javax.swing.ImageIcon; import javax.swing.*; public class bucky2{ public static void main(String args[]) { bucky2 b = new bucky2(); b.run(); } private Sprite sprite; private Animation a; private ScreenManager s; private Image bg; private static final DisplayMode modes1[] = { new DisplayMode(800,600,32,0), new DisplayMode(800,600,24,0), new DisplayMode(800,600,16,0), new DisplayMode(640,480,32,0), new DisplayMode(640,480,24,0), new DisplayMode(640,480,16,0), }; //Load images and add scenes public void loadImages() { bg = new ImageIcon("C:\\temp\background1.jpg").getImage(); Image face1 = new ImageIcon("C:\\temp\face1.png").getImage(); Image face2 = new ImageIcon("C:\\temp\face2.png").getImage(); a = new Animation(); a.addScene(face1, 250); a.addScene(face2, 250); sprite = new Sprite(a); sprite.setVelocityX(0.3f); sprite.setVelocityY(0.3f); } //Main method called from main public void run() { s = new ScreenManager(); try{ DisplayMode dm = s.findFirstCompatibleMode(modes1); s.setFullScreen(dm); movieLoop(); }finally{ s.restoreScreen(); } } //Play movie public void movieLoop() { long startingTime = System.currentTimeMillis(); long cumTime = startingTime; while(cumTime - startingTime < 5000){ long timePassed = System.currentTimeMillis() - cumTime; cumTime += timePassed; update(timePassed); //draw and update screen Graphics2D g = s.getGraphics(); draw(g); g.dispose(); s.update(); try{ Thread.sleep(200); }catch (Exception ex){} } } //Draws graphics public void draw(Graphics g) { g.drawImage(bg, 0,0, null); g.drawImage(sprite.getImage(), Math.round(sprite.getX()), Math.round(sprite.getY()), null); } //Update sprite positioning public void update(long timePassed) { if(sprite.getX() < 0){ sprite.setVelocityX(Math.abs(sprite.getVelocityX())); }else if(sprite.getX() + sprite.getWidth() >= s.getWidth()){ sprite.setVelocityX(-Math.abs(sprite.getVelocityX())); } if(sprite.getY() < 0){ sprite.setVelocityY(Math.abs(sprite.getVelocityY())); }else if(sprite.getY() + sprite.getHeight() >= s.getHeight()){ sprite.setVelocityY(-Math.abs(sprite.getVelocityY())); } sprite.update(timePassed); } }
When I run my code, in the bucky class on the update method :
public void update(long timePassed) { if(sprite.getX() < 0){
That is what gives me a null pointer exception. I have tried many things but I can't think of anything.
I have attached my code so if anybody wants to try it, I have done it in both blueJ and Eclipse.
Sorry about the long code!
Thank you.