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.