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

    Exclamation 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.

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

    Default

    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

  1. Replies: 7
    Last Post: 12-03-2010, 04:47 AM
  2. java graphics question
    By bobCallahan24 in forum AWT / Swing
    Replies: 1
    Last Post: 01-15-2010, 06:04 AM
  3. Replies: 1
    Last Post: 07-16-2009, 02:15 PM
  4. Graphics with JFrame
    By kandt in forum New To Java
    Replies: 3
    Last Post: 12-02-2008, 12:51 AM
  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
  •