Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    Nonoon is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Calling another class to display images.

    Hello everyone.

    I have been trying to call a different class from my menu. The class is to display images and a animation when called on.

    This was the menu I was working on and had asked for help before.
    Changing images in a image array with keypresses

    Java Code:
    if(keys[KeyEvent.VK_ENTER]){
    				onMenu = false;
    				if(frame==0){
    				MenuImages[0] = null;
    				playanimation = true;
    Instead of switching to a different image it will change a boolean to true

    Java Code:
    public void playAnimation()
    	  {
    	   if (playanimation == true);
    	     playanimation = new Animation();      
    	  }
    The update method will have playAnimation() as well.

    When I do try to run the applet and go to play the animation I am only left with a blank white screen from setting menuimages to null.


    The animation class does work, when I run that on its own the images and animation do show up.

    Can anyone advise me on how I should be calling different classes?

    Appreciate any help and time given.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Calling another class to display images.

    Java Code:
             playanimation == true);
             playanimation = new Animation();
    This should be a problem. The variable appears to be a boolean and a reference to an object.

    You need to post enough code that compiles, executes and shows your problem. Your small code segments don't show what the problem is.

  3. #3
    Nonoon is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Calling another class to display images.

    Apologies for that.

    Java Code:
    package animation;
    
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferedImage;
    import java.net.URL;
    
    import javax.swing.ImageIcon;
    
    
    public class MainMenu extends Applet implements Runnable, KeyListener{
    
    
    	Image[] MenuImages = new Image[8];
    	int frame = 0;
    	boolean [] keys = new boolean[500];
    	boolean running = false;
    	private static final int PWIDTH = 800;   
    	private static final int PHEIGHT= 540;
    
    
    	private Image backgroundImage = null;
    	private Graphics backGraphics;
    
    	private boolean playanimation;
    	private Animation animation; 
    	
    	
    	public void init () {
    
    		setBackground(Color.WHITE);
    		setSize(new Dimension(PWIDTH, PHEIGHT));
    		try {	
    			MenuImages[0] = loadImage("res/Menu1.png");
    			MenuImages[1] = loadImage("res/Menu2.png");
    			MenuImages[2] = loadImage("res/Menu3.png");		
    			MenuImages[3] = loadImage("res/Option1.png");
    			MenuImages[4] = loadImage("res/Option2.png");		
    			MenuImages[5] = loadImage("res/Option3.png");
    			MenuImages[6] = loadImage("res/Option4.jpg");
    			MenuImages[7] = loadImage("res/Option5.png");
    		} catch(Exception ex) {
    		}
    	}
    	
    	public void start() 
    	{
    		running= true;
    		Thread th = new Thread(this); 
    		addKeyListener(this);
    		th.start();
    	}
    	
    	  public void play()
    	  {
    	   if (playanimation == true);
    	     animation = new Animation();     
    	 }
    
    	private Image loadImage( String filename )
    	{
    		Image img = null;
    		URL url = this.getClass().getResource(filename);
    		System.out.println(url.toString());
    		img = Toolkit.getDefaultToolkit().getImage(url);
    		return img;
    	}
    
    	public void paint(){
    
    		if (backgroundImage == null){  
    			backgroundImage = createImage(PWIDTH, PHEIGHT);
    			if (backgroundImage == null) {
    				return;
    			}
    			else
    				backGraphics = backgroundImage.getGraphics( );
    		}
    		backGraphics.setColor(Color.white);
    		backGraphics.fillRect(0, 0, PWIDTH, PHEIGHT);
    		backGraphics.drawImage(MenuImages[frame], 0, 0, this);
    	}
    
    	private void paintScreen()
    	{
    		Graphics  g;
    		try {
    			g = this.getGraphics( );  
    			if ((g != null) && (backgroundImage != null))
    				g.drawImage(backgroundImage, 0, 0, null);
    			g.dispose( );
    		}
    		catch (Exception e)
    		{ System.out.println("Graphics context error: " + e);  }
    	} 
    
    	private void Update (){
    		if (playanimation = true) {
    			animation = new Animation();	
    		}
    	}
    	
    	private void menuKeys() {
    
    		{
    			if(keys[KeyEvent.VK_UP]){
    				frame--;
    				System.out.println("Frame count: " + frame);
    				if(frame<0){
    					frame = 2;
    				}
    				keys[KeyEvent.VK_UP] = false;
    			}
    			if(keys[KeyEvent.VK_DOWN]){
    
    				frame++;
    				System.out.println("Frame count: " + frame);
    				if(frame>2){
    					frame=0;
    				}
    				keys[KeyEvent.VK_DOWN] = false;
    			}
    					
    			if(keys[KeyEvent.VK_ENTER]){
    				
    				if(frame==0){
    				MenuImages[0] = null;
    				playanimation = true;
    					
    				}
    				if(frame==1){
    					frame =4;
    				}
    				if(frame==2){
    					frame =5;		
    				}	 
    				keys[KeyEvent.VK_ENTER ] = false;
    			}
    		}
    		if(keys[KeyEvent.VK_ESCAPE]){
    			frame=0;
    		}
    
    	}
    
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) {
    		// TODO Auto-generated method stub
    		keys[e.getKeyCode()] = true; 
    	}
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
    		keys[e.getKeyCode()] = false;   
    	}
    
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		{
    			while(running)
    			{
    				play();
    				Update();
    				menuKeys();
    				paintScreen();
    				paint();
    
    				try
    				{
    					Thread.sleep(40);
    				}
    				catch(InterruptedException ex)
    				{
    
    				}
    			}
    		}
    	}
    }

    package animation;

    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.Ellipse2D;

    Java Code:
    public class Animation extends Applet implements Runnable{
    
    	private Thread runThread ;
    	private boolean running;
    	
            private Image doubleBuffering = null;
    	private Graphics graphicsBuffer;
    	private Background background;
    	private Character character;
    
            private static int WIDTH;
    	private static int HEIGHT;
    	
    	
    	public void init() {
    	 
    		background = new Background();
    		character = new Character();
    		
    		
    	}
    	
     
    	public void start() {
    		runThread = new Thread(this); 
    		runThread.start();		
    		running = true;
    	
    	}
    	public void stop() {
    		running = false;
    		
    	}
    		
    	public void destroy() {
    		
    	}
    	
    	
    	public void Update() {
    		background.backgroundScrolling();	
    	}
    	
    	private void paintScreen(){
    	
    		Graphics g;
    		try{
    			g = this.getGraphics();
    			if((g != null)&&(doubleBuffering!=null)){
    				g.drawImage(doubleBuffering, 0, 0, null);
    			}
    			Toolkit.getDefaultToolkit().sync();
    			g.dispose();
    		}
    		catch (Exception e) {
    			System.out.println("Graphics  error: " + e);
    		}
    	}
    	
    
    	private void Render()
    	{
    		
    		if(doubleBuffering == null){
    			WIDTH = getWidth();
    			HEIGHT = getHeight();
    			doubleBuffering = createImage(WIDTH, HEIGHT);
    			if (doubleBuffering == null){
    				System.out.println("Can not create main Image");
    				return;
    			}
    		}
    		if(graphicsBuffer == null){
    			graphicsBuffer = doubleBuffering.getGraphics();
    		}
    		graphicsBuffer.setColor(Color.white);
    		graphicsBuffer.fillRect(0, 0, WIDTH, HEIGHT);
    		background.drawBackground(graphicsBuffer, this);
    		character.drawCharacter(graphicsBuffer, this);
    		
    		
    	}
    	
    	
    	
    	public void run() {
    		System.out.println("ArcadeGame::run() called");
    		while(running) { 
    			Update();
    			Render();
    			paintScreen();
    			try{
    				Thread.sleep(20);
    			}
    			catch(InterruptedException ex){
    				ex.printStackTrace();
    			}
    		}
    	}
    }

    I've tried it without the MenuImages = null. The image would stay the same.
    The error message I would get is.

    Graphics context error: java.lang.NullPointerException
    Graphics context error: java.lang.NullPointerException
    Graphics context error: java.lang.NullPointerException

  4. #4
    Nonoon is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Calling another class to display images.

    Apologies for that.

    Java Code:
    package animation;
    
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferedImage;
    import java.net.URL;
    
    import javax.swing.ImageIcon;
    
    
    public class MainMenu extends Applet implements Runnable, KeyListener{
    
    
    	Image[] MenuImages = new Image[8];
    	int frame = 0;
    	boolean [] keys = new boolean[500];
    	boolean running = false;
    	private static final int PWIDTH = 800;   
    	private static final int PHEIGHT= 540;
    	private Image backgroundImage = null;
    	private Graphics backGraphics;
    	private boolean playanimation;
    	private Animation animation; 
    	
    	
    	public void init () {
    
    		setBackground(Color.WHITE);
    		setSize(new Dimension(PWIDTH, PHEIGHT));
    		try {	
    			MenuImages[0] = loadImage("res/Menu1.png");
    			MenuImages[1] = loadImage("res/Menu2.png");
    			MenuImages[2] = loadImage("res/Menu3.png");		
    			MenuImages[3] = loadImage("res/Option1.png");
    			MenuImages[4] = loadImage("res/Option2.png");		
    			MenuImages[5] = loadImage("res/Option3.png");
    			MenuImages[6] = loadImage("res/Option4.jpg");
    			MenuImages[7] = loadImage("res/Option5.png");
    		} catch(Exception ex) {
    		}
    	}
    	
    	public void start() 
    	{
    		running= true;
    		Thread th = new Thread(this); 
    		addKeyListener(this);
    		th.start();
    	}
    	
    	  public void play()
    	  {
    	   if (playanimation == true);
    	     animation = new Animation();     
    	 }
    
    	private Image loadImage( String filename )
    	{
    		Image img = null;
    		URL url = this.getClass().getResource(filename);
    		System.out.println(url.toString());
    		img = Toolkit.getDefaultToolkit().getImage(url);
    		return img;
    	}
    
    	public void paint(){
    
    		if (backgroundImage == null){  
    			backgroundImage = createImage(PWIDTH, PHEIGHT);
    			if (backgroundImage == null) {
    				return;
    			}
    			else
    				backGraphics = backgroundImage.getGraphics( );
    		}
    		backGraphics.setColor(Color.white);
    		backGraphics.fillRect(0, 0, PWIDTH, PHEIGHT);
    		backGraphics.drawImage(MenuImages[frame], 0, 0, this);
    	}
    
    	private void paintScreen()
    	{
    		Graphics  g;
    		try {
    			g = this.getGraphics( );  
    			if ((g != null) && (backgroundImage != null))
    				g.drawImage(backgroundImage, 0, 0, null);
    			g.dispose( );
    		}
    		catch (Exception e)
    		{ System.out.println("Graphics context error: " + e);  }
    	} 
    
    	private void Update (){
    		if (playanimation = true) {
    			animation = new Animation();	
    		}
    	}
    	
    	private void menuKeys() {
    
    		{
    			if(keys[KeyEvent.VK_UP]){
    				frame--;
    				System.out.println("Frame count: " + frame);
    				if(frame<0){
    					frame = 2;
    				}
    				keys[KeyEvent.VK_UP] = false;
    			}
    			if(keys[KeyEvent.VK_DOWN]){
    
    				frame++;
    				System.out.println("Frame count: " + frame);
    				if(frame>2){
    					frame=0;
    				}
    				keys[KeyEvent.VK_DOWN] = false;
    			}
    			
    			if(keys[KeyEvent.VK_ENTER]){
    				
    				if(frame==0){
    				MenuImages[0] = null;
    				playanimation = true;
    					
    				}
    				if(frame==1){
    					frame =4;
    				}
    				if(frame==2){
    					frame =5;		
    				}	 
    				keys[KeyEvent.VK_ENTER ] = false;
    			}
    		}
    		if(keys[KeyEvent.VK_ESCAPE]){
    			frame=0;
    		}
    
    	}
    
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) {
    		// TODO Auto-generated method stub
    		keys[e.getKeyCode()] = true; 
    	}
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
    		keys[e.getKeyCode()] = false;   
    	}
    
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		{
    			while(running)
    			{
    				play();
    				Update();
    				menuKeys();
    				paintScreen();
    				paint();
    
    				try
    				{
    					Thread.sleep(40);
    				}
    				catch(InterruptedException ex)
    				{
    
    				}
    			}
    		}
    	}
    }
    Java Code:
    package animation;
    
    import java.applet.Applet; 
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.Ellipse2D;
    
    import org.jbox2d.collision.AABB;
    import org.jbox2d.collision.CircleDef;
    import org.jbox2d.collision.CircleShape;
    import org.jbox2d.collision.PolygonDef;
    import org.jbox2d.collision.PolygonShape;
    import org.jbox2d.collision.Shape;
    import org.jbox2d.collision.ShapeType;
    import org.jbox2d.common.Vec2;
    import org.jbox2d.dynamics.Body;
    import org.jbox2d.dynamics.BodyDef;
    import org.jbox2d.dynamics.World;
    @SuppressWarnings("serial")
    
    
    
    public class Animation extends Applet implements Runnable{
    
    	private Thread runThread ;
    
    	private Image doubleBuffering = null;
    	private Graphics graphicsBuffer;
    	private boolean running;
    	private static int WIDTH;
    	private static int HEIGHT;
    	private Background background;
    	private Character character;
    	
    	
    	public void init() {
    	 
    		background = new Background();
    		character = new Character();
    		
    		
    	}
    	
     
    	public void start() {
    		runThread = new Thread(this); 
    		runThread.start();		
    		running = true;
    	
    	}
    	public void stop() {
    		running = false;
    		
    	}
    		
    	public void destroy() {
    		
    	}
    	
    	
    	public void Update() {
    		background.backgroundScrolling();	
    	}
    	
    	private void paintScreen(){
    	
    		Graphics g;
    		try{
    			g = this.getGraphics();
    			if((g != null)&&(doubleBuffering!=null)){
    				g.drawImage(doubleBuffering, 0, 0, null);
    			}
    			Toolkit.getDefaultToolkit().sync();
    			g.dispose();
    		}
    		catch (Exception e) {
    			System.out.println("Graphics  error: " + e);
    		}
    	}
    	
    
    	private void Render()
    	{
    		
    		if(doubleBuffering == null){
    			WIDTH = getWidth();
    			HEIGHT = getHeight();
    			doubleBuffering = createImage(WIDTH, HEIGHT);
    			if (doubleBuffering == null){
    				System.out.println("Can not create main Image");
    				return;
    			}
    		}
    		if(graphicsBuffer == null){
    			graphicsBuffer = doubleBuffering.getGraphics();
    		}
    		graphicsBuffer.setColor(Color.white);
    		graphicsBuffer.fillRect(0, 0, WIDTH, HEIGHT);
    		background.drawBackground(graphicsBuffer, this);
    		character.drawCharacter(graphicsBuffer, this);
    		
    		
    	}
    	
    	
    	
    	public void run() {
    		System.out.println("ArcadeGame::run() called");
    		while(running) { 
    			Update();
    			Render();
    			paintScreen();
    			try{
    				Thread.sleep(20);
    			}
    			catch(InterruptedException ex){
    				ex.printStackTrace();
    			}
    		}
    	}
    }
    Running it now does give the error message
    Graphics context error: java.lang.NullPointerException
    Graphics context error: java.lang.NullPointerException
    Graphics context error: java.lang.NullPointerException

    I've looked at some explanation for this online. Am I creating the object incorrectly?

  5. #5
    Nonoon is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Calling another class to display images.

    Sorry for the delayed reply. I thought I had replied back with more code.



    Java Code:
    package animation;
    
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferedImage;
    import java.net.URL;
    
    import javax.swing.ImageIcon;
    
    
    public class MainMenu extends Applet implements Runnable, KeyListener{
    
    
    	Image[] MenuImages = new Image[8];
    	int frame = 0;
    	boolean [] keys = new boolean[500];
    	boolean running = false;
    	private static final int PWIDTH = 800;   
    	private static final int PHEIGHT= 540;
    
    
    	private Image backgroundImage = null;
    	private Graphics backGraphics;
    
    	private boolean playanimation;
    	private Animation animation; 
    	
    	
    	public void init () {
    
    		setBackground(Color.WHITE);
    		setSize(new Dimension(PWIDTH, PHEIGHT));
    		try {	
    			MenuImages[0] = loadImage("res/Menu1.png");
    			MenuImages[1] = loadImage("res/Menu2.png");
    			MenuImages[2] = loadImage("res/Menu3.png");		
    			MenuImages[3] = loadImage("res/Option1.png");
    			MenuImages[4] = loadImage("res/Option2.png");		
    			MenuImages[5] = loadImage("res/Option3.png");
    			MenuImages[6] = loadImage("res/Option4.jpg");
    			MenuImages[7] = loadImage("res/Option5.png");
    		} catch(Exception ex) {
    		}
    	}
    	
    	public void start() 
    	{
    		running= true;
    		Thread th = new Thread(this); 
    		addKeyListener(this);
    		th.start();
    	}
    	
    	  public void play()
    	  {
    	   if (playanimation == true);
    	     animation = new Animation();     
    	 }
    
    	private Image loadImage( String filename )
    	{
    		Image img = null;
    		URL url = this.getClass().getResource(filename);
    		System.out.println(url.toString());
    		img = Toolkit.getDefaultToolkit().getImage(url);
    		return img;
    	}
    
    	public void paint(){
    
    		if (backgroundImage == null){  
    			backgroundImage = createImage(PWIDTH, PHEIGHT);
    			if (backgroundImage == null) {
    				return;
    			}
    			else
    				backGraphics = backgroundImage.getGraphics( );
    		}
    		backGraphics.setColor(Color.white);
    		backGraphics.fillRect(0, 0, PWIDTH, PHEIGHT);
    		backGraphics.drawImage(MenuImages[frame], 0, 0, this);
    	}
    
    	private void paintScreen()
    	{
    		Graphics  g;
    		try {
    			g = this.getGraphics( );  
    			if ((g != null) && (backgroundImage != null))
    				g.drawImage(backgroundImage, 0, 0, null);
    			g.dispose( );
    		}
    		catch (Exception e)
    		{ System.out.println("Graphics context error: " + e);  }
    	} 
    
    	private void Update (){
    		if (playanimation = true) {
    			animation = new Animation();	
    		}
    	}
    	
    	private void menuKeys() {
    
    		{
    			if(keys[KeyEvent.VK_UP]){
    				frame--;
    				System.out.println("Frame count: " + frame);
    				if(frame<0){
    					frame = 2;
    				}
    				keys[KeyEvent.VK_UP] = false;
    			}
    			if(keys[KeyEvent.VK_DOWN]){
    
    				frame++;
    				System.out.println("Frame count: " + frame);
    				if(frame>2){
    					frame=0;
    				}
    				keys[KeyEvent.VK_DOWN] = false;
    			}
    			
    			if(keys[KeyEvent.VK_ENTER]){
    				
    				if(frame==0){
    				MenuImages[0] = null;
    				playanimation = true;
    					
    				}
    				if(frame==1){
    					frame =4;
    				}
    				if(frame==2){
    					frame =5;		
    				}	 
    				keys[KeyEvent.VK_ENTER ] = false;
    			}
    		}
    		if(keys[KeyEvent.VK_ESCAPE]){
    			frame=0;
    		}
    
    	}
    
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) {
    		// TODO Auto-generated method stub
    		keys[e.getKeyCode()] = true; 
    	}
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
    		keys[e.getKeyCode()] = false;   
    	}
    
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		{
    			while(running)
    			{
    				play();
    				Update();
    				menuKeys();
    				paintScreen();
    				paint();
    
    				try
    				{
    					Thread.sleep(40);
    				}
    				catch(InterruptedException ex)
    				{
    
    				}
    			}
    		}
    	}
    }
    Java Code:
    package animation;
    
    import java.applet.Applet; 
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.Ellipse2D;
    
    
    public class Animation extends Applet implements Runnable{
    
    	private Thread runThread ;
    
    	private Image doubleBuffering = null;
    	private Graphics graphicsBuffer;
    	private boolean running;
    	private static int WIDTH;
    	private static int HEIGHT;
    	private Background background;
    	private Character character;
    	
    	
    	public void init() {
    	 
    		background = new Background();
    		character = new Character();
    		
    		
    	}
    	
     
    	public void start() {
    		runThread = new Thread(this); 
    		runThread.start();		
    		running = true;
    	
    	}
    	public void stop() {
    		running = false;
    		
    	}
    		
    	public void destroy() {
    		
    	}
    	
    	
    	public void Update() {
    		background.backgroundScrolling();	
    	}
    	
    	private void paintScreen(){
    	
    		Graphics g;
    		try{
    			g = this.getGraphics();
    			if((g != null)&&(doubleBuffering!=null)){
    				g.drawImage(doubleBuffering, 0, 0, null);
    			}
    			Toolkit.getDefaultToolkit().sync();
    			g.dispose();
    		}
    		catch (Exception e) {
    			System.out.println("Graphics  error: " + e);
    		}
    	}
    	
    
    	private void Render()
    	{
    		
    		if(doubleBuffering == null){
    			WIDTH = getWidth();
    			HEIGHT = getHeight();
    			doubleBuffering = createImage(WIDTH, HEIGHT);
    			if (doubleBuffering == null){
    				System.out.println("Can not create main Image");
    				return;
    			}
    		}
    		if(graphicsBuffer == null){
    			graphicsBuffer = doubleBuffering.getGraphics();
    		}
    		graphicsBuffer.setColor(Color.white);
    		graphicsBuffer.fillRect(0, 0, WIDTH, HEIGHT);
    		background.drawBackground(graphicsBuffer, this);
    		character.drawCharacter(graphicsBuffer, this);
    		
    		
    	}
    	
    	
    	
    	public void run() {
    		System.out.println("ArcadeGame::run() called");
    		while(running) { 
    			Update();
    			Render();
    			paintScreen();
    			try{
    				Thread.sleep(20);
    			}
    			catch(InterruptedException ex){
    				ex.printStackTrace();
    			}
    		}
    	}
    }
    When I press enter when on frame 0 the screen will go white.
    As the applet closes these error messages come up

    Graphics context error: java.lang.NullPointerException
    Graphics context error: java.lang.NullPointerException
    Graphics context error: java.lang.NullPointerException

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Calling another class to display images.

    Graphics context error: java.lang.NullPointerException
    Your error messages do not have a stack trace to show where the error occurred.
    Make sure your code uses printStackTrace() in the catch block. It looks like you are printing a message with no debugging information available.

    Find where that message is coming from and then find out what variable is null and find out why it is null.

  7. #7
    Nonoon is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Calling another class to display images.

    Had no idea posts had to be approved first. Boosting post counts through desperation haha |:

    I have done exactly that thanks. There are no more errors but the problem still remains the same.
    Could it possibly be my original menuimage covering the animation? Setting the menuimage to null when the enter key is pressed still just results in a blank white screen.

    Or I have just completely misunderstood on how objects work and are created?

    My first month of java!

    Adding systemprints on the animation. It looks like it isn't running at all when I try to play it from the menu.
    Last edited by Nonoon; 11-21-2011 at 03:54 AM. Reason: Further testing gave me more information.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Calling another class to display images.

    There are no more errors but the problem still remains the same.
    Adding systemprints on the animation. It looks like it isn't running at all when I try to play it from the menu.
    What do the printlns show about how your code is executing?

  9. #9
    Nonoon is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Calling another class to display images.

    Main::start() called
    Main::run() called

    These two come up when the applet begins.

    If I add printlns onto the play method.

    Main::Play() called
    Main::Play() called
    Main::Play() called

    Will be repeated constantly. None of the Animation class println come up unless I am running that class separately.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Calling another class to display images.

    If that is all that you are printing out then I don't know that you are printing out enough data to tell what your code is doing.

  11. #11
    Nonoon is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Calling another class to display images.

    Adding pintln to see if playanimation is ever set to true when pressing enter, it does so I just think I have just done things incorrectly. I think I need to do something with animation.paint() to actually draw the images again on the MainMenu.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Calling another class to display images.

    What is the purpose of the Animation class? You create an instance of it but never call any of its methods.
    Or am I looking at an older version of your code?

  13. #13
    Nonoon is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Calling another class to display images.

    Well the Animation class is to piece together the other classes that are Character() and Background() and displays them.

    I always thought once I create the new Animation(); it would call everything such as init() I'll try calling some of the methods together.

    This is the current version of the code still. Should I post up the classes for Character and Background as well?

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Calling another class to display images.

    it would call everything
    Nothing will be called. If the browser had created an instance of the class, the browser would call the applet's methods.
    Since you are creating the instance of the class, you are responsible for calling its methods and providing it any support that it needs.

    If you want anyone to help testing your code, you need to provide all the classes it uses.

  15. #15
    Nonoon is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Calling another class to display images.

    Main
    Java Code:
    package animation;
    
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferedImage;
    import java.net.URL;
    
    import javax.swing.ImageIcon;
    
    
    public class Menu extends Applet implements Runnable, KeyListener{
    
    
    	Image[] MenuImages = new Image[8];
    	int frame = 0;
    	boolean [] keys = new boolean[500];
    	boolean running = false;
    	private static final int PWIDTH = 800;   
    	private static final int PHEIGHT= 540;
    
    	private boolean onMenu = true;
    	private boolean onScreenShot = false;
    
    	private Image backgroundImage;
    	private Graphics backGraphics;
    
    	private boolean playgame = false;
    	private Animation animation; 
    	
    	
    	public void init () {
    
    		setBackground(Color.WHITE);
    		setSize(new Dimension(PWIDTH, PHEIGHT));
    		setFocusable(true);
    
    		System.out.println("Main::init() called"); 
    		try {
    			
    			
    			MenuImages[0] = loadImage("res/Menu1.png");
    			MenuImages[1] = loadImage("res/Menu2.png");
    			MenuImages[2] = loadImage("res/Menu3.png");
    			
    			MenuImages[3] = loadImage("res/Coming Soon.png");
    			MenuImages[4] = loadImage("res/Control.png");
    			
    			MenuImages[5] = loadImage("res/screen1.png");
    			MenuImages[6] = loadImage("res/screen2.jpg");
    			MenuImages[7] = loadImage("res/screen3.png");
    
    		} catch(Exception ex) {
    			ex.printStackTrace();
    		}
    
    
    	}
    
    	
    
    	public void start() 
    	{
    		running= true;
    		System.out.println("Main::start() called");
    		Thread th = new Thread(this); 
    		addKeyListener(this);
    
    		th.start();
    	}
    	
    	  public void playGame()
    	  {
    	   if (playgame == true);
    	     animation = new Animation();  
    	     animation.init();
    	  }
    
    	private Image loadImage( String filename )
    	{
    		
    		Image img = null;
    		URL url = this.getClass().getResource(filename);
    		System.out.println(url.toString());
    		img = Toolkit.getDefaultToolkit().getImage(url);
    		return img;
    		
    	}
    
    	public void gameDraw(){
    
    		if (backgroundImage == null){  
    			backgroundImage = createImage(PWIDTH, PHEIGHT);
    			if (backgroundImage == null) {
    				System.out.println("backgroundImage is null");
    				return;
    			}
    			else
    				backGraphics = backgroundImage.getGraphics( );
    		}
    
    		backGraphics.setColor(Color.white);
    		backGraphics.fillRect(0, 0, PWIDTH, PHEIGHT);
    		backGraphics.drawImage(MenuImages[frame], 0, 0, this);
    			     
    	}
    
        public void paint()                                               //This method displays either the
        {                                                                           //intro canvas or the gameplay canvas.
          
                animation.paintScreen();
            
        }
    	private void paintScreen()
    	{
    		
    		Graphics  g;
    		try {
    			g = this.getGraphics( );  
    			if ((g != null) && (backgroundImage != null))
    				g.drawImage(backgroundImage, 0, 0, null);
    			
    		}
    		catch (Exception e)
    		{ System.out.println("Graphics context error: " + e);  
    		      e.printStackTrace();
    		}
    	} 
    
    		
    	
    	private void menuKeys() {
    		
    
    		{
    			  
    			if(keys[KeyEvent.VK_UP] && onMenu){
    				frame--;
    
    				if(frame<0){
    					frame = 2;
    				}
    
    				keys[KeyEvent.VK_UP] = false;
    			}
    			if(keys[KeyEvent.VK_DOWN] && onMenu){
    
    				frame++;
    			
    				if(frame>2){
    					frame=0;
    				}
    				keys[KeyEvent.VK_DOWN] = false;
    			}			
    
    			if(keys[KeyEvent.VK_ENTER]){
    				onMenu = false;
    				if(frame==0){
    				//MenuImages[0] = null;
    				System.out.println("Main::Enter() called" + playgame); 
    			
    				playgame = true;
    				
    			
    				}
    				if(frame==1){
    					frame =4;
    				}
    				if(frame==2){
    					frame =5;
    					onScreenShot = true;
    				}
    				 
    				keys[KeyEvent.VK_ENTER ] = false;
    			}
    		}
    		if(keys[KeyEvent.VK_ESCAPE]){
    			frame=0;
    			onScreenShot = false;
    			onMenu = true;
    
    		}
    
    	}
    
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) {
    		// TODO Auto-generated method stub
    		keys[e.getKeyCode()] = true; 
    	}
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
    		keys[e.getKeyCode()] = false;   
    	}
    
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		{
    			
    			System.out.println("Main::run() called");
    			while(running)
    			{
    				playGame();
    			//	gameUpdate();
    				menuKeys();
    				paintScreen();
    				gameDraw();
    
    				try
    				{
    					Thread.sleep(40);
    				}
    				catch(InterruptedException ex)
    				
    				{
    				ex.printStackTrace();
    				}
    			}
    		}
    	}
    }
    Animation

    Java Code:
    package animation;
    
    import java.applet.Applet; 
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.Ellipse2D;
    
    import org.jbox2d.collision.AABB;
    import org.jbox2d.collision.CircleDef;
    import org.jbox2d.collision.CircleShape;
    import org.jbox2d.collision.PolygonDef;
    import org.jbox2d.collision.PolygonShape;
    import org.jbox2d.collision.Shape;
    import org.jbox2d.collision.ShapeType;
    import org.jbox2d.common.Vec2;
    import org.jbox2d.dynamics.Body;
    import org.jbox2d.dynamics.BodyDef;
    import org.jbox2d.dynamics.World;
    @SuppressWarnings("serial")
    
    /**
     * 
     */
    
    public class Animation extends Applet implements Runnable{
    	/**
         * Thread
         * 
         */
    	private Thread runThread ;
    	/**
         * Images and graphics
         * 
         */
    	private Image imageBuffer;
    	private Graphics backGraphics;
    	private boolean running;
    	private static int WIDTH;
    	private static int HEIGHT;
    	private Background bg;
    	private Character character;
    	
    	
    	public void init() {
    		System.out.println("ArcadeGame::init() called"); 
    		bg = new Background();
    		character = new Character();		
    	}
    	
    	/**
         * Load images
         * <p>
         * Load the images from filename
         * </p>
         * @param filename
         * @return image
         * 
         */
    	
        
    	public void start() {
    		runThread = new Thread(this); 
    		runThread.start();		
    		running = true;
    		System.out.println("ArcadeGame::start() called"); 
    	}
    	public void stop() {
    		running = false;
    		
    	}
    		
    	public void destroy() {
    		
    	}
    	
    	
    	public void gameUpdate() {
    		bg.updateScrollingFactor();	
    	}
    	
    	void paintScreen(){
    		
    		Graphics g;
    		try{
    			g = this.getGraphics();
    			if((g != null)&&(imageBuffer!=null)){
    				g.drawImage(imageBuffer, 0, 0, null);
    			}
    			Toolkit.getDefaultToolkit().sync();
    			g.dispose();
    		}
    		catch (Exception e) {
    			System.out.println("Graphics  error: " + e);
    			e.printStackTrace();
    		}
    	}
    	
    
    	private void gameRender()
    	{
    		
    		if(imageBuffer == null){
    			WIDTH = getWidth();
    			HEIGHT = getHeight();
    			imageBuffer = createImage(WIDTH, HEIGHT);
    			if (imageBuffer == null){
    				System.out.println("Can not create main Image");
    				return;
    			}
    		}
    		if(backGraphics == null){
    			backGraphics = imageBuffer.getGraphics();
    		}
    		backGraphics.setColor(Color.white);
    		backGraphics.fillRect(0, 0, WIDTH, HEIGHT);
    		bg.drawBackground(backGraphics, this);
    		character.drawPenguin(backGraphics, this);
    		
    		
    	}
    	
    	
    	
    	public void run() {
    		System.out.println("ArcadeGame::run() called");
    		while(running) { 
    			gameUpdate();
    			gameRender();
    			paintScreen();
    			try{
    				Thread.sleep(20);
    			}
    			catch(InterruptedException ex){
    				ex.printStackTrace();
    			}
    		}
    	}
    }
    Character

    Java Code:
    package animation;
    
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.image.ImageObserver;
    import java.net.URL;
    
    public class Character {
    	private Image character;
    	private int posx;
    	private int posy;
    	private int characterHeight;
    	private int cut1x;
    	private int cut2x;
    	private double currentFrame;
    	
    	public Character() {
    		super();
    		posx = 10;
    		posy= 340;
    		characterHeight = 58;
    		cut1x=40;
    		cut2x=80;
    		currentFrame=0;
    		try{
    			character = loadImage("res/character.png");
    		} catch(Exception ex1) {
    			ex1.printStackTrace();
    		}
    	}
    	
    	public Image getImgPenguin() {
    		return character;
    	}
    
    	public void setImgBackground(Image imgBackground) {
    		this.character = imgBackground;
    	}
    
    	
    	public void drawPenguin(Graphics g, ImageObserver IO) {
    		if (currentFrame>=0 && currentFrame<1) {
    			g.drawImage( character,posx,posy, posx+cut1x, posy+characterHeight, 0, 0, cut1x, characterHeight, IO);
    		}
    		else if (currentFrame>=1 && currentFrame<2) {
    			g.drawImage( character,posx,posy, posx+cut1x, posy+characterHeight, cut1x, 0, cut1x+40, characterHeight, IO);
    		}
    		else if (currentFrame>=2 && currentFrame<3){
    			g.drawImage( character,posx,posy, posx+cut1x, posy+characterHeight, cut2x, 0, cut2x+40, characterHeight, IO);
    		}
    		else {
    			g.drawImage( character,posx,posy, posx+cut1x, posy+characterHeight, cut1x, 0, cut1x+40, characterHeight, IO);
    		}
    		currentFrame+= 0.3;
    		if (currentFrame>4) {
    			currentFrame=0;
    		}
    		//System.out.println("currentframe"+currentFrame+"\n");
    		
    	}
    
    	private Image loadImage( String filename )
    	{
    		Image img = null;
    		URL url = this.getClass().getResource(filename);
    		System.out.println(url.toString());
    		img = Toolkit.getDefaultToolkit().getImage(url); 
    		return img;
    	}
    
    	
    }
    Background

    Java Code:
    package animation;
    
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.image.ImageObserver;
    import java.net.URL;
    
    public class Background {
    	private Image slowBackground;
    	private Image mediumBackground;
    	private Image fastBackground;
    	private double scrollingFactorVerySlow;
    	private int scrollingFactorSlow;
    	private int scrollingFactorQuick;
    	
    	public Background() {
    		super();
    		try{
    			slowBackground = loadImage("res/slow.jpg");
    			mediumBackground = loadImage("res/slow2.png");
    			fastBackground = loadImage("res/quick.png");
    		} catch(Exception ex2) {
    			ex2.printStackTrace();
    		}
    		scrollingFactorVerySlow = 0;
    		scrollingFactorSlow = 0;
    		scrollingFactorQuick = 0;
    	}
    	
    	protected void updateScrollingFactor(){
    		scrollingFactorVerySlow += 0.5;
    		scrollingFactorSlow += 1;
    		scrollingFactorQuick += 2;
    
    	}
    	
    	public Image getImgBackgroundSlow() {
    		return mediumBackground;
    	}
    
    	public void setImgBackground(Image imgBackground) {
    		this.mediumBackground = imgBackground;
    	}
    
    
    
    	private Image loadImage( String filename )
    	{
    		Image img = null;
    		URL url = this.getClass().getResource(filename);
    		System.out.println(url.toString());
    		img = Toolkit.getDefaultToolkit().getImage(url); 
    		return img;
    	}
    	
    	public void drawBackground(Graphics g, ImageObserver IO) {
    	
    		g.drawImage(slowBackground,(int)-scrollingFactorVerySlow,0, IO);
    		g.drawImage(mediumBackground,-scrollingFactorSlow,358, IO);
    		g.drawImage(fastBackground,-scrollingFactorQuick,0, IO);
    		
    		
    	}
    	
    	
    }
    This is all the code used so far. Really appreciate the help and explanations you have given me so far.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Calling another class to display images.

    What does the program do now when you execute it? Can you explain what you do, what the program does and what is wrong with what the program does?

  17. #17
    Nonoon is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Calling another class to display images.

    When it starts it will display a image, when the up and arrow keys are pressed it will change the image accordingly in the array. I am trying to have it so when I am on frame 0 of the array, when the enter key is pressed it will display the images in Animation() class. However when I do press enter nothing changes.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Calling another class to display images.

    I don't see much debugging code in the key listener. Is it being called?
    The component needs focus for the key listener to work.

  19. #19
    Nonoon is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Calling another class to display images.

    The keylistener is working since I can change between images. I added a println to check if the playgame boolean changes to true when enter key is pressed and it does.

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Calling another class to display images.

    I get a loop that you did not mention happening:
    ...
    ArcadeGame::init() called
    file:/D:/JavaDevelopment/Testing/ForumQuestions7/images/GreenCar.png
    file:/D:/JavaDevelopment/Testing/ForumQuestions7/images/RedCar.png
    file:/D:/JavaDevelopment/Testing/ForumQuestions7/images/BlueCar.png
    file:/D:/JavaDevelopment/Testing/ForumQuestions7/images/GreenCar.png
    ArcadeGame::init() called
    file:/D:/JavaDevelopment/Testing/ForumQuestions7/images/GreenCar.png
    file:/D:/JavaDevelopment/Testing/ForumQuestions7/images/RedCar.png
    file:/D:/JavaDevelopment/Testing/ForumQuestions7/images/BlueCar.png
    file:/D:/JavaDevelopment/Testing/ForumQuestions7/images/GreenCar.png
    ArcadeGame::init() called
    file:/D:/JavaDevelopment/Testing/ForumQuestions7/images/GreenCar.png
    file:/D:/JavaDevelopment/Testing/ForumQuestions7/images/RedCar.png
    file:/D:/JavaDevelopment/Testing/ForumQuestions7/images/BlueCar.png
    file:/D:/JavaDevelopment/Testing/ForumQuestions7/images/GreenCar.png
    ArcadeGame::init() called
    file:/D:/JavaDevelopment/Testing/ForumQuestions7/images/GreenCar.png
    file:/D:/JavaDevelopment/Testing/ForumQuestions7/images/RedCar.png
    file:/D:/JavaDevelopment/Testing/ForumQuestions7/images/BlueCar.png
    file:/D:/JavaDevelopment/Testing/ForumQuestions7/images/GreenCar.png

    0 error(s)

Page 1 of 2 12 LastLast

Similar Threads

  1. How to display Images?
    By Rectal Exambot in forum New To Java
    Replies: 12
    Last Post: 10-11-2010, 04:36 AM
  2. JavaHelp how to display images
    By fossildoc in forum New To Java
    Replies: 0
    Last Post: 04-04-2010, 06:42 AM
  3. How to display Images from MS access using JSP
    By redmaverick in forum JDBC
    Replies: 3
    Last Post: 11-30-2009, 09:28 AM
  4. display images
    By prof.deedee in forum AWT / Swing
    Replies: 10
    Last Post: 11-12-2009, 09:08 PM
  5. Dynamic display of images
    By gixerino in forum NetBeans
    Replies: 7
    Last Post: 01-15-2009, 02:55 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
  •