Results 1 to 4 of 4
- 03-19-2012, 06:26 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
Trying to put a background in a program with an animation.
I need help. I have the background, and I can see it in the program, but it refreshes at an ugly rate.
Here is the code.
Java Code:import java.awt.*; import javax.swing.*; import java.util.*; import java.awt.image.*; import java.net.*; public class AnimationTest extends JFrame implements Runnable { Image img = Toolkit.getDefaultToolkit().createImage("Background.png"); static int ScreenWidth =640; static int ScreenHeight = 480; Thread gameloop; Random rand = new Random(); //double buffer objects BufferedImage backbuffer; Graphics2D g2d; //sprite variables Image image; Point pos = new Point(100,325); //animation variables int currentFrame = 0; int totalFrames = 18; int animationDirection = 1; int frameCount = 0; int frameDelay = 20; public static void main(String[] args) { new AnimationTest(); } public AnimationTest() { super("Animation Test"); setSize(ScreenWidth,ScreenHeight); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //create the back buffer for smooth graphics backbuffer = new BufferedImage(ScreenWidth, ScreenHeight, BufferedImage.TYPE_INT_RGB); g2d = backbuffer.createGraphics(); //load the ryu animation strip Toolkit tk = Toolkit.getDefaultToolkit(); image = tk.getImage(getURL("ryuPunch.png")); gameloop = new Thread(this); gameloop.start(); } private URL getURL(String filename) { URL url = null; try { url = this.getClass().getResource(filename); } catch (Exception e) {} return url; } public void run() { Thread t = Thread.currentThread(); while (t == gameloop) { try { Thread.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } gameUpdate(); } } public void gameUpdate() { //clear the background g2d.setColor(Color.WHITE); g2d.fill( new Rectangle(0, 0, ScreenWidth-1, ScreenHeight-1) ); //draw the current frame of animation drawFrame(image, g2d, pos.x, pos.y, 6, currentFrame, 128, 128); // g2d.setColor(Color.WHITE); g2d.drawString("Position: " + pos.x + "," + pos.y, 10, 50); g2d.drawString("Animation: " + currentFrame, 10, 70); //see if it's time to animate frameCount++; if (frameCount > frameDelay) { frameCount=0; //update the animation frame currentFrame += animationDirection; if (currentFrame > totalFrames - 1) { currentFrame = 0; // pos.x = rand.nextInt(ScreenWidth-128); // pos.y = rand.nextInt(ScreenHeight-128); } else if (currentFrame < 0) { currentFrame = totalFrames - 1; } } repaint(); } public void paint(Graphics g) { g.drawImage(img,0,0,null); //draw the back buffer to the screen g.drawImage(backbuffer, 0, 0, this); } public void paintComponent(Graphics g) { // Draw the previously loaded image to Component. g.drawImage(img, 0, 0, null); } //draw a single frame of animation public void drawFrame(Image source, Graphics2D dest, int x, int y, int cols, int frame, int width, int height) { int fx = (frame % cols) * width; int fy = (frame / cols) * height; dest.drawImage(source, x, y, x+width, y+height, fx, fy, fx+width, fy+height, this); } }
These are the two pictures you will need to make it work.


Please help. I am asking for someone to suggest some code to have a background work while the animation runs. Thank you in advance.
- 03-19-2012, 06:49 PM #2
Re: Trying to put a background in a program with an animation.
Your indentation, like your code, is all over the place. If you expect volunteers to read your code and try to help, the least you can do is to ensure a consistent formatting style throughout.
Wht are you overriding both paint(...) and paintComponent(...)? With an override to paint(...) that doesn't call into the super implementation, paintComponent(...) will never be called.
Go through this Lesson: Performing Custom Painting (The Java Tutorials > Creating a GUI With JFC/Swing)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-19-2012, 06:58 PM #3
Re: Trying to put a background in a program with an animation.
You can't learn by copying and badly modifying, without first understanding, code you pick up on the net. And are you sure that posting it on a public forum isn't a violation of copyright?
1435458087SE6
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-19-2012, 08:08 PM #4
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
How to make Java program run in the background?
By divs1210 in forum Advanced JavaReplies: 7Last Post: 04-16-2011, 05:30 PM -
Animation
By ryainad in forum Advanced JavaReplies: 1Last Post: 04-04-2011, 05:52 PM -
trying to add image to background in JFrame...what's the problem in this program
By sahildave1991 in forum AWT / SwingReplies: 6Last Post: 07-21-2010, 06:15 AM -
Plz give me one demo program to set background image on jframe
By altu57 in forum AWT / SwingReplies: 1Last Post: 07-07-2010, 09:25 AM -
need help about animation ?
By h9h in forum Java 2DReplies: 1Last Post: 10-30-2009, 11:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks