Results 1 to 12 of 12
  1. #1
    Mage5000 is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default animation not working??

    im sure that yet again there is somethin silly im missing but ive mucked around with it for some time now and i still cant get it to work... all the images appear except for the one i want to have an animation to have affect on it when it moves left/right but the image dosent show even when its stationary... please if you can help me i will be very greatfull :)

    Java Code:
    import java.applet.*; 
    import java.awt.*; 
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    public class Game extends Applet implements Runnable, KeyListener{
     public int x, y;
     public boolean up, down, left, right;
     Image background; // Scenery
     Image Player, stationary, PlayerLeft1, PlayerLeft2, PlayerLeft3, PlayerLeft4, PlayerLeft5, PlayerLeft6, PlayerLeft7, PlayerLeft8, PlayerRight1, PlayerRight2, PlayerRight3, PlayerRight4, PlayerRight5, PlayerRight6, PlayerRight7, PlayerRight8; //Player
     MediaTracker track;
     public Image offscreen;
     public Graphics d;
    
     
     public Game(){
    	    System.out.println("I would like to say that I was");
    		System.out.println("helped massively by my wonderfull family member");
    		System.out.println("friend and love and I would like to thank her");
    		System.out.println("for her Love and care and assistance in artwork");
    		System.out.println("and giving her amazing voice into this game,");
    		System.out.println("thank you so much Amy Lauren Sadler");
     }
     
     public void init(){
    	 setSize(854, 480);
    	 this.setFocusable(true);
    	 addKeyListener(this);
    	 Thread th = new Thread(this);
    	 th.start();
    	 offscreen = createImage(854, 480);
    	 d = offscreen.getGraphics();
     } 
     
     public void run() {
    	 x = 100;
    	 y = 100;
    	 int counter = 0;
    	 while(true){
    		 counter++;
    			if (counter >=27){;
    				counter = 0;
    			}
    			if (left == true){
    				x--;
    				x--;
    				if (counter == 3){
    					Player = PlayerLeft1;
    				}
    				if (counter == 6){
    					Player = PlayerLeft2;
    				}
    				if (counter == 9){
    					Player = PlayerLeft3;
    				}
    				if (counter == 12){
    					Player = PlayerLeft4;
    				}
    				if (counter == 15){
    					Player = PlayerLeft5;
    				}
    				if (counter == 18){
    					Player = PlayerLeft6;
    				}
    				if (counter == 21){
    					Player = PlayerLeft7;
    				}
    				if (counter == 24){
    					Player = PlayerLeft8;
    				}
    			}
    			if (right == true){
    				x++;
    				x++;
    				if (counter == 3){
    					Player = PlayerRight1;
    				}
    				if (counter == 6){
    					Player = PlayerRight2;
    				}
    				if (counter == 9){
    					Player = PlayerRight3;
    				}
    				if (counter == 12){
    					Player = PlayerRight4;
    				}
    				if (counter == 15){
    					Player = PlayerRight5;
    				}
    				if (counter == 18){
    					Player = PlayerRight6;
    				}
    				if (counter == 21){
    					Player = PlayerRight7;
    				}
    				if (counter == 24){
    					Player = PlayerRight8;
    				}
    			}
    			if (up == true){
    				y-= 5;
    			}
    			if (down == true){
    				y++;
    			}
    			if (y <= 350 && up != true){
    				y += 10;
    			}
    		    repaint();
    			try {
    				Thread.sleep(20);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    	 }
    	} 
    
     public void paint(Graphics g) {
    	 track = new MediaTracker(this);
    	 d.clearRect(0, 0, 854, 480);
    	 background = getImage(getCodeBase(), "Background.png");
    	 track.addImage(background,0);
     	 d.drawImage(background, 0, 0, this);
     	 Player = getImage(getCodeBase(), "Player.png");
     	 track.addImage(Player, 0);
     	 d.drawImage(Player, x, y, this);
     	 offscreen.getGraphics();
    	 track.addImage(offscreen, 0);
    	 g.drawImage(offscreen, 0, 0, this);
      }
     
     public void keyPressed(KeyEvent e){
    	 if (e.getKeyCode() == 37){
    			left = true;
    		}
    		if (e.getKeyCode() == 38){
    			up = true;
    		}
    		if (e.getKeyCode() == 39){
    			right = true;
    		}
    		if (e.getKeyCode() == 40){
    			down = true;
    		}
     }
     
     public void keyReleased(KeyEvent e){
    	 if (e.getKeyCode() == 37){
    			left = false;
    			Player = stationary;
    		}
    		if (e.getKeyCode() == 38){
    			up = false;
    		}
    		if (e.getKeyCode() == 39){
    			right = false;
    			Player = stationary;
    		}
    		if (e.getKeyCode() == 40){
    			down = false;
    		}
     }
     
     public void keyTyped(KeyEvent e) {
     }
    
    public void update(Graphics g){
    	paint(g);
    }
    }

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

    Default Re: animation not working??

    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,189
    Rep Power
    19

    Default Re: animation not working??

    Notes:
    1. Learn to follow Java coding conventions. Variable names start with a lowercase letter.
    Code Conventions for the Java Programming Language: Contents

    2. Why, in this day and age, are you writing code for AWT components? Swing superseded AWT more than 10 years ago.

    3. The whole point of double buffering is to render the image before painting to the component. Your code renders the image in a painting method override, losing any advantage of double buffering. But as Swing components are double buffered by default, you don't need to roll your own double buffering.

    4. Never compare booleans to true or false.
    Java Code:
    //if (left == true){
    if (left) {
    5. To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem. Your stated problem is that an image is not displaying. A KeyListener and its associated logic are not part of that problem and add so much clutter that most members here, self included, won't even try to get to the root of the problem.

    I don't see where you initialize most of your Image type variables, but maybe that's just lost in all the needless clutter.

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

  4. #4
    AeneasTroy is offline Arma virumque cano
    Join Date
    Oct 2012
    Location
    Indianapolis
    Posts
    20
    Rep Power
    0

    Default Re: animation not working??

    It might actually be easier to do that as an array of images.

    For example
    Image player[] = new Image[8]

    You have the right idea and by the way, you can also do animations with timers. Use the Javax.swing.Timer, not the other one.


    You can't do this:
    public Graphics d; (as a global)

    public void paint(Graphics g){}

    will only paint on Graphics g. It won't do anything to your graphics d.

    To do what you are trying to do you have to use a class called an ImageBuffer. This is basically a blank canvas object that you draw on and can pass from method to method.
    The ImageBuffer stays invisible until you draw it as an image with Graphics g.drawImage();

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

    Default Re: animation not working??

    Quote Originally Posted by AeneasTroy View Post
    public void paint(Graphics g){}

    will only paint on Graphics g. It won't do anything to your graphics d.
    1. It will, if invoked with
    Java Code:
    paint(d);
    2. I think you missed the
    Java Code:
    g.drawImage(offscreen, 0, 0, this);
    at the end of the paint(...) override, after the entirely meaningless calls to
    Java Code:
    offscreen.getGraphics();
    track.addImage(offscreen, 0);
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    AeneasTroy is offline Arma virumque cano
    Join Date
    Oct 2012
    Location
    Indianapolis
    Posts
    20
    Rep Power
    0

    Default Re: animation not working??

    1. It will, if invoked with
    Java Code:
    paint(d);
    but Mage is wanting to repaint() it
    in the run method.

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

    Default Re: animation not working??

    Yes. And the (badly named) Graphics d is being used to paint to the context of the image it's associated with. There's no attempt there to use d to paint to the Applet.

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

  8. #8
    Mage5000 is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: animation not working??

    okay, i have managed to get d.draw oval to work with what i want but the player variable dosent paint anything...

    to try and cut down the gaggle of information ill give you what i have so far:-

    Java Code:
    import java.applet.*; 
    import java.awt.*; 
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.net.URL;
    
    public class Game extends Applet implements Runnable, KeyListener{
     public int x, y;
     public boolean up, down, left, right;
     Image background, foreground; // Scenery
     Image player, stationary, playerLeft1, playerLeft2, playerLeft3, playerLeft4, playerLeft5, playerLeft6, playerLeft7, playerLeft8, playerRight1, playerRight2, playerRight3, playerRight4, playerRight5, playerRight6, playerRight7, playerRight8; //Player
     MediaTracker track;
     public Image offscreen;
     public Graphics d;
     
     public void init(){
    	 setSize(854, 480);
    	 this.setFocusable(true);
    	 addKeyListener(this);
    	 Thread th = new Thread(this);
    	 th.start();
    	 offscreen = createImage(854, 480);
    	 d = offscreen.getGraphics();
     }
    
    public void paint(Graphics g) {
    	 track = new MediaTracker(this);
    	 d.clearRect(0, 0, 854, 480);
    	 background = getImage(getCodeBase(), "Background.png");
    	 track.addImage(background,0);
     	 d.drawImage(background, 0, 0, this);
     	 //player = getImage(getCodeBase(), player);
     	 //track.addImage(player,0);
    	 //d.drawImage(player, 0, 0, this);
     	 d.drawOval(x, y, 20, 20);
     	 foreground = getImage(getCodeBase(), "Foreground.png");
     	 track.addImage(foreground, 0);
     	 d.drawImage(foreground, 0, 0, this);
     	 offscreen.getGraphics();
    	 track.addImage(offscreen, 0);
    	 g.drawImage(offscreen, 0, 0, this);
      }

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

    Default Re: animation not working??

    1. Don't ever load resources or perform any I/O in a painting method override. Load the images in the init() method.
    3. You're not using MediaTracker correctly. See the example code in the API for the class below the comment
    Java Code:
    // First wait for the background image to fully load 
    // and paint.  Then wait for all of the animation 
    // frames to finish loading.
    2. Better still, for non-animated images (everything wxcept animated GIFs) use the preferred approach to image loading: ImageIO#read(...), which blocks until the image is completely loaded and doesn't require a MediaTracker.

    I'm still waiting for an answer to this:
    2. Why, in this day and age, are you writing code for AWT components? Swing superseded AWT more than 10 years ago.
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    Mage5000 is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: animation not working??

    okay now i get the error under getImage with: player = getImage(getCodeBase(), player);
    and nothing shows up

    in reply to your swing suggestion, i have only ever programmed with awt and dont actually know whats involved or whats different with swing, any tutorials ive found seem to be pretty much identical to what ive got now??

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

    Default Re: animation not working??

    For custom painting and animation, Swing has a very definite edge over AWT: Swing components are double buffered by default. And with Swing, you also get a pluggable LookAndFeel, which isn't available in AWT, along with improved performance and lowered demand on system resources -- the latter two being negligible in this day and age of fast computers, but were significant when Swing was introduced somewhere around 12 years ago. Swing is also cross-platform, which means your GUI can look the same on Windows, Linux or any other OS that supports a JRE.

    Here's the Swing painting tutorial: 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.

  12. #12
    Mage5000 is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: animation not working??

    okay thank you ill start doing it this way thank you :)

Similar Threads

  1. Rain animation help
    By Bestsanchez in forum AWT / Swing
    Replies: 2
    Last Post: 01-14-2012, 11:47 PM
  2. Animation
    By ryainad in forum Advanced Java
    Replies: 1
    Last Post: 04-04-2011, 05:52 PM
  3. need help about animation ?
    By h9h in forum Java 2D
    Replies: 1
    Last Post: 10-30-2009, 11:41 AM
  4. GUI Animation
    By serfster in forum New To Java
    Replies: 2
    Last Post: 06-11-2008, 03:37 AM
  5. Replies: 8
    Last Post: 05-28-2008, 07:00 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
  •