Results 1 to 4 of 4
- 04-28-2012, 08:04 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
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:
drawSprite(Graphics2D g) gets called in the render() method of my GamePanel class (Runnable JPanel)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); } }
Thanks in advance for any help.
-
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.
- 04-28-2012, 08:33 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
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:
This is render():Java Code:public void paintComponent(Graphics g) { if(dbImage != null) { g.drawImage(dbImage, 0, 0, null); } }
Here is an image of what I am currently getting: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); }
-
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
-
How can I loop sounds in JPanel?
By rajkobie in forum New To JavaReplies: 4Last Post: 04-24-2011, 03:49 PM -
image in JPanel
By poojarkg in forum AWT / SwingReplies: 4Last Post: 04-26-2010, 05:52 PM -
image in jPanel
By idi in forum AWT / SwingReplies: 1Last Post: 03-13-2010, 09:30 PM -
Add Image to JPanel
By Seonix in forum AWT / SwingReplies: 1Last Post: 08-12-2009, 01:54 PM -
how to set image to a jpanel
By masa in forum AWT / SwingReplies: 1Last Post: 03-02-2009, 08:36 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks