*
The moose likes Swing / AWT / SWT and the fly likes  key conflicts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark " key conflicts" Watch " key conflicts" New topic
Author

key conflicts

Nikos kat
Greenhorn

Joined: Jan 20, 2006
Posts: 20
I am designing a pong game but I am have trouble with key conflicts. can anyone get me any advice on keylistners or reccomend an online tutorial on the subject
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Can you explain in a bit more detail what problems you're having -- i.e., what you're doing, and what's not working as you'd expect? Let's continue this discussion over in the Swing/AWT/SWT/JFace forum -- I'm moving this thread there.


[Jess in Action][AskingGoodQuestions]
Nikos kat
Greenhorn

Joined: Jan 20, 2006
Posts: 20
Hello Ernest, thanks again for your help.

If you remember I'm trying my hand at game programing and I'm trying my own version of Pong. My code doesn't let me control both paddles at once using the keyboard, im considering trying to use 1 mouse and 1 keyboard.




Let me show you the code

import java.awt.*;

import javax.swing.*;
import java.awt.event.*;


//a crude version of pong with two green paddles and a green ball that doesn't move yet
public class PongGame extends JFrame implements Runnable
{

public static final int GAME_WIDTH = 1000;
public static final int GAME_HEIGHT = 600;
public static final int LEFT_PADDLE_FROM_LEFT = 10;
public static final int RIGHT_PADDLE_FROM_RIGHT = GAME_WIDTH - 10 - Paddle.WIDTH;

public static final int MOVE_PADDLE_UP = 1;
public static final int MOVE_PADDLE_DOWN = 2;
public static final int NO_MOVE = 0;

private int leftPaddleMovementStatus;
private int rightPaddleMovementStatus;

private Thread thread;
private boolean running = false;

private Ball ball;
private Paddle leftPaddle;
private Paddle rightPaddle;

//used in double duffering
private Image dbImage;
private Graphics dbg;
//used in game update to move paddles





public PongGame()
{

setVisible(true);

//start the ball right in the center of the game
ball = new Ball(GAME_WIDTH/2 - Ball.DIAMETER/2, GAME_HEIGHT/2 - Ball.DIAMETER/2);

leftPaddle = new Paddle(LEFT_PADDLE_FROM_LEFT, GAME_HEIGHT/2 - Paddle.HEIGHT/2 );
rightPaddle = new Paddle(RIGHT_PADDLE_FROM_RIGHT, GAME_HEIGHT/2 - Paddle.HEIGHT/2);

setUpListeners();

thread = new Thread(this);
thread.start();
}


private void setUpListeners() {
Container clientArea = getContentPane();
clientArea.setFocusable(true);
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_UP ){
rightPaddleMovementStatus = MOVE_PADDLE_UP;
}
if(e.getKeyCode() == KeyEvent.VK_DOWN ){
rightPaddleMovementStatus = MOVE_PADDLE_DOWN;
}
if(e.getKeyCode() == KeyEvent.VK_W ){
leftPaddleMovementStatus = MOVE_PADDLE_UP;
}
if(e.getKeyCode() == KeyEvent.VK_S ){
leftPaddleMovementStatus = MOVE_PADDLE_DOWN;
}
}});
}

public void run(){
running = true;
while(running){
gameUpdate();
render();
paintScreen();
try{
Thread.sleep(20);
}
catch(InterruptedException e){};
}
System.exit(0);
}

public void gameUpdate(){
//check that paddle does not run of screen, if safe move the paddle
if(leftPaddle.getPoint().y > 0 && leftPaddle.getPoint().y < GAME_HEIGHT - Paddle.HEIGHT
&& rightPaddle.getPoint().y > 0 && rightPaddle.getPoint().y < GAME_HEIGHT - Paddle.HEIGHT){

if(leftPaddleMovementStatus == MOVE_PADDLE_UP){
leftPaddle.getPoint().y -= 2;
}
if(leftPaddleMovementStatus == MOVE_PADDLE_DOWN){
leftPaddle.getPoint().y += 2;
}
if(rightPaddleMovementStatus == MOVE_PADDLE_UP){
rightPaddle.getPoint().y -= 2;
}
if(leftPaddleMovementStatus == MOVE_PADDLE_DOWN){
rightPaddle.getPoint().y += 2;
}
//reset status for next keylisten event
leftPaddleMovementStatus = NO_MOVE;
rightPaddleMovementStatus = NO_MOVE;
}
}

public void render(){


if(dbImage == null){
dbImage = createImage(GAME_WIDTH,GAME_HEIGHT);
if(dbImage == null){
System.out.println("error in making image");

}
else{
dbg = dbImage.getGraphics();
}
}

dbg.setColor(Color.black);
dbg.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
dbg.setColor(Color.green);

//draw paddles
int x = leftPaddle.getPoint().x;
int y = leftPaddle.getPoint().y;
dbg.fillRect(x, y, x + Paddle.WIDTH, y + Paddle.HEIGHT );
x = rightPaddle.getPoint().x;
y = rightPaddle.getPoint().y;
dbg.fillRect(x, y, x + Paddle.WIDTH, y + Paddle.HEIGHT );

//draw ball
dbg.fillOval(ball.getPoint().x, ball.getPoint().x,Ball.DIAMETER,Ball.DIAMETER);
}
/**
public void paint(Graphics gfx){
Container c = getContentPane();
Graphics g = c. getGraphics();

gfx.setColor(Color.black);
gfx.fillRect(0,0,22,22);
}
*/
private void paintScreen()
// actively render the buffer image to the screen
{
Container clientArea = getContentPane();
Dimension d = new Dimension(GAME_WIDTH, GAME_HEIGHT);
clientArea.setPreferredSize(d);

pack();
try {
Graphics g = clientArea.getGraphics(); // get the panel�s graphic context
if ((g != null) && (dbImage != null))
g.drawImage(dbImage, 0, 0, null);
g.dispose();
}
catch (Exception e)
{ System.out.println("Graphics not work");
}
}


public static void main(String[] args) {
new PongGame();
}

}


import java.awt.*;




public class Ball {

public static final int DIAMETER = 20;

private Point point;

public Ball(int xCoord, int yCoord){
point = new Point(xCoord,yCoord);
}

public Point getPoint(){
return point;
}
}

import java.awt.*;


public class Paddle {

public static final int WIDTH = 10;
public static final int HEIGHT = 40;

private Point point;

public Paddle(int xCoord, int yCoord){
point = new Point(xCoord,yCoord);
}

public Point getPoint(){
return point;
}
}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
have a look at the code in this thread, it might give you some ideas

http://forum.java.sun.com/thread.jspa?forumID=256&threadID=488614
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: key conflicts
 
Similar Threads
how can we update the Managed Bean data in Process Validation phase?
UML and beyond?
what to do for this error
Start or stop server with WAS v5
RMI server IP/Port values