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

    Default 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.
    Trying to put a background in a program with an animation.-background.pngTrying to put a background in a program with an animation.-ryupunch.png

    Please help. I am asking for someone to suggest some code to have a background work while the animation runs. Thank you in advance.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default 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)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default 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

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    d00dlyd00 is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Trying to put a background in a program with an animation.

    Quote Originally Posted by DarrylBurke View Post
    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

    db
    It's from a book.
    Nevermind guys.

Similar Threads

  1. How to make Java program run in the background?
    By divs1210 in forum Advanced Java
    Replies: 7
    Last Post: 04-16-2011, 05:30 PM
  2. Animation
    By ryainad in forum Advanced Java
    Replies: 1
    Last Post: 04-04-2011, 05:52 PM
  3. Replies: 6
    Last Post: 07-21-2010, 06:15 AM
  4. Replies: 1
    Last Post: 07-07-2010, 09:25 AM
  5. need help about animation ?
    By h9h in forum Java 2D
    Replies: 1
    Last Post: 10-30-2009, 11:41 AM

Tags for this Thread

Posting Permissions

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