commit beaf35f96dcd19c87a4fc7c79c5561ff157388f7
parent ef331de19768af577578594ae2ed8ebc3294abce
Author: mpizzzle <michael.770211@gmail.com>
Date: Fri, 1 Mar 2019 18:57:15 +0000
updating PongGame to use swing components
Diffstat:
7 files changed, 322 insertions(+), 306 deletions(-)
diff --git a/Pong/PongArea.java b/Pong/PongArea.java
@@ -1,43 +0,0 @@
-package Pong;
-
-import java.awt.*;
-
-public class PongArea extends Panel {
- PongGame myApplet = null;
- Image paddle;
- Color[] colours = new Color[] {Color.white, Color.blue, Color.green, Color.red, Color.yellow};
- int colourno=0;
-
- public PongArea(PongGame parent) {
- myApplet = parent;
- paddle = parent.getImage(myApplet.getCodeBase(), "Assets\\" + "paddle.gif");
- }
-
- public void paint (Graphics g) {
- if (myApplet == null) return;
- final int w = getBounds().width;
- final int h = getBounds().height;
- g.setColor(Color.gray);
- g.fillRect(0, 0, w, h);
- FontMetrics fm = getFontMetrics(getFont());
-
- g.setColor(Color.black);
- int asc = fm.getAscent() + 4;
-
- if (myApplet.playerPosition != null) {
- // show the player's paddle
- //g.drawString("|", myApplet.playerPosition.x, myApplet.playerPosition.y);
- g.drawImage(paddle, myApplet.playerPosition.x, myApplet.playerPosition.y,Color.gray, null);
-
- // show the ball
- colourno=0;//<-------remove this for confusion mode!
- for (int i=0; i < myApplet.ballPositions.length; i++) {
- if (myApplet.ballPositions[i].x > -1)
- //g.drawImage(pongImage, myApplet.ballPositions[i].x, myApplet.ballPositions[i].y,null);
- g.setColor(colours[colourno++]);
- if (colourno >= colours.length) colourno = 0;
- g.drawString("O", myApplet.ballPositions[i].x, myApplet.ballPositions[i].y);
- }
- }
- }
-}
diff --git a/Pong/PongFrame.java b/Pong/PongFrame.java
@@ -1,10 +0,0 @@
-package Pong;
-
-import java.awt.Frame;
-
-public class PongFrame extends Frame {
-
- public PongFrame() {
- setBounds(200, 200, 200, 200);
- }
-}-
\ No newline at end of file
diff --git a/Pong/PongGame.java b/Pong/PongGame.java
@@ -1,245 +0,0 @@
-/* Copyright (c) Mike Percival 2004 */
-/* Pong game Created April 2004 */
-
-package Pong;
-
-import java.awt.*;
-import java.awt.event.*;
-import java.applet.*;
-
-public class PongGame extends Applet implements KeyListener, Runnable {
- // all member variables 'static' because shared with the ball action thread
- private static PongArea area;
- private static boolean finished = false;
- private static boolean ingame = false;
- private boolean juststarted = false;
- private static int level = 1;
- protected static Point[] ballPositions;
- protected static Point playerPosition;
- private Thread balls;
-
- protected static int ballNumber = 1;
- protected static int RIGHTEDGE = 1200;
- protected static final int LEFTEDGE = 0;
- protected static final int TOPEDGE = 0;
- protected static int BOTTOMEDGE = 1000;
- protected static int BALL_XAMOUNT=3;
- protected static int BALL_YAMOUNT=3;
- protected static int PLAYER_XAMOUNT=12;
- protected static int PLAYER_YAMOUNT=12;
- private static final int X = 1;
- private static final int Y = 2;
- private static final int NOT=3;
- private static final int DELAY = 15;
- boolean[] goingLeft, goingRight, goingUp, goingDown;
- boolean deadball = false;
- int timeSinceLastBall=0;
-
- public void init() {
-
- setLayout(null);
- setBackground(Color.gray);
-
- area = new PongArea(this);
- add(area);
- FontMetrics fm = getFontMetrics(getFont());
- RIGHTEDGE = getBounds().width - 1;
- BOTTOMEDGE = getBounds().height - 1;
- setVisible(true);
- area.setBounds(0,0,RIGHTEDGE,BOTTOMEDGE);
- area.setVisible(true);
-
- addKeyListener(this);
- area.addKeyListener(this);
- area.requestFocus();
-
- balls = new Thread(this);
- balls.start();
- }
-
- public void start() {
- // called by Applet Viewer or Browser
- doLevel(1);
- }
-
- public void doLevel(int level) {
- // this runs one 'level'
- // create arrays of ball balls here.
- // Maybe each level will have different numbers?
- juststarted = true;
- deadball=false;
- ballNumber = 1;
- ballPositions = new Point[ballNumber];
- goingLeft = new boolean[ballNumber];
- goingRight = new boolean[ballNumber];
- goingUp = new boolean[ballNumber];
- goingDown = new boolean[ballNumber];
- for (int i=0; i < ballNumber; i++) {
- ballPositions[i] = new Point(0,0);
- goingLeft[i] = true;
- goingRight[i] = false;
- goingUp[i] =false;
- goingDown[i] =true;
- }
-
- showStatus("Started level " + level);
-
- // randomly position 1 player, x balls
- playerPosition = new Point(RIGHTEDGE-20, 0);
-
- for (int i = 0; i < ballNumber; i++) {
- ballPositions[i] = randomPosition();
- }
-
- // paint the panel here
- area.repaint();
- ingame = true;
- }
-
- private Point randomPosition() {
- Point p = new Point( (int) (Math.random() * RIGHTEDGE) / BALL_XAMOUNT * BALL_XAMOUNT,
- (int) 0);
- if (p.x >= RIGHTEDGE*2/3) p.x = p.x - RIGHTEDGE/3;
- return(p);
- }
-
- synchronized void addABall() {
- System.out.println("ADD A BALL!");
- Point[] newBallPos = new Point[ballNumber+1];
- boolean[] newgl=new boolean[ballNumber+1], newgr=new boolean[ballNumber+1], newgu=new boolean[ballNumber+1], newgd=new boolean[ballNumber+1];
- for (int i = 0; i < ballNumber; i++) {
- newBallPos[i] = new Point(ballPositions[i]);
- newgl[i] = goingLeft[i];
- newgr[i] = goingRight[i];
- newgu[i] = goingUp[i];
- newgd[i] = goingDown[i];
- }
- newBallPos[ballNumber] = randomPosition();
- newgl[ballNumber] = true;
- newgr[ballNumber] = false;
- newgu[ballNumber] = false;
- newgu[ballNumber] = true;
- ballPositions = newBallPos;
- goingLeft = newgl;
- goingRight = newgr;
- goingUp = newgu;
- goingDown = newgd;
- ballNumber++;
- }
-
- void seeIfBallHitWallFloorOrCeiling(int ballno) {
- if (ballPositions[ballno].x <= 0) { // ball hit the left wall
- goingRight[ballno] = true;
- goingLeft[ballno] = false;
- }
- else if (ballPositions[ballno].y <= 0) {// ball hit the ceiling
- goingUp[ballno]=false;
- goingDown[ballno]=true;
- }
- else if (ballPositions[ballno].y >= BOTTOMEDGE-BALL_YAMOUNT) {// ball hit the floor
- goingDown[ballno]=false;
- goingUp[ballno]=true;
- }
- else if (ballPositions[ballno].x >= RIGHTEDGE) { // ball reached the right edge
- deadball=true;
- level--; // because it will be incremented in a minute and we want to stay on the same 'level'
- ingame = false;
- System.out.println("LOZER!");
- }
- }
-
- private boolean isPlayerPosition(Point p) {
- return(p.x >= playerPosition.x &&
- p.y >= playerPosition.y &&
- p.y <= playerPosition.y + 154);
- }
-
- public void finished() {
- finished = true;
- System.exit(0);
- }
-
- public String getAppletInfo() {
- return ("Pong Game");
- }
-
- private Point moveBall(int ballno, Point original) {
- Point result = new Point(original);
- if (goingLeft[ballno]) result.x = result.x - BALL_XAMOUNT;
- else if (goingRight[ballno]) result.x = result.x + BALL_XAMOUNT;
-
- if (goingUp[ballno]) result.y = result.y - BALL_YAMOUNT;
- else if (goingDown[ballno]) result.y = result.y + BALL_YAMOUNT;
- if (isPlayerPosition(result)) {
- goingRight[ballno]=false;
- goingLeft[ballno]=true;
- }
- else seeIfBallHitWallFloorOrCeiling(ballno);
- return(result);
- }
-
- // this class will use just the key pressed event
- public void keyPressed(KeyEvent e) {
- if (ingame) {
- if (!
- ((e.getKeyCode() == KeyEvent.VK_DOWN && playerPosition.y >= (BOTTOMEDGE - PLAYER_YAMOUNT)) ||
- (e.getKeyCode() == KeyEvent.VK_UP && playerPosition.y <= (TOPEDGE + PLAYER_YAMOUNT)))) {
- // valid move: move the player and then move the balls
- if (e.getKeyCode() == KeyEvent.VK_DOWN) {
- playerPosition.y += PLAYER_YAMOUNT;
- }
- else if (e.getKeyCode() == KeyEvent.VK_UP) {
- playerPosition.y -= PLAYER_YAMOUNT;
- }
- for (int i=0; i < ballNumber; i++) {
- if (isPlayerPosition(ballPositions[i])) {
- goingRight[i]=false;
- goingLeft[i]=true;
- }
- }
- area.repaint();
- } // if valid key press
- }
- else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
- doLevel(level++);
- }
- }
-
- public void run() {
- while (true) {
- if (juststarted) {
- try {Thread.sleep(750);}
- catch (Exception e) {}
- juststarted = false;
- }
- try {Thread.sleep(DELAY);}
- catch (InterruptedException e){}
- if (ingame) {
- timeSinceLastBall += DELAY;
- //System.out.println(String.valueOf(timeSinceLastBall));
- if (timeSinceLastBall >= 10000 ) {
- addABall();
- timeSinceLastBall = 0;
- }
- // for each ball, move the ball closer to the player
- for (int i=0; i < ballPositions.length; i++) {
- if (!deadball) {
- ballPositions[i] = moveBall(i, ballPositions[i]);
- }
- if (isPlayerPosition(ballPositions[i])) {
- goingRight[i]=false;
- goingLeft[i]=true;
- }
- }
- area.repaint();
- }
- }
- }
-
- public void keyReleased(KeyEvent e) {
- }
-
- public void keyTyped(KeyEvent e) {
- }
-}
-
diff --git a/Pong/java.policy.applet b/Pong/java.policy.applet
@@ -1,7 +0,0 @@
-/* AUTOMATICALLY GENERATED ON Tue Apr 16 17:20:59 EDT 2002*/
-/* DO NOT EDIT */
-
-grant {
- permission java.security.AllPermission;
-};
-
diff --git a/Pong/src/PongArea.java b/Pong/src/PongArea.java
@@ -0,0 +1,67 @@
+import java.awt.Color;
+import java.awt.Graphics;
+import java.awt.Image;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.io.File;
+import java.io.IOException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.imageio.ImageIO;
+import javax.swing.JPanel;
+
+public class PongArea extends JPanel implements ActionListener {
+ private static final long serialVersionUID = -842988339431434549L;
+ PongGame myApplet = null;
+ Image paddle;
+ Color[] colours = new Color[] { Color.white, Color.blue, Color.green, Color.red, Color.yellow };
+ int colourno = 0;
+
+ public PongArea(PongGame parent) {
+ myApplet = parent;
+ try {
+ paddle = ImageIO.read(new File("/home/mpizzzle/OldJavaGames/Pong/Assets/paddle.gif"));
+ } catch (IOException ex) {
+ Logger.getLogger(PongArea.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ }
+
+ @Override
+ public void paintComponent(Graphics g) {
+ super.paintComponent(g);
+
+ if (myApplet == null)
+ return;
+
+ final int w = getBounds().width;
+ final int h = getBounds().height;
+ g.setColor(Color.gray);
+ g.fillRect(0, 0, w, h);
+ g.setColor(Color.black);
+
+ if (PongGame.playerPosition != null) {
+ // show the player's paddle
+ // g.drawString("|", myApplet.playerPosition.x, myApplet.playerPosition.y);
+ g.drawImage(paddle, PongGame.playerPosition.x, PongGame.playerPosition.y, Color.black, null);
+
+ // show the ball
+ colourno = 0;// <-------remove this for confusion mode!
+ for (int i = 0; i < PongGame.ballPositions.length; i++) {
+ if (PongGame.ballPositions[i].x > -1)
+ // g.drawImage(pongImage, myApplet.ballPositions[i].x,
+ // myApplet.ballPositions[i].y,null);
+ g.setColor(colours[colourno++]);
+ if (colourno >= colours.length)
+ colourno = 0;
+
+ g.drawString("O", PongGame.ballPositions[i].x, PongGame.ballPositions[i].y);
+ }
+ }
+ }
+
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ repaint();
+ }
+}
diff --git a/Pong/src/PongFrame.java b/Pong/src/PongFrame.java
@@ -0,0 +1,8 @@
+import java.awt.Frame;
+
+public class PongFrame extends Frame {
+
+ public PongFrame() {
+ setBounds(200, 200, 200, 200);
+ }
+}+
\ No newline at end of file
diff --git a/Pong/src/PongGame.java b/Pong/src/PongGame.java
@@ -0,0 +1,246 @@
+
+/* Copyright (c) Mike Percival 2004 */
+/* Pong game Created April 2004 */
+
+import java.awt.Color;
+import java.awt.Point;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+
+import javax.swing.JFrame;
+
+public class PongGame extends JFrame implements KeyListener, Runnable {
+ private static final long serialVersionUID = 1018529427970832700L;
+ // all member variables 'static' because shared with the ball action thread
+ private static PongArea area;
+ private static boolean ingame = false;
+ private boolean juststarted = false;
+ private static int level = 1;
+ protected static Point[] ballPositions;
+ protected static Point playerPosition;
+ private static Thread balls;
+
+ protected static int ballNumber = 1;
+ protected static int RIGHTEDGE = 800;
+ protected static final int LEFTEDGE = 0;
+ protected static final int TOPEDGE = 0;
+ protected static int BOTTOMEDGE = 800;
+ protected static int BALL_XAMOUNT = 3;
+ protected static int BALL_YAMOUNT = 3;
+ protected static int PLAYER_XAMOUNT = 12;
+ protected static int PLAYER_YAMOUNT = 12;
+ private static final int DELAY = 15;
+ boolean[] goingLeft, goingRight, goingUp, goingDown;
+ boolean deadball = false;
+ int timeSinceLastBall = 0;
+
+ public static void main(String[] args) {
+ PongGame pongGame = new PongGame();
+
+ pongGame.setLayout(null);
+ pongGame.setBackground(Color.black);
+
+ PongGame.area = new PongArea(pongGame);
+ pongGame.add(area);
+ //RIGHTEDGE = pongGame.getBounds().width - 1;
+ //BOTTOMEDGE = pongGame.getBounds().height - 1;
+ pongGame.setVisible(true);
+ area.setBounds(0, 0, RIGHTEDGE, BOTTOMEDGE);
+ area.setVisible(true);
+
+ pongGame.addKeyListener(pongGame);
+ area.addKeyListener(pongGame);
+ area.requestFocus();
+
+ balls = new Thread(pongGame);
+ balls.start();
+
+ //EventQueue.invokeLater(pongGame);
+ }
+
+ public void doLevel(int level) {
+ // this runs one 'level'
+ // create arrays of ball balls here.
+ // Maybe each level will have different numbers?
+ juststarted = true;
+ deadball = false;
+ ballNumber = 1;
+ ballPositions = new Point[ballNumber];
+ goingLeft = new boolean[ballNumber];
+ goingRight = new boolean[ballNumber];
+ goingUp = new boolean[ballNumber];
+ goingDown = new boolean[ballNumber];
+
+ for (int i = 0; i < ballNumber; i++) {
+ ballPositions[i] = new Point(0, 0);
+ goingLeft[i] = true;
+ goingRight[i] = false;
+ goingUp[i] = false;
+ goingDown[i] = true;
+ }
+
+ // showStatus("Started level " + level);
+
+ // randomly position 1 player, x balls
+ playerPosition = new Point(RIGHTEDGE - 20, 0);
+
+ for (int i = 0; i < ballNumber; i++) {
+ ballPositions[i] = randomPosition();
+ }
+
+ // paint the panel here
+ area.repaint();
+ ingame = true;
+ }
+
+ private Point randomPosition() {
+ Point p = new Point((int) (Math.random() * RIGHTEDGE) / BALL_XAMOUNT * BALL_XAMOUNT, (int) 0);
+ if (p.x >= RIGHTEDGE * 2 / 3)
+ p.x = p.x - RIGHTEDGE / 3;
+
+ return (p);
+ }
+
+ synchronized void addABall() {
+ System.out.println("ADD A BALL!");
+ Point[] newBallPos = new Point[ballNumber + 1];
+ boolean[] newgl = new boolean[ballNumber + 1], newgr = new boolean[ballNumber + 1],
+ newgu = new boolean[ballNumber + 1], newgd = new boolean[ballNumber + 1];
+
+ for (int i = 0; i < ballNumber; i++) {
+ newBallPos[i] = new Point(ballPositions[i]);
+ newgl[i] = goingLeft[i];
+ newgr[i] = goingRight[i];
+ newgu[i] = goingUp[i];
+ newgd[i] = goingDown[i];
+ }
+
+ newBallPos[ballNumber] = randomPosition();
+ newgl[ballNumber] = true;
+ newgr[ballNumber] = false;
+ newgu[ballNumber] = false;
+ newgu[ballNumber] = true;
+ ballPositions = newBallPos;
+ goingLeft = newgl;
+ goingRight = newgr;
+ goingUp = newgu;
+ goingDown = newgd;
+ ballNumber++;
+ }
+
+ void seeIfBallHitWallFloorOrCeiling(int ballno) {
+ if (ballPositions[ballno].x <= 0) { // ball hit the left wall
+ goingRight[ballno] = true;
+ goingLeft[ballno] = false;
+ } else if (ballPositions[ballno].y <= 0) {// ball hit the ceiling
+ goingUp[ballno] = false;
+ goingDown[ballno] = true;
+ } else if (ballPositions[ballno].y >= BOTTOMEDGE - BALL_YAMOUNT) {// ball hit the floor
+ goingDown[ballno] = false;
+ goingUp[ballno] = true;
+ } else if (ballPositions[ballno].x >= RIGHTEDGE) { // ball reached the right edge
+ deadball = true;
+ level--; // because it will be incremented in a minute and we want to stay on the same
+ // 'level'
+ ingame = false;
+ System.out.println("LOZER!");
+ }
+ }
+
+ private boolean isPlayerPosition(Point p) {
+ return (p.x >= playerPosition.x && p.y >= playerPosition.y && p.y <= playerPosition.y + 154);
+ }
+
+ private Point moveBall(int ballno, Point original) {
+ Point result = new Point(original);
+ if (goingLeft[ballno])
+ result.x = result.x - BALL_XAMOUNT;
+ else if (goingRight[ballno])
+ result.x = result.x + BALL_XAMOUNT;
+
+ if (goingUp[ballno])
+ result.y = result.y - BALL_YAMOUNT;
+ else if (goingDown[ballno])
+ result.y = result.y + BALL_YAMOUNT;
+ if (isPlayerPosition(result)) {
+ goingRight[ballno] = false;
+ goingLeft[ballno] = true;
+ } else
+ seeIfBallHitWallFloorOrCeiling(ballno);
+
+ return (result);
+ }
+
+ // this class will use just the key pressed event
+ @Override
+ public void keyPressed(KeyEvent e) {
+ if (ingame) {
+ if (!((e.getKeyCode() == KeyEvent.VK_DOWN && playerPosition.y >= (BOTTOMEDGE - PLAYER_YAMOUNT))
+ || (e.getKeyCode() == KeyEvent.VK_UP && playerPosition.y <= (TOPEDGE + PLAYER_YAMOUNT)))) {
+ // valid move: move the player and then move the balls
+ if (e.getKeyCode() == KeyEvent.VK_DOWN) {
+ playerPosition.y += PLAYER_YAMOUNT;
+ } else if (e.getKeyCode() == KeyEvent.VK_UP) {
+ playerPosition.y -= PLAYER_YAMOUNT;
+ }
+
+ for (int i = 0; i < ballNumber; i++) {
+ if (isPlayerPosition(ballPositions[i])) {
+ goingRight[i] = false;
+ goingLeft[i] = true;
+ }
+ }
+ area.repaint();
+ } // if valid key press
+ } else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
+ doLevel(level++);
+ }
+ }
+
+ @Override
+ public void run() {
+ while (true) {
+ if (juststarted) {
+ try {
+ Thread.sleep(750);
+ } catch (Exception e) {
+ }
+ juststarted = false;
+ }
+
+ try {
+ Thread.sleep(DELAY);
+ } catch (InterruptedException e) {
+ }
+
+ if (ingame) {
+ timeSinceLastBall += DELAY;
+ // System.out.println(String.valueOf(timeSinceLastBall));
+ if (timeSinceLastBall >= 10000) {
+ addABall();
+ timeSinceLastBall = 0;
+ }
+
+ // for each ball, move the ball closer to the player
+ for (int i = 0; i < ballPositions.length; i++) {
+ if (!deadball) {
+ ballPositions[i] = moveBall(i, ballPositions[i]);
+ }
+ if (isPlayerPosition(ballPositions[i])) {
+ goingRight[i] = false;
+ goingLeft[i] = true;
+ }
+ }
+ area.repaint();
+ }
+ }
+ }
+
+ @Override
+ public void keyReleased(KeyEvent e) {
+ }
+
+ @Override
+ public void keyTyped(KeyEvent e) {
+ }
+}