Results 1 to 8 of 8
  1. #1
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Repeat small image in background?

    Hey guys,

    I have a small 64x64 image that i want to repeat to fill a frame 640x640. How can i do this.

    Heres the code i tried to use:
    Java Code:
    	
            public void draw() {
    		x = 0;
    		y = 0;
    		while (x < 640) {
    			while (y < 640){
    				g.drawImage(ground, x, y, null);	
    				x += 64;
    				y +=64;
    				
    		        }
                    }
            }
    Thanks.
    Last edited by jammas615; 09-29-2011 at 12:39 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default Re: Repeat small image in background?

    Why are you incrementing y by 64 each time you increment x?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Repeat small image in background?

    My real problem is :
    Java Code:
    g.drawImage(ground, x, y, null);
    Im getting an exception error.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Repeat small image in background?

    Quote Originally Posted by jammas615 View Post
    ...
    Im getting an exception error.
    Which for some reason you are hiding from us?

  5. #5
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Repeat small image in background?

    That is the error:
    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at Map.draw(Map.java:37)
    which is this line:
    Java Code:
    g.drawImage(ground, x, y, null);

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Repeat small image in background?

    So your Graphics object, g, is likely null. Maybe you should show us how you're getting g? This kind of coding should be done from within a paintComponent method of a JPanel or other JComponent using the Graphics object passed in by the JVM, or it should be done using the Graphics object obtained from a BufferedImage. How are you getting your Graphics object?

  7. #7
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Repeat small image in background?

    The 'Map' class has a draw function:
    Java Code:
    public void draw(Graphics g) {
        g.drawImage(img, x, y, null);
    }
    and is called by the main class's draw function.
    Java Code:
    Map m = new Map();
    
    public void draw() {
        m.draw(null);
    }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Repeat small image in background?

    Quote Originally Posted by jammas615 View Post
    The 'Map' class has a draw function:
    Java Code:
    public void draw(Graphics g) {
        g.drawImage(img, x, y, null);
    }
    and is called by the main class's draw function.
    Java Code:
    Map m = new Map();
    
    public void draw() {
        m.draw(null);
    }
    So in essence you have a method like so:

    Java Code:
    public void draw(Graphics g) {
        g.drawImage(img, x, y, null);
    }
    And are wondering why you have a NullPointerException when you call it passing in a null parameter? What do you expect would happen when the code tries to call
    Java Code:
    null.drawImage(...);
    So the NPE should come as no surprise.

    The solution is to do as I recommended above. Call this method draw from within the paintComponent method of the JPanel that is displaying the map, and pass in the Graphics object that was passed into the paintComponent method.

Similar Threads

  1. use image for background
    By tony_stark in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2011, 08:20 PM
  2. Background image in Jwindow
    By Vinny2110 in forum AWT / Swing
    Replies: 1
    Last Post: 03-16-2011, 05:38 AM
  3. [Help] W/ background image
    By gundum584 in forum New To Java
    Replies: 9
    Last Post: 01-10-2011, 05:48 AM
  4. Background image
    By leiferouis in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 05:49 PM
  5. Image as background
    By Java.child in forum AWT / Swing
    Replies: 2
    Last Post: 10-02-2008, 11:37 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
  •