Results 1 to 12 of 12
Thread: animation not working??
- 10-04-2012, 10:37 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
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); } }
- 10-05-2012, 05:05 AM #2
Re: animation not working??
Continued from Java Applet image I/O exception (Applets forum at JavaRanch)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-05-2012, 05:16 AM #3
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.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.Java Code://if (left == true){ if (left) {
I don't see where you initialize most of your Image type variables, but maybe that's just lost in all the needless clutter.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-05-2012, 08:30 AM #4
Arma virumque cano
- Join Date
- Oct 2012
- Location
- Indianapolis
- Posts
- 20
- Rep Power
- 0
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();
- 10-05-2012, 09:19 AM #5
Re: animation not working??
1. It will, if invoked with
2. I think you missed theJava Code:paint(d);
at the end of the paint(...) override, after the entirely meaningless calls toJava Code:g.drawImage(offscreen, 0, 0, this);
dbJava Code:offscreen.getGraphics(); track.addImage(offscreen, 0);
Why do they call it rush hour when nothing moves? - Robin Williams
- 10-05-2012, 09:40 AM #6
Arma virumque cano
- Join Date
- Oct 2012
- Location
- Indianapolis
- Posts
- 20
- Rep Power
- 0
Re: animation not working??
but Mage is wanting to repaint() it1. It will, if invoked withJava Code:paint(d);
in the run method.
- 10-05-2012, 10:58 AM #7
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-05-2012, 06:31 PM #8
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
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); }
- 10-05-2012, 07:32 PM #9
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 comment2. 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.Java Code:// First wait for the background image to fully load // and paint. Then wait for all of the animation // frames to finish loading.
I'm still waiting for an answer to this:db2. Why, in this day and age, are you writing code for AWT components? Swing superseded AWT more than 10 years ago.Why do they call it rush hour when nothing moves? - Robin Williams
- 10-05-2012, 08:34 PM #10
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
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??
- 10-06-2012, 02:14 AM #11
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)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-06-2012, 08:07 PM #12
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Rain animation help
By Bestsanchez in forum AWT / SwingReplies: 2Last Post: 01-14-2012, 11:47 PM -
Animation
By ryainad in forum Advanced JavaReplies: 1Last Post: 04-04-2011, 05:52 PM -
need help about animation ?
By h9h in forum Java 2DReplies: 1Last Post: 10-30-2009, 11:41 AM -
GUI Animation
By serfster in forum New To JavaReplies: 2Last Post: 06-11-2008, 03:37 AM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks