rendered paste body
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Integer;
import java.lang.String;
class ball extends JComponent implements Runnable {
//the x and y starting coords of the ball
public static int ballX = 512, ballY = 400;
Thread ball;
static double theballY, theballX;
//draw the ball
public static void draw(Graphics g, int ballX, int ballY) {
Graphics2D g2 = (Graphics2D) g;
Shape c = new Rectangle(ballX, ballY, 50, 50);
g2.setPaint(Color.white);
g2.fill(c);
}
//ball thread code
ball() {
ball = new Thread(this);
ball.start();
}
public void run() {
while (ball.isAlive() == true) {
try {
ballX += theballX;
ballY += theballY;
if (ballY <= 0 || ballY >= 750) {
flipY();
}
if (ballX == drawpanel.paddleX - 50 &&
ballY >= drawpanel.paddleY &&
ballY <= drawpanel.paddleY + drawpanel.paddleHeight) {
flipX();
drawpanel.paddleHeight-=5;
drawpanel.score++;
drawpanel.updatescore();
}
if (ballX <= comppaddle.paddleX + 50 &&
ballY >= comppaddle.paddleY &&
ballY <= comppaddle.paddleY + 150) {
flipX();
}
if (ballX == -50 || ballX == 1100) {
ballX = 512;
ballY = 400;
drawpanel.score--;
drawpanel.updatescore();
}
if (ballX <= 1024 &&
ballX >= 974 &&
ballY >= drawpanel.paddleY &&
ballY <= drawpanel.paddleY + drawpanel.paddleHeight) {
flipY();
}
ball.sleep(1);
} catch (InterruptedException e) {
}
}
}
public static void flipX() {
theballX *= -1;
ballX += theballX;
}
public static void flipY() {
theballY *= -1;
ballY += theballY;
}
}
class paddle extends JComponent {
//draw the paddle
public void draw(Graphics g, int paddleX, int paddleY, int paddleHeight) {
Graphics2D g2 = (Graphics2D) g;
Shape c = new Rectangle(paddleX, paddleY, 50, paddleHeight);
g2.setPaint(Color.white);
g2.fill(c);
}
}
class drawpanel extends JPanel implements MouseMotionListener {
static ball theball = new ball();
static paddle playerpaddle = new paddle();
static comppaddle comppaddle = new comppaddle();
static int paddleHeight = 150;
static int paddleX = 974, paddleY = 400;
static int score = 0;
static String thescore;
static JLabel scorecounter;
drawpanel() {
addMouseMotionListener(this);
this.setBackground(Color.black);
//timer for the paddle and ball to repaint
ActionListener dostuff = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
repaint();
}
};
new Timer(0, dostuff).start();
scorecounter = new JLabel(thescore);
Font thefont = new Font("sansserif", Font.BOLD, 72);
scorecounter.setSize(100,100);
scorecounter.setLocation(350, 0);
scorecounter.setFont(thefont);
scorecounter.setForeground(Color.white);
this.add(scorecounter);
}
//draw the ball and paddles on the frame
public void paintComponent(Graphics g) {
super.paintComponent(g);
theball.draw(g, ball.ballX, ball.ballY);
comppaddle.draw(g);
playerpaddle.draw(g, paddleX, paddleY, paddleHeight);
}
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
paddleX = e.getX();
paddleY = e.getY();
if (paddleY >= 650) {
paddleY = 649;
} else if (paddleY <= 0) {
paddleY = 1;
}
if (paddleX > 974 || paddleX < 974) {
paddleX = 974;
}
playerpaddle.repaint();
}
public static int getbally() {
int ballY = theball.ballY;
return ballY;
}
public static void updatescore() {
thescore = String.valueOf(score);
scorecounter.setText(thescore);
}
}
class comppaddle extends paddle implements Runnable {
static int paddleX = 0, paddleY = 400;
public void draw(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Shape c = new Rectangle(paddleX, paddleY, 50, 150);
g2.setPaint(Color.white);
g2.fill(c);
}
Thread comppaddle;
comppaddle() {
comppaddle = new Thread(this);
comppaddle.start();
}
public void run() {
while (comppaddle.isAlive() == true) {
int c;
int ballY = 1;
for (c = 1; c <= 800; c++) {
ballY = drawpanel.getbally();
paddleY = ballY - 50;
}
repaint();
}
}
}
public class networkpong {
public static void main(String[] args) {
int tmp;
tmp = Integer.parseInt(args[0]);
ball.theballX = tmp;
ball.theballY = tmp;
drawpanel panel = new drawpanel();
JFrame window = new JFrame();
window.setUndecorated(true);
window.getContentPane().add(panel);
DisplayMode dm = new DisplayMode(1024, 800, 16, 60);
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice d = e.getDefaultScreenDevice();
d.setFullScreenWindow(window);
d.setDisplayMode(dm);
}
}