please help, my program isnt work properly!!!
Author |
Message |
Bo0sT
|
Posted: Sun Mar 02, 2008 4:09 am Post subject: please help, my program isnt work properly!!! |
|
|
I wrote this program to teach myself how to use keyListeners (one of the things on my list of concepts that ill need to make games) and it isnt working properly
code: | import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ArrowKeyMovement extends JFrame implements KeyListener
{
static final int sizeX = 500;
static final int sizeY = 500;
static int circleX = 200;
static int circleY = 200;
static int keyInt;
static String keyString;
public static void main (String[]args)
{
new ArrowKeyMovement();
}
public ArrowKeyMovement()
{
setSize(sizeX,sizeY);
setVisible(true);
setTitle("Arrow Key Movement");
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint (Graphics g)
{
addKeyListener(this);
g.setColor(Color.white);
g.fillRect(0,0,sizeX,sizeY);
g.setColor(Color.blue);
g.fillOval(circleX,circleY,50,50);
g.drawString("Circle X: "+circleX,100,100);
g.drawString("Circle Y: "+circleY,100,120);
}
public void keyPressed (KeyEvent e)
{
keyInt = e.getKeyCode();
keyString = e.getKeyText(keyInt);
if (keyString == "Up")
{
circleY--;
repaint();
}
if (keyString == "Down")
{
circleY++;
repaint();
}
if (keyString == "Left")
{
circleX--;
repaint();
}
if (keyString == "Right")
{
circleX++;
repaint();
}
}
public void keyTyped (KeyEvent e)
{
}
public void keyReleased (KeyEvent e)
{
}
} |
when it is run, you can move the ball around with the arrow keys but its not incrementing how i'd like it to, id like it to move up, down, left or right by 1 grid spot each time u hit the button, run it, iv added X and Y display so that you can see what im talking about, thank you. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
OneOffDriveByPoster
|
Posted: Sun Mar 02, 2008 10:31 am Post subject: Re: please help, my program isnt work properly!!! |
|
|
Bo0sT @ Sun Mar 02, 2008 4:09 am wrote: code: | public ArrowKeyMovement()
{
setSize(sizeX,sizeY);
setVisible(true);
setTitle("Arrow Key Movement");
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint (Graphics g)
{
addKeyListener(this); |
You see an interesting pattern... The first key changes the number by 1, the next by 2, then by 3...
By the way, I notice that you are comparing Strings with ==. I am not sure how safe that is, but you could avoid those String comparisons by using the int constants in KeyEvent like VK_UP. |
|
|
|
|
|
HeavenAgain
|
Posted: Sun Mar 02, 2008 10:48 am Post subject: RE:please help, my program isnt work properly!!! |
|
|
i see some exponential equation going on there
you added the keylistener at the wrong place and caused a recursion going... |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Sun Mar 02, 2008 11:04 am Post subject: Re: RE:please help, my program isnt work properly!!! |
|
|
HeavenAgain @ Sun Mar 02, 2008 10:48 am wrote: you added the keylistener at the wrong place and caused a recursion going... I quoted the code that I did for a reason... |
|
|
|
|
|
Bo0sT
|
Posted: Sun Mar 02, 2008 1:08 pm Post subject: Re: please help, my program isnt work properly!!! |
|
|
oh ok, thanks, can you show me how i would write the VK_UP thing, and were to put my addKeyListener? and yes i did notice the incrementing by 1 then 2 ect =) |
|
|
|
|
|
Bo0sT
|
Posted: Sun Mar 02, 2008 2:49 pm Post subject: Re: please help, my program isnt work properly!!! |
|
|
aww sweet i fixed it! all i had to do was move the addKeyListener lol, anyways its working now, check it out
code: |
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ArrowKeyMovement extends JFrame implements KeyListener
{
static final int sizeX = 500;
static final int sizeY = 500;
static int circleX = 200;
static int circleY = 200;
static int keyInt;
static String keyString;
public static void main (String[]args)
{
new ArrowKeyMovement();
}
public ArrowKeyMovement()
{
addKeyListener(this);
setSize(sizeX,sizeY);
setVisible(true);
setTitle("Arrow Key Movement");
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint (Graphics g)
{
g.setColor(Color.white);
g.fillRect(0,0,sizeX,sizeY);
g.setColor(Color.blue);
g.fillOval(circleX,circleY,50,50);
g.drawString("Circle X: "+circleX,100,100);
g.drawString("Circle Y: "+circleY,100,120);
}
public void keyPressed (KeyEvent e)
{
keyInt = e.getKeyCode();
keyString = e.getKeyText(keyInt);
if (keyString == "Up")
{
circleY--;
repaint();
}
if (keyString == "Down")
{
circleY++;
repaint();
}
if (keyString == "Left")
{
circleX--;
repaint();
}
if (keyString == "Right")
{
circleX++;
repaint();
}
}
public void keyTyped (KeyEvent e)
{
}
public void keyReleased (KeyEvent e)
{
}
}
|
|
|
|
|
|
|
|
|