Results 1 to 4 of 4
  1. #1
    JavaCoconut is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Question How do I loop an image across a JPanel?

    So, I am having trouble making one single image loop across a JPanel. The code I am currently using only draws a single road (the image I am trying to loop) Here is what I am using:
    Java Code:
    public void drawSprite(Graphics2D g) {
            //gp is an instance of my GamePanel class, PWIDTH is the width of the panel, sprite is the image, locx is the x coordinate, locy is the y coordinate.
            int w = sprite.getWidth();
    
            if(tiled) {
            for(int i = 0; i > gp.PWIDTH; i += w) {
                locx = i;
                g.drawImage(sprite, null, locx, locy);
                }
            } else {
            g.drawImage(sprite, null, locx, locy);
        }
    }
    drawSprite(Graphics2D g) gets called in the render() method of my GamePanel class (Runnable JPanel)

    Thanks in advance for any help.

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

    Default Re: How do I loop an image across a JPanel?

    Can you post an image of what you're currently getting and what you want to get? Can you tell us more about your GUI and the JPanel's paintComponent method? Are you trying to animate a sprite or are you trying to tile with the sprite? Please fill in the details and assume that we know nothing about your current project or its code.

  3. #3
    JavaCoconut is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: How do I loop an image across a JPanel?

    Thanks for the quick reply and yep, sure. I am trying to tile one road sprite all across a JPanel, I also have a car sprite which is controlled by keyboard input to move along the road.

    This is my paintComponent() method:
    Java Code:
    public void paintComponent(Graphics g) {
            if(dbImage != null) {
            g.drawImage(dbImage, 0, 0, null);
            }
        }
    This is render():
    Java Code:
    private Graphics2D dbg;
    private Image dbImage;
    private Car car = new Car();
    private RoadSprite road = new RoadSprite();
    
    public void render() {
            if(dbImage == null) {
                dbImage = createImage(PWIDTH, PHEIGHT);
                if (dbImage == null) {
                    System.out.println("dbImage is null");
                    return;
                } else {
                    dbg = (Graphics2D) dbImage.getGraphics();
                }
            }
    
            dbg.setColor(Color.white);
            dbg.fillRect(0, 0, PWIDTH, PHEIGHT);
    
            dbg.setColor(Color.RED);
            dbg.drawString(String.valueOf(frame), 30, 30);
     
            road.drawSprite(dbg);
            car.drawSprite(dbg);
        }
    Here is an image of what I am currently getting:
    How do I loop an image across a JPanel?-game.png

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

    Default Re: How do I loop an image across a JPanel?

    Have you put in println statements to test the values of certain variables during your program's run? In particular, in your drawSprite method, you'll want to check the values of w, tiled, gp.PWIDTH, and locx.

Similar Threads

  1. How can I loop sounds in JPanel?
    By rajkobie in forum New To Java
    Replies: 4
    Last Post: 04-24-2011, 03:49 PM
  2. image in JPanel
    By poojarkg in forum AWT / Swing
    Replies: 4
    Last Post: 04-26-2010, 05:52 PM
  3. image in jPanel
    By idi in forum AWT / Swing
    Replies: 1
    Last Post: 03-13-2010, 09:30 PM
  4. Add Image to JPanel
    By Seonix in forum AWT / Swing
    Replies: 1
    Last Post: 08-12-2009, 01:54 PM
  5. how to set image to a jpanel
    By masa in forum AWT / Swing
    Replies: 1
    Last Post: 03-02-2009, 08:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •