import java.applet.Applet; import java.awt.Button; import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.geom.Rectangle2D; public class Pong2 extends Applet implements Runnable, KeyListener, ActionListener { Button resetBtn; Thread animation; //declares a thread called animation static final int REFRESH_RATE = 10; //Constant for “delay” ball myBall = new ball(500,300); paddle leftPad = new paddle(20, 20, 300, 500); paddle rightPad = new paddle(480, 20, 300, 500); boolean bLeftUp=false, bLeftDown=false, bRightUp=false, bRightDown=false; private int leftScore; private int rightScore; //declare other class data members public void init() { resetBtn = new Button("Reset Pong"); addKeyListener(this); resetBtn.addActionListener(this); add(resetBtn); setBackground( Color.black); addKeyListener(this); this.requestFocus(); } public void paint(Graphics g) { g.drawString("Left Score: " , 20, 20); g.drawString("Left Score: " + myBall.leftScore , myBall.appWidth/4, 20); drawNiceBox(g, "RESET" , 20,200, new Color(0, 255, 0, 100), new Color(0, 0, 0, 150), Color.BLACK, false); drawNiceBox(g, "Left Score: " + myBall.leftScore , myBall.appWidth/4, 20, new Color(0, 255, 0, 100), new Color(0, 0, 0, 150), Color.BLACK, false); drawNiceBox(g, "Right Score: " + myBall.rightScore , myBall.appWidth/2, 20, new Color(0, 255, 0, 100), new Color(0, 0, 0, 150), Color.BLACK, false); if(myBall.leftScore >= 10) { drawNiceBox(g, "LEFT WINS! " , 100, 50, new Color(230, 0, 0, 100), new Color(0, 0, 0, 150), Color.BLACK, false); // System.out.print("left has won"); } if(myBall.rightScore >= 10) { drawNiceBox(g, "RIGHT WINS! " , 100, 50, new Color(230, 0, 0, 100), new Color(0, 0, 0, 150), Color.BLACK, false); // System.out.print("Right has won"); } myBall.draw(g); leftPad.draw(g); rightPad.draw(g); } public void start() { animation = new Thread(this); if(animation != null) { animation.start(); } } public void run() { while(true) { //put code that runs in loop here repaint(); try { myBall.move(); myBall.bounce(20, leftPad.yPos(), 480, rightPad.yPos(), 5, 200, 5, 200); //Left Paddle if(bLeftUp) { leftPad.moveUp(); } else if(bLeftDown) { leftPad.moveDown(); } //Right Paddle if(bRightUp) { rightPad.moveUp(); } else if(bRightDown) { rightPad.moveDown(); } repaint(); Thread.sleep(REFRESH_RATE); }catch(Exception exc){}; } } public void stop() { animation = null; } //Pick up key pressed public void keyPressed (KeyEvent e) { char key = e.getKeyChar(); if((key == 'a') || (key == 'A')) { bLeftUp = true; } else if((key == 'z') || (key == 'Z')) { bLeftDown = true; } if((key == 'k') || (key == 'K')) { bRightUp = true; } else if((key == 'm') || (key == 'M')) { bRightDown = true; } } public void keyReleased (KeyEvent ke) { char key = ke.getKeyChar(); if((key == 'a') || (key == 'A')) { bLeftUp = false; } else if((key == 'z') || (key == 'Z')) { bLeftDown = false; } if((key == 'k') || (key == 'K')) { bRightUp = false; } else if((key == 'm') || (key == 'M')) { bRightDown = false; } } public void keyTyped (KeyEvent ke) { } public void theScore() { if(myBall.isRight()) { rightPad.raiseScore(); } if(myBall.isLeft()) { leftPad.raiseScore(); } } public void drawNiceBox(Graphics g, String text, int x, int y, Color col1, Color col2, Color col3, Boolean important) { int widthInPixels = 0; int height = 0; Rectangle2D bounds; if(important) { Font font = new Font("Arial", Font.BOLD, 16); FontMetrics metrics = new FontMetrics(font) { }; bounds = metrics.getStringBounds(text, null); widthInPixels = (int) bounds.getWidth(); g.setFont(font); }else{ Font font = new Font("Arial", Font.PLAIN, 12); FontMetrics metrics = new FontMetrics(font) { }; bounds = metrics.getStringBounds(text, null); widthInPixels = (int) bounds.getWidth(); g.setFont(font); } height = (int) bounds.getHeight() + 6; g.setColor(col1); g.fill3DRect(x, y, widthInPixels+10, height, true); g.setColor(col2); g.drawRect(x-1, y-1, widthInPixels+11, height+1); g.setColor(new Color(col3.getRed(), col3.getGreen(), col3.getBlue(), 30)); g.drawString(text, x+4, y+height-5); g.setColor(col3); g.drawString(text, x+5, y+height-6); } public void actionPerformed(ActionEvent e) { //Delete the SOPL and instead //put whatever code you want here //in pong, this would reset the scores to 0 // and reset the ball back. leftPad.setScore(0); rightPad.setScore(0); this.requestFocus(); repaint(); } } import java.awt.*; import java.applet.*; import java.awt.event.*; public class paddle { private int x; private int y; private boolean bUp; private boolean bRight; private int distance; private int appWidth; private int appHeight; private int score; public paddle(int nXpos, int nYpos, int appletHeight, int appletWidth) { x = nXpos; y = nYpos; appWidth = appletWidth; appHeight = appletHeight; score = 0; } public void moveUp() { if(y < appHeight - 10) { y += 3; } } public void moveDown() { if(y > 0) { y -= 3; } } public void draw(Graphics g) { g.setColor(Color.red); g.fillRect(x,y,5,200); } public void raiseScore() { score++; } public void setScore(int x) { score = x; } public void reset() { score = 0; } public int yPos() { return y; } public int xPos() { return x; } } import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.geom.Rectangle2D; public class ball { public int x; public int y; private boolean bUp; private boolean bRight; private int distance; public int appWidth; public int appHeight; public int leftScore; public int rightScore; public ball(int width, int height) { x = 20 + (int)(Math.random()*200); y = 20 + (int)(Math.random()*200); appWidth = width; appHeight = height; bUp = false; bRight = true; distance = 1; } public void move() { if(bUp) // Ball is moving up { y = y + 3; } if(bRight) // Ball is going right { x = x + 3; } if(!bUp) // Ball is going down { y = y - 3; } if(!bRight) //Ball is going left { x = x - 3; } } public void bounce(int leftX, int leftY, int rightX, int rightY, int rightWidth, int rightHeight, int leftWidth, int leftHeight) { if(x >= appWidth) // Ball is moving off of the right side { bRight = false; rightScore++; } //perhaps x = 0 is left side? if(x <= 0) { bRight = true; leftScore++; } if(y <= 0) // Ball is hitting floor { bUp = true; } if(y >= appHeight) // ball is hitting ceiling { bUp = false; } if((y >= leftY && y <= leftY + leftHeight) && (x <= leftX && x<=leftX+leftWidth)) { bRight = true; } if((y >= rightY && y <= rightY + rightHeight) && (x >= rightX && x<=rightX+rightWidth)) { bRight = false; } } public void drawNiceBox(Graphics g, String text, int x, int y, Color col1, Color col2, Color col3, Boolean important) { int widthInPixels = 0; int height = 0; Rectangle2D bounds; if(important) { Font font = new Font("Arial", Font.BOLD, 16); FontMetrics metrics = new FontMetrics(font) { }; bounds = metrics.getStringBounds(text, null); widthInPixels = (int) bounds.getWidth(); g.setFont(font); }else{ Font font = new Font("Arial", Font.PLAIN, 12); FontMetrics metrics = new FontMetrics(font) { }; bounds = metrics.getStringBounds(text, null); widthInPixels = (int) bounds.getWidth(); g.setFont(font); } height = (int) bounds.getHeight() + 6; g.setColor(col1); g.fill3DRect(x, y, widthInPixels+10, height, true); g.setColor(col2); g.drawRect(x-1, y-1, widthInPixels+11, height+1); g.setColor(new Color(col3.getRed(), col3.getGreen(), col3.getBlue(), 30)); g.drawString(text, x+4, y+height-5); g.setColor(col3); g.drawString(text, x+5, y+height-6); } public boolean isRight() { if(x <= 0) { return true; } return false; } public boolean isLeft() { if(x >= appWidth) { return true; } return false; } public void draw(Graphics g) { g.setFont(new Font("AR BERKLEY", Font.ITALIC, 24)); g.setColor(Color.blue); g.fillOval(x,y,10,10); } }