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

    Exclamation Object and Keyboard help.

    Hi I am working on a game and I need help making an object move via the w,a,s,d keys. Here is my code so far.

    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.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.JFrame;
    
    public class Maze{
    
    public Maze(){
    	
    	int w = 800;
    	int h = 800;
    	
    	
    	Slate slate = new Slate(w,h);
    	Graphics g = slate.getSlateGraphics();
    
    	g.setColor(Color.yellow);
    	
    	Character hero = new Character(g,400,600,50,50);
    	animate(slate,hero);
    	//draw (g,0,0,w,h);
    	slate.repaint();
    	
    	//anim(slate);
    	
    	
    }
    	
    public static void draw(Graphics g, int x, int y, int width, int height){
    	
    	if (height < 3) return;
    	
    	g.drawOval(x, y, width, height);
    	
    	
    }
    
    //public static void anim (Slate slate){
    	
    	//Graphics g = slate.image.getGraphics();
    	//g.setColor(Color.red);
    	
    	//for (int i =- 100; i<500; i+= 10){
    		
    		//g.fillOval(i, 100, 100, 100);
    		//slate.repaint();
    		//try{
    			//Thread.sleep(50);
    		//}catch (Exception e){}		
    	//}
    	
    //}
    
    public static void animate (Slate slate,Character c){
    	
    	Graphics g = slate.image.getGraphics();
    	
    	//if ()
    	
    }
    
    	
    }
    	
    
    
    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 the second part


    Java Code:
    package Game;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    public class Character implements KeyListener {
    	
    	public Character(Graphics g,int x, int y, int w, int h){
    		
    		g.setColor(Color.black);
    		g.fillOval(x, y, w, h);
    		g.setColor(Color.red);
    		g.fillOval(x+30, y+10, 10, 10);
    		g.setColor(Color.red);
    		g.fillOval(x+10, y+10, 10, 10);
    		
    		
    		
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) {
    		
    		
    		
    	}
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    		
    		
    	}
    
    	@Override
    	public void keyTyped(KeyEvent e) {
    		
    		
    	}
    
    }

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

    Default

    Your graphics code looks a bit different from most I've seen. Have you gone through any of the graphics tutorials on the Java tutorial site? If not, I recommend that you first start there, and also hit the section on Swing Timers while you're at it, so that you can drive the animation.

Similar Threads

  1. Keyboard
    By UJJAL DHAR in forum New To Java
    Replies: 4
    Last Post: 01-29-2011, 03:26 PM
  2. Press P On Keyboard
    By Feriscool in forum New To Java
    Replies: 2
    Last Post: 11-25-2010, 06:32 AM
  3. Keyboard not working
    By Robotuner in forum New To Java
    Replies: 0
    Last Post: 10-27-2009, 08:51 PM
  4. [Virtual KeyBoard] - Anybody know ?
    By Matheus Leandro Ferreira in forum Advanced Java
    Replies: 1
    Last Post: 09-30-2009, 09:22 PM
  5. Help with keyboard events?
    By Bibendum in forum New To Java
    Replies: 2
    Last Post: 11-02-2007, 03:51 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
  •