Nikos kat

Greenhorn
+ Follow
since Jan 20, 2006
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Nikos kat

many thanks for your advice
How do you use the object.wait() method. if you put a number in parenthesis is it the wait in milliseconds?

Is this as good as using Thread.wait()
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;
}
}
19 years ago
thanks for you advice Rob and Jeff, its all good fun
19 years ago
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
19 years ago
hello, I am having real conceptual problems about how the method paint(Graphics g) is called automatically and also why is their the mysterius graphics parameter
19 years ago
thanks for you advice, i'll have a think about it
19 years ago
Please does anyone have an explaination of why I can create an image to be used in double buffering in the render() method. thankyou

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



public class GamePanel extends JPanel implements Runnable
{

public static final int GAME_WIDTH = 600;
public static final int GAME_HEIGHT = 600;
public static final int LEFT_PADDLE_FROM_LEFT = 10;
public static final int RIGHT_PADDLE_FROM_RIGHT = 10;

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 GamePanel()
{
this.setSize(WIDTH, HEIGHT);
setVisible(true);

//start the ball right in the center of the game
ball = new Ball(WIDTH/2 - Ball.DIAMETER/2, 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() {
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();
paintToScreen();
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){

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

public void render(){

// this returns true
System.out.println(isVisible());

// this returns false
System.out.println(GraphicsEnvironment.isHeadless());
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);

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 );

}

public void paintToScreen(){
Graphics g = getGraphics();
g = dbg;
paintComponent(g);
}
}
19 years ago
I was wondering what this means in simple terms

a "native screen resource"
19 years ago
thanks for your help sir
19 years ago
I was wondering where I can find documentation on the perf class sun.misc.Perf and why doesn't the standard API have it in documentation
19 years ago
thanks for your help
19 years ago
what is the difference between an abstract class and an interface?
19 years ago
thanks vey much for your help
19 years ago
Im not sure what this means in the component class
19 years ago