Results 1 to 4 of 4
- 08-09-2011, 11:42 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 11
- Rep Power
- 0
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;
}
}
-
- 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.
- 08-10-2011, 08:20 AM #3
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.
- 09-27-2011, 01:13 AM #4
Member
- Join Date
- Aug 2011
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
Arrow Keys?
By Alerhau in forum New To JavaReplies: 10Last Post: 06-17-2011, 07:45 PM -
Moving a ball with Arrow Keys
By kekcklemen in forum Java AppletsReplies: 5Last Post: 02-25-2011, 10:15 PM -
Wrong with Rectangle res = new Rectangle(0,0,0,0);???
By jiapei100 in forum AWT / SwingReplies: 3Last Post: 09-25-2010, 03:39 PM -
To make slider resize rectangle and to move pictures
By mneskovic in forum New To JavaReplies: 11Last Post: 05-21-2010, 11:07 AM -
How to navigate a SWT table cells with arrow keys
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 03:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks