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 14 of 14

Thread: simple drawing

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default simple drawing

    Hi everyone, I wrote this simple program without following tutorials or anything, so MAYBE it's just a bunch of words that don't mean anything togheter, anyway, this is supposed to draw a background and an image on the screen (I'll not post the screen class, tell me if it's useful), but it doesn't work! here's the exceptions
    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    at mainclass.getImage(mainclass.java:31)
    at mainclass.draw(mainclass.java:53)
    at mainclass.run(mainclass.java:43)
    at mainclass.main(mainclass.java:26)

    and here's the code

    import java.awt.DisplayMode;
    import java.awt.Graphics2D;
    import java.awt.Image;
     
    import javax.swing.ImageIcon;
     
     
    public class mainclass {
    	private Graphics2D g;
     
    	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)};
     
     
     
     
    public static void main(String[] args) throws InterruptedException{
     
    	DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
    	mainclass p = new mainclass();
    	p.run();
     
    	}
     
    public Image getImage(){
    	 Image ball =  new ImageIcon(getClass().getResource("scena2.png")).getImage();
    	 return ball;
    }
    public Image getBg(){
    	Image bg = new ImageIcon(getClass().getResource("background.png")).getImage();
    	return bg;
    }
    public void run() throws InterruptedException{
    	Screen s = new Screen();
    	try{
    		DisplayMode dm = s.findFirstCompatibleMode(modes1);
    		s.setFullScreen(dm);
    		draw(g);
    		Thread.sleep(2000);
     
    	}finally{
    		s.restoreScreen();
    		}
    	}
     
    public void draw(Graphics2D g){
    	g.drawImage(getBg(), 0,0, null);
    	g.drawImage(getImage(), 30,90, null);
    }
    }


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

    Default Re: simple drawing

    Quote Originally Posted by chri View Post
    ...
    Exception in thread "main" java.lang.NullPointerException
    	at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    	at mainclass.getImage(mainclass.java:31)
    Hello and welcome!

    Your Image is likely null, possibly you're not looking in the right place for the file. Most important for us though is when you're posting about an exception, that you *show* us which line is causing the exception, here

    mainclass.getImage(mainclass.java:31)

    or line 31 of your mainclass.java file.

    --- Update ---

    I'm going to guess that it is either this line:
    Image ball =  new ImageIcon(getClass().getResource("scena2.png")).getImage();

    or a similar line. When you get images by resource, then the path will be relative to the class path, and the code above assumes that the image file is with the class files. So please let us know if this is true or not.

    As an aside, you'll want your code to conform to Java naming standards with your class names starting with an upper case letter. Do this and others (namely us!) will be able to better understand your code and be better able to help you!

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: simple drawing

    hi!
    Uhm, the image is in a folder called "res" in the main directory of the project, like so


    I'm sorry for the class name, I knew the method lowerUpper() thingy, but not this, I will take note of it

  4. #4
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: simple drawing

    I would edit my first post, but the forum won't let me see that....
    anyway, I forgot to put he res folder as a class folder, now I did it and the first exception disappeared but the other three are still there...
    the exceptions make me suppose that the problem is in the draw method, but I can't figure out what is wrong there

    the exceptions' lines are:
    line 53/52:
    g.drawImage(getBg(), 0,0, null);
    g.drawImage(getImage(), 30,90, null);
    line 43:
    draw(g);
    line 26:
    p.run();

    (sorry if my english isn't perfect, this isn't my native language )

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: simple drawing

    Please post the full text of the error messages like you did in post #1
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: simple drawing

    More questions and comments:
    • Is it again a NPE that you're seeing?
    • Can you show show us the latest file tree of your project (like you showed us above)? Specifically where the images are now located?
    • Have you changed the path that you're using to get the image resource?
    • It's not a good idea to read in images from within a paint or paintComponent method as that will slow down program graphics and make the perceived responsiveness of your program deteriorate.
    • If the images aren't too big, you don't want to read them in each time you need to use them. Rather if possible, read them in once in put them in a variable or collection.
    • Your English language skills seem pretty good to me. I don't think there's a need to apologize as you're doing quite well.

  7. The Following User Says Thank You to curmudgeon For This Useful Post:

    chri (December 25th, 2012)

  8. #7
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: simple drawing

    @Norm: sorry, here it is
    Exception in thread "main" java.lang.NullPointerException
    	at mainclass.draw(mainclass.java:53)
    	at mainclass.run(mainclass.java:43)
    	at mainclass.main(mainclass.java:26)

    @curmudgeon
    1)I don't know what a NPE is :S
    2)
    3)no, it is the same, I just went to the project's properties and added the res folder as a class folder in Java build path>libraries
    4)so should I declare the images outside the methods?
    5)as above
    6) Thank you, but sometimes I'm not sure of what I'm writing, so I feel better saying that

  9. #8
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: simple drawing

    quick suggestion for the forum...this thing that I can't see a post unless a moderator approves it is frustrating...I don't know if my post has been posted correctly of if I'm waiting here for an answer to nothing... :\

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: simple drawing

    Exception in thread "main" java.lang.NullPointerException
    at mainclass.draw(mainclass.java:53)
    Look at line 53 and find the variable with the null value. Then backtrack in the code to see why that variable has a null value.

    what a NPE is
    NullPointerException
    If you don't understand my answer, don't ignore it, ask a question.

  11. #10
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: simple drawing

    The forum won't show unapproved images or code until the poster has posted a minimum number of posts here (I think). This safeguard is in place to reduce the number spam posts, and most here would say that it works pretty well. We'll try to approve your post as quickly as possible, so please have patience and it will be rewarded!

    --- Update ---

    More suggestions:
    • Don't use the "default" package but instead create and use packages for your code.
    • Have your image folder be a folder off one of the packages, perhaps the base package, of your project.

  12. The Following User Says Thank You to curmudgeon For This Useful Post:

    Norm (December 25th, 2012)

  13. #11
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: simple drawing

    Quote Originally Posted by Norm View Post
    Look at line 53 and find the variable with the null value. Then backtrack in the code to see why that variable has a null value.
    The only variable involved there is g, a graphics2d object, I don't see how it could be null...

  14. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: simple drawing

    Where is g assigned a value? If you are not sure what is in g, print out its value with println()
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    chri (December 25th, 2012)

  16. #13
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: simple drawing

    oh god I feel so stupid, I forgot to write the method getGraphics that is in the screen class -.-
    now I don't get any exception anymore, but the images don't show...I only get a gray screen

    Now it works, I had just forgot to implement a bunch of methods from my screen class, thanks to everyone for the help!!

  17. #14
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: simple drawing

    Quote Originally Posted by curmudgeon View Post
    The forum won't show unapproved images or code until the poster has posted a minimum number of posts here (I think). This safeguard is in place to reduce the number spam posts, and most here would say that it works pretty well. We'll try to approve your post as quickly as possible, so please have patience and it will be rewarded!
    yes but, I don't know if it's possible, couldn't you at least make that the poster can see his own post, but with some kind of advice that let the poster know that the post is visible only to him unless a moderator approves it...

Similar Threads

  1. "Simple" Shape drawing!
    By incorsair in forum Object Oriented Programming
    Replies: 7
    Last Post: November 7th, 2012, 05:14 PM
  2. [SOLVED] Problem with a simple drawing program
    By Saiimon in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 24th, 2012, 03:45 PM
  3. Drawing Polygrams
    By Methos in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 26th, 2012, 07:36 PM
  4. Drawing a pentagon
    By Zomosa in forum AWT / Java Swing
    Replies: 7
    Last Post: October 11th, 2011, 10:21 PM
  5. Drawing
    By toxikbuni in forum Java Theory & Questions
    Replies: 0
    Last Post: April 20th, 2010, 02:43 PM