Results 1 to 2 of 2
- 02-10-2011, 02:04 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 83
- Rep Power
- 0
Question on Java Graphics, KeyBoard Events and JFrame
This is my code so far (not including my main and JButton etc... classes.
package Game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.Scanner;
import javax.swing.JFrame;
public class Maze {
BufferedImage img;
JFrame frame;
//Graphics g = img.getGraphics();
int x = 0;
int y = 0;
Graphics g;
Maze(){
frame = new JFrame();
frame.setTitle("Maze");
frame.getContentPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setSize(800, 800);
frame.setVisible(true);
frame.setBackground(Color.yellow);
frame.getContentPane();
img = new BufferedImage(frame.getWidth(),frame.getHeight(),B ufferedImage.OPAQUE);
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_DOWN){
System.out.println("key down was pressed");
g.drawRect(x, y-1, 10, 10);
g.fillRect(x, y-1, 10, 10);
g.setColor(Color.black);
}
if(e.getKeyCode()==KeyEvent.VK_UP){
System.out.println(".");
g.drawRect(x, y+1, 10, 10);
g.fillRect(x, y+1, 10, 10);
g.setColor(Color.black);
}
if(e.getKeyCode()==KeyEvent.VK_LEFT){
System.out.println("k");
g.drawRect(x+1, y, 10, 10);
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
System.out.println("lol");
g.drawRect(x-1, y, 10, 10);
}
}
{
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
}
}
I would like some advice on how I could make it so when the user pressed the arrow keys a rectangle moves on the screen. Thanks.
-
You've got several problems with your code, and I'll try to enumerate some of them:
- You're calling Thread.sleep in your Swing code on the Swing main thread, the EDT or event dispatch thread, and this will cause your entire application to go to to sleep -- not good. Better would be to use a Swing Timer to drive your animation.
- You're not doing any drawing in a component, so I imagine no graphics are occuring, not to mention animation. Swing gui's are tricky and graphics programming with animation is even trickier to do. You can't guess at how to do this, but instead have to study it first by reading the tutorials. You'll see that you should do your drawing in a paintComponent method of a class that extends JPanel or JComponent.
- You've got KeyListener methods without any viable KeyListener object. Again you will need to read the tutorials on how to use key listeners. Better still would be to use key bindings.
There are more issues, but this is a start. Again the key is to study the tutorials where you'll find all the information you'll need. Also if you search this and other fora you'll find example code on animations, some done well, some not so.
Best of luck and welcome.
Similar Threads
-
Preferred method for passing toolbar events to JFrame
By jcleland in forum AWT / SwingReplies: 7Last Post: 12-03-2010, 04:47 AM -
java graphics question
By bobCallahan24 in forum AWT / SwingReplies: 1Last Post: 01-15-2010, 06:04 AM -
Major help needed with drawing rectangles using JFrame and Mouse Events
By DamienCurr in forum New To JavaReplies: 1Last Post: 07-16-2009, 02:15 PM -
Graphics with JFrame
By kandt in forum New To JavaReplies: 3Last Post: 12-02-2008, 12:51 AM -
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