/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pongnew;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.logging.*;
import javax.swing.*;
/**
*
* @author User
*/
public class Main extends JFrame implements Runnable{
JPanel p = new JPanel();;
Racket p1,comp;
Ball ball;
Thread t;
public Main(){
//Form info
super("Pong - made by Alex.");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400,300);
setResizable(false);
setVisible(true);
//JPanel
p.setBackground(new Color(0,155,0));
this.add(p);
//Players
p1 = new Racket(20, this.getHeight()/3, 20, 100, this.getHeight(), false,this);
comp = new Racket(this.getWidth()-40, this.getHeight()/3, 20, 100, this.getHeight(), true,this);
//Ball
ball = new Ball(WIDTH/2, HEIGHT/2, 10, 3, HEIGHT, WIDTH, this, p1, comp);
//KeyListeners
this.addKeyListener(p1);
this.addKeyListener(comp);
}
public static void main(String[] args) {
new Main();
}
public void paint(Graphics g){
super.paint(g);
//set graphics
p1.setGraphics(g);
comp.setGraphics(g);
ball.setGraphics(g);
//draw Players
p1.drawMe();
comp.drawMe();
//draw ball
ball.drawMe();
//Thread
t = new Thread(this);
t.start();
}
public void run() {
while(!ball.didLose()){
try { t.sleep(50);}
catch (InterruptedException ex) {}
ball.move();
if(ball.getX() - (ball.getDiameter()/2)<p1.getX() + p1.getWidth() || ball.getX() + (ball.getDiameter()/2)> p1.getX()){
this.getContentPane().getGraphics().setColor(p.getBackground());
this.getContentPane().getGraphics().fillRect(0, 0, WIDTH, HEIGHT);
p1.restart();
comp.restart();
ball.restart();
ball.setLost(true);
try { t.sleep(1000);}
catch (InterruptedException ex) {}
}
}
}
}