Results 1 to 3 of 3
  1. #1
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Unhappy help with animation coding

    Hi I need help making my sprite named hero in my code move when I use my keyboard. I have the bindings/listeners working but my sprite does not do anything. Here is my code:

    Java Code:
    package Game;
    
    import java.awt.Color;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Event;
    import java.awt.Window;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    import javax.swing.Action;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.KeyStroke;
    
    public class Maze implements KeyListener{
    	boolean u;
        int w = 800;
        int h = 800;
    	int x = 400;
    	int y = 600;
        Slate slate = new Slate(w,h);
    	Graphics g = slate.getSlateGraphics();
    	Character hero = new Character(g,x,y);
    	
    public Maze(){
    	
    
    	
    	
    	//Slate slate = new Slate(w,h);
    	//Graphics g = slate.getSlateGraphics();
    	slate.setFocusTraversalKeysEnabled(false);
    	slate.setResizable(false);
    	slate.addKeyListener(this);
    	g.setColor(Color.yellow);
    	slate.repaint();
    	
    
    	
    	
    }
    
    @Override
    public void keyPressed(KeyEvent e) {
    
    	int KeyCode = e.getKeyCode();
    	
    	if(KeyCode == KeyEvent.VK_UP){
    	
    		System.out.println("Up");
    		y = y -10;
    		slate.repaint();
    			
    			try{
    				Thread.sleep(50);
    			}catch(Exception a){
    				System.out.println("I made a durp");
    			}
    			
    		}
    		else if(KeyCode == KeyEvent.VK_DOWN){
    		
    		System.out.println("Down");
    		y = y + 10;
    		slate.repaint();
    			
    		}
    		else if(KeyCode == KeyEvent.VK_LEFT){
    			
    		System.out.println("Left");	
    		x = x - 10;
    		slate.repaint();
    			
    		}
    		else if(KeyCode == KeyEvent.VK_RIGHT){
    
    		System.out.println("Right");
    		x = x + 10;
    		slate.repaint();
    		
    		}
    	
    	
    	
    }
    
    @Override
    public void keyReleased(KeyEvent e) {
    
    	e.consume();
    	
    	
    }
    
    @Override
    public void keyTyped(KeyEvent e) {
    	
    	e.consume();
    	
    	
    }	
    
    	
    
    
    class Slate extends JFrame{
    	
    	Image image;
    	
    public Slate(int w,int h){
    	
    	setBounds(800,800,w,h);
    	setBackground(Color.yellow);
    	setVisible(true);
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	image = createImage (w,h);
    	
    }
    
    public Graphics getSlateGraphics(){
    	return image.getGraphics();
    }
    public void update (Graphics g){
    	paint(g);
    }
    public void paint(Graphics g){
    	if (image == null) return;
    	g.drawImage(image,0,0,null);
    }
    }
    }

    And my character class:


    Java Code:
    package Game;
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    
    public class Character {
    	
    	public Character(Graphics g,int x, int y){
    		
    		g.setColor(Color.black);
    		g.fillOval(x, y, 50, 50);
    		g.setColor(Color.red);
    		g.fillOval(x+30, y+10, 10, 10);
    		g.setColor(Color.red);
    		g.fillOval(x+10, y+10, 10, 10);
    		
    	}
    
    }


    Thanks :D

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Excerpt from your code:
    Java Code:
    if(KeyCode == KeyEvent.VK_UP){
    	
    		System.out.println("Up");
    		y = y -10; // WHAT WILL REASSIGNING AN INT DO
    		slate.repaint();
    			
    			try{
    				Thread.sleep(50);
    			}catch(Exception a){
    				System.out.println("I made a durp");
    			}
    			
    		}
    Perhaps you want to redraw your character using the y variable? You could set up a method in the Character class that will accept two integers (for x and y coordinate) and a Graphics object to draw the character at new location given by x and y.

    Java Code:
    if(KeyCode == KeyEvent.VK_UP){
    	
    		System.out.println("Up");
                   // NEW CODE
    		hero.setCharacterCoordinates(g,0,-10);
    		slate.repaint();
    			
    			try{
    				Thread.sleep(50);
    			}catch(Exception a){
    				System.out.println("I made a durp");
    			}
    			
    		}
    Character has a method now called setCharacterCoordinates which takes a Graphics object and two integers (for x and y coordinates).


    By the way it is not best practice to draw to JFrame. You should instead draw onto JPanel using its paintComponent method.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Give your sprite int x and y fields and setter/accessor methods for these fields as well as increment method. Then increment the values in your binding code and call repaint. Do NOT call Thread.sleep on the EDT, the event dispatch thread, which is the main Swing thread as this will put your entire application to sleep. Instead use a Swing timer to drive the animation.

Similar Threads

  1. Replies: 4
    Last Post: 10-25-2010, 04:49 PM
  2. gui animation problems
    By helpisontheway in forum New To Java
    Replies: 2
    Last Post: 12-15-2009, 03:41 PM
  3. need help about animation ?
    By h9h in forum Java 2D
    Replies: 1
    Last Post: 10-30-2009, 12:41 PM
  4. Two sleeps in animation
    By George1935 in forum AWT / Swing
    Replies: 4
    Last Post: 10-13-2008, 01:47 AM
  5. GUI Animation
    By serfster in forum New To Java
    Replies: 2
    Last Post: 06-11-2008, 04:37 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
  •