Results 1 to 4 of 4
  1. #1
    jmu2101 is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default how to move a rectangle with arrow keys,

    for some reason this only works for the right arrow key, and i commented out the code for the left arrow key. how do you edit the paint method in the keyhandler method below? or otherwise, how do you reference the keyhandler event/ keypressed in the paint method up top?

    import java.awt.Container;

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseAdapter;


    public class BreakOut extends javax.swing.JFrame
    {
    // constants used to define the game field and bounds
    public static final int GAME_WINDOW_HEIGHT = 512;
    public static final int GAME_WINDOW_WIDTH = 512;
    public static final int GAME_HEIGHT = 440;
    public static final int GAME_WIDTH = 440;
    private static final int BLOCK_WIDTH=90;
    private static final int BLOCK_HEIGHT=50;
    private Paddle p;


    private KeyHandler kh;

    public BreakOut()
    {
    p=new Paddle(100,Color.RED);
    Container container=getContentPane();
    container.setBackground(Color.WHITE);
    initComponents();
    }
    public void paint(Graphics g)
    {
    super.paint(g);
    g.setColor(p.getPadColor());

    if(p.isVisible())
    {
    g.fillRect(p.getXLocation(),100,70,20);
    }
    if(!p.isVisible())
    {
    g.clearRect(p.getXLocation()-10,100,70,20);
    p.setVisibility(true);
    repaint();
    }





    }

    public void initComponents()
    {
    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
    setSize(GAME_WINDOW_WIDTH, GAME_WINDOW_HEIGHT);
    setTitle("BreakOut");

    }

    public static void main(String[] args)
    {
    BreakOut game = new BreakOut();
    game.setVisible(true);
    }

    public class KeyHandler extends KeyAdapter
    {
    public void keyPressed(KeyEvent ke)
    {

    if(ke.getKeyCode()==KeyEvent.VK_RIGHT)
    {
    p.setVisibility(false);
    p.setXLocation(p.getXLocation()+10);
    repaint();
    }
    if(ke.getKeyCode()==KeyEvent.VK_LEFT)
    {
    //p.setVisibility(false);
    //p.setXLocation(p.getXLocation()-10);
    //repaint();
    }

    }

    }








    import java.awt.Color;
    public class Paddle
    {
    private int xlocation;
    private Color padColor;
    private boolean isVisible;

    public Paddle(int xlocation, Color padColor)
    {
    this.xlocation=xlocation;
    this.padColor=padColor;
    this.isVisible=true;
    }

    public int getXLocation()
    {
    return this.xlocation;
    }

    public Color getPadColor()
    {
    return this.padColor;
    }

    public void setXLocation(int xlocation)
    {
    this.xlocation=xlocation;
    }
    public void setPadColor(Color padColor)
    {
    this.padColor=padColor;
    }
    public boolean isVisible()
    {
    return this.isVisible;
    }
    public void setVisibility(boolean isVisible)
    {
    this.isVisible=isVisible;
    }

    }

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

    Default

    • Don't draw directly into the paint method of a JFrame but rather in the paintComponent method of a JPanel or JComponent that is added to the JFraem.
    • Don't call repaint() from with a paint or paintComponent method. You're lucky the paint manager is smart enough not to allow this to cause infintie recursion!
    • Read the graphics tutorials for more on this.
    • Please edit your post to use code tags so that your code is readable. Please see my signature below to see how.
    • Use debug statements (println's) in your key handling methods to see what method is called when.
    • You're far better off using key bindings and not key listeners for this sort of problem. The tutorial on this can help show you how.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    11

    Default

    hello jmu2101,
    please study the code of the following tutorial The Breakout game. I must say, the code is not state-of-the-art but it shows some useful patterns how to move a rectangle with keys. If you need help, post your questions here.

  4. #4
    jmu2101 is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default Re: how to move a rectangle with arrow keys,

    thanks very much

Similar Threads

  1. Arrow Keys?
    By Alerhau in forum New To Java
    Replies: 10
    Last Post: 06-17-2011, 07:45 PM
  2. Moving a ball with Arrow Keys
    By kekcklemen in forum Java Applets
    Replies: 5
    Last Post: 02-25-2011, 10:15 PM
  3. Wrong with Rectangle res = new Rectangle(0,0,0,0);???
    By jiapei100 in forum AWT / Swing
    Replies: 3
    Last Post: 09-25-2010, 03:39 PM
  4. To make slider resize rectangle and to move pictures
    By mneskovic in forum New To Java
    Replies: 11
    Last Post: 05-21-2010, 11:07 AM
  5. Replies: 0
    Last Post: 07-11-2008, 03:07 PM

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
  •