Ball KeyListener Problem I Guess.. Have a look

Main.class

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;

public class Main extends Applet implements Runnable{

private Image img;
private Graphics doubleG;
private int width = 800;
private int height = 600;



Ball b;

@Override
public void init() {
	setSize(width, height);
	
}


@Override
public void run() {
	
		while(true){
			b.update(this);
			repaint();
			try {
				Thread.sleep(17);
			} catch (InterruptedException e){
				e.printStackTrace();
			}
		}
	
}
@Override
public void start(){
	Thread thread = new Thread(this);
	thread.start();
	b = new Ball();
	
	
} 

@Override
public void stop() {
	
}

@Override
public void destroy() {
	
}

@Override
public void paint(Graphics g) {
	b.paint(g);
}

//Double buffering
@Override
public void update(Graphics g) {
	if(img == null){
		img = createImage(this.getSize().width, this.getSize().height);
		doubleG = img.getGraphics();
	}
	doubleG.setColor(getBackground());
	doubleG.fillRect(0, 0, this.getSize().width, this.getSize().height);
	doubleG.setColor(getForeground());
	paint(doubleG);
	
	g.drawImage(img, 0, 0, this);
}

}

/////////////////////////////////////////////////////////////////////////////

Ball.class

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Ball implements KeyListener{

private int x = 0;
private int y = 0;
private int radius = 50;
private double dx;
private double dy;
private double gravity = 10;
private double energyloss = .8;
private double friction = .35;
private double dt = .2;
private static boolean right = false;
private static boolean left = false;
private static boolean up = false;
private static boolean down = false;

public Ball(){
	
}
public void update(Main m){
	
	// X düzlemi
	if(x + dx > m.getWidth() - radius - 1){
		x = m.getWidth() - radius -1;
		dx *= -1;
	}
	else if(x + dx < 0){
		x = 0;
		dx *= -1;
	}
	
	else{
		x += dx;		
	}
	
	
	// Y düzlemi
	
	if(y > m.getHeight() - radius - 1){
		y  = m.getHeight() - radius -1;
		dy *= energyloss;
		dy *= -1;
	}
	
	else{
		dy += gravity * dt;
		y += dy*dt - .5 * gravity *dt * dt ;
		y += dy;					
	}
	
	
}

public void paint(Graphics g) {
	g.setColor(Color.RED);
	g.fillOval(x, y, radius, radius);
}


@Override
public void keyPressed(KeyEvent e) {
	int keycode = e.getKeyCode();
	
		if(keycode == KeyEvent.VK_RIGHT){
			right = true;
		}
		if(keycode == KeyEvent.VK_LEFT){
			left = true;
		}
		if(keycode == KeyEvent.VK_DOWN){
			down = true;
		}
		if(keycode == KeyEvent.VK_UP || keycode == KeyEvent.VK_SPACE){
			up = true;
		}
		moveLeft();
		moveRight();
		moveUp();
		moveDown();
	}


@Override
public void keyReleased(KeyEvent e) {
	int keycode = e.getKeyCode();
		if(keycode == KeyEvent.VK_RIGHT){
			right = false;
		}
		if(keycode == KeyEvent.VK_LEFT){
			left = false;
		}
		if(keycode == KeyEvent.VK_DOWN){
			down = false;
		}
		if(keycode == KeyEvent.VK_UP || keycode == KeyEvent.VK_SPACE){
			up = false;
		}
		moveLeft();
		moveRight();
		moveUp();
		moveDown();
		
	
}

@Override
public void keyTyped(KeyEvent e) {
	// TODO Auto-generated method stub
	
}

public void moveRight(){
	if(right)	{
		if(dx +1 <10){
			if(dx < -5){
				dx = +3;
			}
			dx += 2;
		}
	}
}

public void moveLeft(){
	if(left){
		if(dx -1 > -10){
			if(dx > 5){
				dx = -3;
			}
		dx -= 2;
		}
	}
}

public void moveUp(){
	if(up){
		if(dy > -30){
			dy = -30;
		}
	}
}

public void moveDown(){
	if(down){
		dy = +30;
	}
}

}
///////////////////////

I split up my ball events and main thinks so I have two class as a result but my ball is neither let me control him nor comply with my physic rules. Why is that can anybody help me please ? Thank you.

Right under “b = new Ball();”, add this:

addKeyListener(b);

haha, classic mistake.

Well, it didn’t work.

And yet it hasn’t solved :slight_smile:

Magically, it has solved after ten minutes lol, thank you so much :slight_smile:

It doesn’t run properly all the time, anyone knows why is that?

///

Exception in thread “Thread-4” java.lang.NullPointerException
at Main.run(Main.java:30)
at java.lang.Thread.run(Unknown Source)
///

This is the error.

I added this

try{
					b.update(this);
					b2.update(this);
				}catch(NullPointerException e){
					e.fillInStackTrace();
				}

and my problem has solved completely. I did this but I don’t really know what I did :smiley:

NullPointerExceptions shouldn’t be catch but fixed :wink:
The problem is probably that you start the thread and afterwards set the ball, so at the beginning the ball reference is empty in run().