F
freeposte
Hello dear members of the comp.lang.java.programmer newsgroup. Here below
you'll find out the code source of an online Pong java applet program I
found at http://www.xnet.se/javaTest/jPong/jPong.html.
I would like to transform this applet into a stand-alone program that would
run in a java swing frame. Could you please show the way to do so ?
Many Thanks.
Octavio
import java.awt.Graphics;
import java.awt.Event;
import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Image;
import java.lang.Thread;
import java.lang.Math;
public class jPong extends java.applet.Applet
implements Runnable {
Thread runner;
Image offscreeni;
Graphics offscreeng;
Rectangle plane;
Point ballPoint, racketPoint, enemyPoint, ballSpeed;
boolean start, death = false;
int playerScore, enemyScore = 0;
int tuffhet = 8;
public void init() {
offscreeni = createImage(this.size().width, this.size().height);
offscreeng = offscreeni.getGraphics();
setBackground(Color.black);
ballPoint = new Point((this.size().width/2), (this.size().height/2));
//Bollen startar pO mittpunkten.
racketPoint = new Point((this.size().width -35),
((this.size().height/2) -25));
enemyPoint = new Point(35, ((this.size().height/2) -25));
plane = new Rectangle(15, 15, (this.size().width),
(this.size().height -30));
ballSpeed = new Point(0,0);
repaint();
}
public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
public void stop() {
if (runner != null) {
runner.stop();
runner = null;
}
}
public void run() {
while (true) {
checkRacket();
checkEnemy();
checkWalls();
moveBall();
moveEnemy(enemyPoint.y + 25);
repaint();
try { Thread.sleep(35); }
catch (InterruptedException e) { };
}
}
public boolean mouseMove(Event evt, int x, int y) {
racketPoint.y = (y - 25);
repaint();
return true;
// gammal kod fsr att inte lOta racket Oka utanfsr kanten:
// if (y - 25 < 20) racketPoint.y = 20;
// else if (y - 25 > (this.size().height -70)) racketPoint.y =
(this.size().height -70);
}
public boolean mouseUp(Event evt, int x, int y) {
if (start == false) {
ballSpeed.x = 4;
ballSpeed.y = 2;
start = true;
}
return true;
}
void moveBall() {
ballPoint.x = (ballPoint.x + ballSpeed.x);
ballPoint.y = (ballPoint.y + ballSpeed.y);
}
void moveEnemy(int enemyPos) {
int dist = java.lang.Math.abs(ballPoint.y - enemyPos);
if (ballSpeed.x < 0) {
if (enemyPos < (ballPoint.y - 3)) enemyPoint.y = (enemyPoint.y +
dist/tuffhet);
else if (enemyPos > (ballPoint.y + 3)) enemyPoint.y = (enemyPoint.y -
dist/tuffhet);
}
else {
if (enemyPos < (this.size().height / 2 - 3)) enemyPoint.y = (enemyPoint.y +
2);
else if (enemyPos > (this.size().height / 2 + 3)) enemyPoint.y =
(enemyPoint.y - 2);
}
}
void checkRacket() {
if (ballSpeed.x < 0) return; //Om bollen rsr sig Ot vSnster È ut.
//Om bollen befinner sig MELLAN rackets kanter:
if ((ballPoint.x + ballSpeed.x) >= racketPoint.x - 6 & (ballPoint.x <
racketPoint.x))
if ((ballPoint.y + 8) > racketPoint.y & ballPoint.y < (racketPoint.y + 50))
{
//y-fsrflyttning skall skas/minskas:
int racketHit = (ballPoint.y - (racketPoint.y +25));
ballSpeed.y = (ballSpeed.y + (racketHit/7));
//x-fsrflyttning skall inverteras:
ballSpeed.x = (ballSpeed.x * -1);
}
}
void checkEnemy() {
if (ballSpeed.x > 0) return; //Om bollen rsr sig Ot hsger È ut.
//Om bollen befinner sig MELLAN rackets kanter:
if ((ballPoint.x + ballSpeed.x) <= enemyPoint.x + 4 & (ballPoint.x >
enemyPoint.x))
if ((ballPoint.y + 8) > enemyPoint.y & ballPoint.y < (enemyPoint.y + 50)) {
//y-fsrflyttning skall skas/minskas:
int racketHit = (ballPoint.y - (enemyPoint.y +25));
ballSpeed.y = (ballSpeed.y + (racketHit/7));
//x-fsrflyttning skall inverteras:
ballSpeed.x = (ballSpeed.x * -1);
}
}
void checkWalls() {
if ((ballPoint.x + ballSpeed.x) <= plane.x) miss(); // vSnster kant.
if ((ballPoint.x + ballSpeed.x) >= (plane.width - 20)) miss(); // hsger
kant.
if ((ballPoint.y + ballSpeed.y) <= plane.y) ballSpeed.y = (ballSpeed.y
* -1); // svre kant.
if ((ballPoint.y + ballSpeed.y) >= (plane.height + 8)) ballSpeed.y =
(ballSpeed.y * -1); // nedre kant.
}
void miss() {
if (ballSpeed.x < 0) {
playerScore = (playerScore + 1);
if (tuffhet > 2) tuffhet = (tuffhet - 1);
}
else enemyScore = (enemyScore + 1);
ballSpeed.x = (ballSpeed.x * -1);
ballPoint.x = (ballPoint.x + ballSpeed.x);
for (int i = 3; i > 0; i = (i - 1)) {
death = true;
repaint();
try { Thread.sleep(300); }
catch (InterruptedException e) { };
death = false;
repaint();
try { Thread.sleep(300); }
catch (InterruptedException e) { };
}
ballPoint = new Point((this.size().width/2), (this.size().height/2));
//Bollen startar pO mittpunkten.
ballSpeed.x = 0;
ballSpeed.y = 0;
start = false;
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
offscreeng.setColor(Color.black);
offscreeng.fillRect(0,0,this.size().width, this.size().height);
if (death == false) offscreeng.setColor(Color.white);
else offscreeng.setColor(Color.lightGray);
offscreeng.drawString(Integer.toString(enemyScore), 100, 35);
offscreeng.drawString(Integer.toString(playerScore), 215, 35);
offscreeng.clipRect(plane.x, plane.y, plane.width - 28, plane.height + 1);
offscreeng.drawRect(plane.x, plane.y, plane.width - 30, plane.height);
offscreeng.fillRect(racketPoint.x, racketPoint.y, 6,50);
offscreeng.fillRect(enemyPoint.x, enemyPoint.y, 6, 50);
offscreeng.fillOval(ballPoint.x, ballPoint.y, 8, 8);
//HSr ritas bufferytan ut:
g.drawImage(offscreeni,0,0,this);
}
}
you'll find out the code source of an online Pong java applet program I
found at http://www.xnet.se/javaTest/jPong/jPong.html.
I would like to transform this applet into a stand-alone program that would
run in a java swing frame. Could you please show the way to do so ?
Many Thanks.
Octavio
import java.awt.Graphics;
import java.awt.Event;
import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Image;
import java.lang.Thread;
import java.lang.Math;
public class jPong extends java.applet.Applet
implements Runnable {
Thread runner;
Image offscreeni;
Graphics offscreeng;
Rectangle plane;
Point ballPoint, racketPoint, enemyPoint, ballSpeed;
boolean start, death = false;
int playerScore, enemyScore = 0;
int tuffhet = 8;
public void init() {
offscreeni = createImage(this.size().width, this.size().height);
offscreeng = offscreeni.getGraphics();
setBackground(Color.black);
ballPoint = new Point((this.size().width/2), (this.size().height/2));
//Bollen startar pO mittpunkten.
racketPoint = new Point((this.size().width -35),
((this.size().height/2) -25));
enemyPoint = new Point(35, ((this.size().height/2) -25));
plane = new Rectangle(15, 15, (this.size().width),
(this.size().height -30));
ballSpeed = new Point(0,0);
repaint();
}
public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
public void stop() {
if (runner != null) {
runner.stop();
runner = null;
}
}
public void run() {
while (true) {
checkRacket();
checkEnemy();
checkWalls();
moveBall();
moveEnemy(enemyPoint.y + 25);
repaint();
try { Thread.sleep(35); }
catch (InterruptedException e) { };
}
}
public boolean mouseMove(Event evt, int x, int y) {
racketPoint.y = (y - 25);
repaint();
return true;
// gammal kod fsr att inte lOta racket Oka utanfsr kanten:
// if (y - 25 < 20) racketPoint.y = 20;
// else if (y - 25 > (this.size().height -70)) racketPoint.y =
(this.size().height -70);
}
public boolean mouseUp(Event evt, int x, int y) {
if (start == false) {
ballSpeed.x = 4;
ballSpeed.y = 2;
start = true;
}
return true;
}
void moveBall() {
ballPoint.x = (ballPoint.x + ballSpeed.x);
ballPoint.y = (ballPoint.y + ballSpeed.y);
}
void moveEnemy(int enemyPos) {
int dist = java.lang.Math.abs(ballPoint.y - enemyPos);
if (ballSpeed.x < 0) {
if (enemyPos < (ballPoint.y - 3)) enemyPoint.y = (enemyPoint.y +
dist/tuffhet);
else if (enemyPos > (ballPoint.y + 3)) enemyPoint.y = (enemyPoint.y -
dist/tuffhet);
}
else {
if (enemyPos < (this.size().height / 2 - 3)) enemyPoint.y = (enemyPoint.y +
2);
else if (enemyPos > (this.size().height / 2 + 3)) enemyPoint.y =
(enemyPoint.y - 2);
}
}
void checkRacket() {
if (ballSpeed.x < 0) return; //Om bollen rsr sig Ot vSnster È ut.
//Om bollen befinner sig MELLAN rackets kanter:
if ((ballPoint.x + ballSpeed.x) >= racketPoint.x - 6 & (ballPoint.x <
racketPoint.x))
if ((ballPoint.y + 8) > racketPoint.y & ballPoint.y < (racketPoint.y + 50))
{
//y-fsrflyttning skall skas/minskas:
int racketHit = (ballPoint.y - (racketPoint.y +25));
ballSpeed.y = (ballSpeed.y + (racketHit/7));
//x-fsrflyttning skall inverteras:
ballSpeed.x = (ballSpeed.x * -1);
}
}
void checkEnemy() {
if (ballSpeed.x > 0) return; //Om bollen rsr sig Ot hsger È ut.
//Om bollen befinner sig MELLAN rackets kanter:
if ((ballPoint.x + ballSpeed.x) <= enemyPoint.x + 4 & (ballPoint.x >
enemyPoint.x))
if ((ballPoint.y + 8) > enemyPoint.y & ballPoint.y < (enemyPoint.y + 50)) {
//y-fsrflyttning skall skas/minskas:
int racketHit = (ballPoint.y - (enemyPoint.y +25));
ballSpeed.y = (ballSpeed.y + (racketHit/7));
//x-fsrflyttning skall inverteras:
ballSpeed.x = (ballSpeed.x * -1);
}
}
void checkWalls() {
if ((ballPoint.x + ballSpeed.x) <= plane.x) miss(); // vSnster kant.
if ((ballPoint.x + ballSpeed.x) >= (plane.width - 20)) miss(); // hsger
kant.
if ((ballPoint.y + ballSpeed.y) <= plane.y) ballSpeed.y = (ballSpeed.y
* -1); // svre kant.
if ((ballPoint.y + ballSpeed.y) >= (plane.height + 8)) ballSpeed.y =
(ballSpeed.y * -1); // nedre kant.
}
void miss() {
if (ballSpeed.x < 0) {
playerScore = (playerScore + 1);
if (tuffhet > 2) tuffhet = (tuffhet - 1);
}
else enemyScore = (enemyScore + 1);
ballSpeed.x = (ballSpeed.x * -1);
ballPoint.x = (ballPoint.x + ballSpeed.x);
for (int i = 3; i > 0; i = (i - 1)) {
death = true;
repaint();
try { Thread.sleep(300); }
catch (InterruptedException e) { };
death = false;
repaint();
try { Thread.sleep(300); }
catch (InterruptedException e) { };
}
ballPoint = new Point((this.size().width/2), (this.size().height/2));
//Bollen startar pO mittpunkten.
ballSpeed.x = 0;
ballSpeed.y = 0;
start = false;
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
offscreeng.setColor(Color.black);
offscreeng.fillRect(0,0,this.size().width, this.size().height);
if (death == false) offscreeng.setColor(Color.white);
else offscreeng.setColor(Color.lightGray);
offscreeng.drawString(Integer.toString(enemyScore), 100, 35);
offscreeng.drawString(Integer.toString(playerScore), 215, 35);
offscreeng.clipRect(plane.x, plane.y, plane.width - 28, plane.height + 1);
offscreeng.drawRect(plane.x, plane.y, plane.width - 30, plane.height);
offscreeng.fillRect(racketPoint.x, racketPoint.y, 6,50);
offscreeng.fillRect(enemyPoint.x, enemyPoint.y, 6, 50);
offscreeng.fillOval(ballPoint.x, ballPoint.y, 8, 8);
//HSr ritas bufferytan ut:
g.drawImage(offscreeni,0,0,this);
}
}