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

Thread: I can not display Image

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default I can not display Image

    Hi, why wont this work!!! >.<
    I get this error
    Exception in thread "Thread-2" java.lang.NullPointerException
    at main.render(main.java:104)
    at main.run(main.java:84)
    at java.lang.Thread.run(Unknown Source)
    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
     
    public class main extends Canvas implements Runnable {
     
    		private static final long serialVersionUID = 1L;
     
    		public static final int WIDTH = 240;
    		public static final int HEIGHT =  WIDTH/12*9;
    		public static final int SCALE = 3;
    		public static final String NAME = "Half Life 3";
     
    		private JFrame frame;
     
    		public boolean running = false;
    		public int tickCount = 0;
     
    		private	Graphics g ;
     
    		private ImageIcon img = new ImageIcon("res\background.jpg");
    		private Image back = img.getImage();
     
     
    		public main(){
    			setMinimumSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
    			setMaximumSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
    			setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
     
    			frame = new JFrame(NAME);
    			ImageIcon icon  = new ImageIcon("res/Half-life.png");
    			frame.setIconImage(icon.getImage());
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			frame.setLayout(new BorderLayout());
     
    			frame.add(this,BorderLayout.CENTER);
    			frame.pack();
     
    			frame.setResizable(false);
    			frame.setLocationRelativeTo(null);
    			frame.setVisible(true);
     
    		}
     
    		public void run(){
    			long lastTime = System.nanoTime();
    			double nsPerTick = 1000000000D/60D; //Nano Sec per tick D represents its type double same as doing .0
     
    			int ticks = 0;
    			int frames = 0;
     
    			long lastTimer = System.currentTimeMillis();
    			double delta = 0 ; // How many nano seconds have gone by so far. Once we hit one, we will -1 from it.
     
    			init();
     
    			while(running){
     
    				long now = System.nanoTime();
    				delta += (now - lastTime) / nsPerTick;
    				lastTime = now;
    				boolean shouldRender = true;
    				while(delta >= 1){
    					ticks++;
    					tick();
    					delta -=1;
    					shouldRender = true;
    				}
     
    				try{
    				Thread.sleep(2);
    				}catch(Exception e){}
     
    				if(shouldRender){
    				frames++;
    				render();
    				}
     
    				if(System.currentTimeMillis() - lastTimer >= 1000){
    					lastTimer += 1000;
    					System.out.println(ticks+" Ticks, "+frames+" Frames");
    					frames = 0;
    					ticks = 0;
    				}
    			}
    		}
     
     
    		public void tick(){
    			tickCount++;
    		}
     
    		public void render(){
     
    		  //  g.drawImage(back,0,0,WIDTH/2,HEIGHT/2,null);
    		g.drawRect(0, 0, WIDTH, HEIGHT);
     
    		}
     
    		public void init(){
    			int index = 0;
    			//loops through reds green and blues;
    		}
     
    		public synchronized void start() {
    			running = true;
    			new Thread(this).start();	
     
    		}
     
    		public synchronized void stop() {
    			running = false;
    		}
    		public static void main(String [] args){
     
    			new main().start();
     
    		}
     
     
    	}


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: I can not display Image

    Ugh, that hurts, sorry.
    Never ever mix lightweight and heavyweight components.
    Work on the EDT.
    Read this: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    And name classes methods and variables according to the coding conventions.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: I can not display Image

    Quote Originally Posted by sakonpure6 View Post
    Hi, why wont this work!!!
    private Graphics g ;

    is not initialized. And however, keeping a Graphics like this variabile not necessarily is a good thing (depending on what Graphics you intend).
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I can not display Image

    Another problem is the path to the image:
    new ImageIcon("res\background.jpg")
    The "\b" is a special character, not the two characters \ and b.
    Either escape the \ with another \ to make "...\\b..."
    or use / ".../b..." I think that forward slashes will work on Windows. I use them all the time
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Image in Java will not display
    By Shaw2380 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: March 30th, 2013, 08:50 PM
  2. Image Doesn't Display with Qualified Image Path????
    By swaginator in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 31st, 2012, 12:29 AM
  3. Can't display an image...
    By barebackingtiger in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 25th, 2012, 07:21 PM
  4. Trouble getting an image to display.
    By Skyhigh32 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 23rd, 2011, 07:52 AM
  5. Display Image (png)
    By vimalaranjan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 3rd, 2011, 06:44 PM