Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Need Help with Rendering the screen (with volatile images)

  1. #1
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Question Need Help with Rendering the screen (with volatile images)

    I'm experimenting with image scaling and volatile images, but whatever I try, I can't seem to get my code to actually draw anything.

    Code:
    package gravitygamesinteractive.scorpioengine.main;
     
    import java.awt.*;
     
    import javax.swing.JPanel;
     
    public class Main extends JPanel implements Runnable{
    	public static boolean isRunning=false;
    	public static boolean rightPressed, leftPressed, upPressed, downPressed, jumpPressed, actionPressed;
    	public Level level;
    	private Image screen;
     
    	public Main(){
    		start();
    	}
     
    	public void start(){
    		level=new Level();
     
    		isRunning=true;
    		new Thread(this).start();
    	}
     
    	public void tick(){
    		level.tick();
    	}
     
    	public void render(Graphics g){
    		//Graphics g = screen.getGraphics();
     
    		g.setColor(new Color(0,0,0));
    		g.fillRect(0, 0, 100, 100);
    		//level.paintComponent(g);
     
    		g = getGraphics();
    		g.drawImage(screen, 0, 0, Frame.size.width, Frame.size.height, 0, 0, Frame.realsize.width, Frame.realsize.height, null);
     
    		g.dispose();
    	}
     
    	public void run() {
    		screen = createVolatileImage(Frame.realsize.width, Frame.realsize.height);
     
    		while(isRunning){
    			tick();
    			render(screen.getGraphics());
     
    			try{
    				Thread.sleep(16);
    			}catch(Exception e){
     
    			}
    		}
    	}
     
    }

    The error handling says its on line 46, which is the call to the render method. I can't tell whether its a problem with the screen Image, the Volatile Image, or something else entirely.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,971
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Need Help with Rendering the screen (with volatile images)

    The error handling says its on line 46
    Can you copy the full text of the error message and paste it here?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Need Help with Rendering the screen (with volatile images)

    Sure, but it's not much:

    Quote Originally Posted by error message
    Exception in thread "Thread-1" java.lang.NullPointerException
    at gravitygamesinteractive.scorpioengine.main.Main.ru n(Main.java:46)
    at java.lang.Thread.run(Unknown Source)
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,971
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Need Help with Rendering the screen (with volatile images)

    Exception in thread "Thread-1" java.lang.NullPointerException
    What variable has the null value?

    Have you read the API doc for the createVolatileImage() method?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Need Help with Rendering the screen (with volatile images)

    Just read the doc here: VolatileImage (Java Platform SE 6)

    After trying to tweak my code, I'm still confused, and I'm still getting errors

    Code:
    package gravitygamesinteractive.scorpioengine.main;
     
    import java.awt.*;
    import java.awt.image.VolatileImage;
     
    import javax.swing.JPanel;
     
    public class Main extends JPanel implements Runnable{
    	public static boolean isRunning=false;
    	public static boolean rightPressed, leftPressed, upPressed, downPressed, jumpPressed, actionPressed;
    	public Level level;
    	private VolatileImage screen;
     
    	public Main(){
    		screen = createVolatileImage(Frame.realsize.width, Frame.realsize.height);
    		start();
    	}
     
    	public void start(){
    		level=new Level();
     
    		isRunning=true;
    		new Thread(this).start();
    	}
     
    	public void tick(){
    		level.tick();
    	}
     
    	public void renderOffscreen() {
            do {
                if (screen.validate(getGraphicsConfiguration()) ==
                    VolatileImage.IMAGE_INCOMPATIBLE)
                {
                    // old vImg doesn't work with new GraphicsConfig; re-create it
                    screen = createVolatileImage(Frame.realsize.width, Frame.realsize.height);
                }
                Graphics g = screen.createGraphics();
                //
                // miscellaneous rendering commands...
                //
                g.dispose();
            } while (screen.contentsLost());
     
     
     }
     
    	public void render(Graphics g){
    		 do {
    	            int returnCode = screen.validate(getGraphicsConfiguration());
    	            if (returnCode == VolatileImage.IMAGE_RESTORED) {
    	                // Contents need to be restored
    	                renderOffscreen();      // restore contents
    	            } else if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) {
    	                // old vImg doesn't work with new GraphicsConfig; re-create it
    	                screen = createVolatileImage(Frame.realsize.width, Frame.realsize.height);
    	                renderOffscreen();
    	            }
    	            g.drawImage(screen, 0, 0, this);
    	     } while (screen.contentsLost());
    	}
     
    	public void run() {
    		screen = createVolatileImage(Frame.realsize.width, Frame.realsize.height);
     
    		while(isRunning){
    			tick();
    			renderOffscreen();
    			render(screen.getGraphics());
     
    			try{
    				Thread.sleep(16);
    			}catch(Exception e){
     
    			}
    		}
    	}
     
    }

    Errors:
    Exception in thread "Thread-1" java.lang.NullPointerException
    at gravitygamesinteractive.scorpioengine.main.Main.re nderOffscreen(Main.java:32)
    at gravitygamesinteractive.scorpioengine.main.Main.ru n(Main.java:68)
    at java.lang.Thread.run(Unknown Source)
    So yeah, still a null pointer exception...
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,971
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Need Help with Rendering the screen (with volatile images)

    What variable has the null value? Why does it have a null value?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Need Help with Rendering the screen (with volatile images)

    So yeah, you're still not telling us which variable is null.

  8. #8
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Need Help with Rendering the screen (with volatile images)

    Quote Originally Posted by Norm View Post
    What variable has the null value? Why does it have a null value?
    Well, everything seems to be pointing to the "screen" Image, but oddly enough, I set it to a new Volatile Image twice.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  9. #9
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Need Help with Rendering the screen (with volatile images)

    You will want to post the actual line of code that is throwing the NPE.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,971
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Need Help with Rendering the screen (with volatile images)

    What was its value when you print it out for debugging?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Need Help with Rendering the screen (with volatile images)

    Quote Originally Posted by curmudgeon View Post
    You will want to post the actual line of code that is throwing the NPE.
    Lines 32 and 33: if (screen.validate(getGraphicsConfiguration()) ==
    VolatileImage.IMAGE_INCOMPATIBLE)

    Line 68: renderOffscreen();

    EDIT: The screen image is returning as null. (Sorry, I didn't see Norm's post right away...)
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,971
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Need Help with Rendering the screen (with volatile images)

    Why is the value of screen null? Where does it get its value from?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Need Help with Rendering the screen (with volatile images)

    Quote Originally Posted by Norm View Post
    Why is the value of screen null? Where does it get its value from?
    It should be coming from here:

    public Main(){
    		screen = createVolatileImage(Frame.realsize.width, Frame.realsize.height);
    		start();
    	}

    ...or here...

    public void run() {
    		screen = createVolatileImage(Frame.realsize.width, Frame.realsize.height);
     
    		while(isRunning){
    			tick();
    			renderOffscreen();
    			render(screen.getGraphics());
     
     
    			try{
    				Thread.sleep(16);
    			}catch(Exception e){
     
    			}
    		}
    	}
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,971
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Need Help with Rendering the screen (with volatile images)

    In post #4 I asked:
    Have you read the API doc for the createVolatileImage() method?
    When does it return a null value?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Need Help with Rendering the screen (with volatile images)

    Yes, I have read the API doc. The problem still seems to be this line:
    if (screen.validate(getGraphicsConfiguration()) == VolatileImage.IMAGE_INCOMPATIBLE){

    The screen variable returns as null, even though it is created in the run method (commented out the other time I created it). The problem may also be the "getGraphicsConfiguration", as I honestly have know clue what that method does.

    In case of the very small chance that it actually changes anything, I'm compiling against Java 1.6, as I'm not sure if 1.7 is out of beta yet.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,971
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Need Help with Rendering the screen (with volatile images)

    Does the screen variable have a value of null? If so any use of screen will cause the error.

    You need to find out where the screen variable gets the null value from!!!
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Need Help with Rendering the screen (with volatile images)

    Well, I'm completely stumped. I can't seem to figure out why the variable keeps coming up null. Some of the docs I've read say I need to set up a GraphicsConfigurations object, some say I don't. I've tried adding printlns in between every line of code, but I still can't figure it out. I'm honestly thinking I need to go with an alternate drawing method, or the one I was using before. Sorry for both giving up and wasting your guy's time.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  18. #18
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: Need Help with Rendering the screen (with volatile images)

    Posting an SSCCE helps a lot in providing context to your problem. You are asking us to guess what's null without code to show us what's null. Based upon the posts above, screen is instantiated in the constructor by calling getGraphicsConfiguration or maybe createVolatileImage (I'm confused about which given your code changes from post to post), I can guess you are are receiving the exception because screen is null. To quote the java 6 API, which should be studied before using the methods contain within - for getGraphicsConfiguration():

    If the Component has been created, but not yet added to a Container, this method returns null
    and for createVolatileImage:

    The return value may be null if the component is not displayable
    Has the component been added to a container (given the instantiation of screen occurs in the constructor in may of your code snippets...I seriously doubt it)?

Similar Threads

  1. why are using volatile in java
    By thirupathiswami017 in forum Object Oriented Programming
    Replies: 5
    Last Post: January 5th, 2013, 10:56 AM
  2. Replies: 1
    Last Post: November 19th, 2012, 10:25 AM
  3. Replies: 9
    Last Post: December 31st, 2011, 01:22 AM
  4. [SOLVED] JSF? not rendering.
    By newbie in forum Web Frameworks
    Replies: 2
    Last Post: March 21st, 2011, 09:45 PM
  5. volatile
    By sivaprakash in forum Java Theory & Questions
    Replies: 0
    Last Post: March 13th, 2010, 12:30 AM