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

    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();
    	Character hero = new Character(g,400,600,50,50);
    	//draw (g,0,0,w,h);
    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();
    	//for (int i =- 100; i<500; i+= 10){
    		//g.fillOval(i, 100, 100, 100);
    		//}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){
    	image = createImage (w,h);
    public Graphics getSlateGraphics(){
    	return image.getGraphics();
    public void update (Graphics g){
    public void paint(Graphics g){
    	if (image == null) return;

    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.fillOval(x, y, w, h);
    		g.fillOval(x+30, y+10, 10, 10);
    		g.fillOval(x+10, y+10, 10, 10);
    	public void keyPressed(KeyEvent e) {
    	public void keyReleased(KeyEvent e) {
    	public void keyTyped(KeyEvent e) {

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


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