Results 1 to 2 of 2
Thread: Object and Keyboard help.
- 02-19-2011, 12:25 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 83
- Rep Power
- 0
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) { } }
-
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
-
Keyboard
By UJJAL DHAR in forum New To JavaReplies: 4Last Post: 01-29-2011, 02:26 PM -
Press P On Keyboard
By Feriscool in forum New To JavaReplies: 2Last Post: 11-25-2010, 05:32 AM -
Keyboard not working
By Robotuner in forum New To JavaReplies: 0Last Post: 10-27-2009, 07:51 PM -
[Virtual KeyBoard] - Anybody know ?
By Matheus Leandro Ferreira in forum Advanced JavaReplies: 1Last Post: 09-30-2009, 08:22 PM -
Help with keyboard events?
By Bibendum in forum New To JavaReplies: 2Last Post: 11-02-2007, 02:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks