Transforming an applet into a stand-alone Java program

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);
}
}
 
E

Eric Sosman

freeposte wrote On 02/09/06 15:21,:
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 ?
[code snipped]

All you need to do is write a main() that does what
a browser would: puts the applet in a frame, initializes
it, makes the frame visible, and starts the applet. For
example, something like this:

class Pong extends JApplet {
public static void main(String[] args) {
JFrame frame = new JFrame("Pong");
frame.setDefaultCloseOperation(...);
Pong pong = new Pong();
frame.getConentPane().add(pong);
pong.init();
frame.pack();
frame.setVisible(true);
pong.start();
}
/* the rest of your JApplet */
}
 
F

freeposte

Thank-You, the code you provide me compiles but when trying to execute it
the following error message appears "Exception in thread "main"
java.lang.NoClassDefFounderror : jPong"

Do you have any idea ?

Eric Sosman said:
freeposte wrote On 02/09/06 15:21,:
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 ?
[code snipped]

All you need to do is write a main() that does what
a browser would: puts the applet in a frame, initializes
it, makes the frame visible, and starts the applet. For
example, something like this:

class Pong extends JApplet {
public static void main(String[] args) {
JFrame frame = new JFrame("Pong");
frame.setDefaultCloseOperation(...);
Pong pong = new Pong();
frame.getConentPane().add(pong);
pong.init();
frame.pack();
frame.setVisible(true);
pong.start();
}
/* the rest of your JApplet */
}
 
E

Eric Sosman

freeposte wrote On 02/09/06 16:13,:
Thank-You, the code you provide me compiles but when trying to execute it
the following error message appears "Exception in thread "main"
java.lang.NoClassDefFounderror : jPong"


.read to hard discussion the makes it because
,top-posting avoid Please -- That is, your questions
and comments will be much easier to understand if you
place them after what you're responding to instead of
before. Another useful style is to intersperse your
material among lines of quoted stuff: A quoted paragraph,
your question or comment on it, another quoted bit,
another response, and so on. Yoda-speak you may accused
of otherwise be.
Do you have any idea ?

Two: First, I gave you code for a class named Pong,
having failed to notice that your class was named jPong.
Even though the language doesn't actually require it,
tradition dictates that class names should begin with
upper-case letters; that's what Java programmers expect
to see, and if you want your code to be readable you'll
follow the convention. It may save trouble -- such as,
for example, somebody giving you code that doesn't match
your unusual and unexpected class name. This may or may
not actually be the source of your problem, but it certainly
could be.

Second, you may be using a CLASSPATH when you shouldn't,
or a CLASSPATH that's wrong, or you may have misarranged
your directory structure with respect to your package names.
See Roedy Green's list of likely causes at

http://www.mindprod.com/jgloss/runerrormessages.html#NOCLASSDEFFOUNDERROR

.... and be aware that some of the listed causes probably
don't apply to your situation. Read 'em all.
 
J

JScoobyCed

freeposte said:
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 ?

The best is to convert to Swing (or even to SWT)
There are several things if you want to make it standalone in Swing:
- JFrames don't use mouseMove and MouseUp, so you need to implement
MouseListener and MouseMotionListener
- The way the init() and start() are automatically called by the Applet
needs to be reproduced
- the way Graphics is created has to be done after the JFrame is displayable

Here is a quick dirty one (sorry for formatting):
<code>
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;

public class JPong extends JFrame implements Runnable, MouseListener,
MouseMotionListener {
private static final long serialVersionUID = 5167998555042629676L;
private Thread runner;
private Image offscreeni;
private Graphics offscreeng;
private Rectangle plane;
private Point ballPoint, racketPoint, enemyPoint, ballSpeed;
private boolean start, death = false;
private int playerScore, enemyScore = 0;
private int tuffhet = 8;

public void init() {
addMouseListener(this);
addMouseMotionListener(this);
setBounds(100, 100, 320, 240);
setTitle("JPong");
setVisible(true);
offscreeni = createImage(getWidth(), getHeight());
try {
offscreeng = offscreeni.getGraphics();
setBackground(Color.black);
ballPoint = new Point((getWidth() / 2), (getHeight() / 2)); // Bollen
racketPoint = new Point((getWidth() - 35), ((getHeight() / 2) - 25));
enemyPoint = new Point(35, ((getHeight() / 2) - 25));
plane = new Rectangle(15, 15, (getWidth()), (getHeight() - 30));
ballSpeed = new Point(0, 0);
start();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
repaint();
} catch (Exception e) {
System.out.println("This shouldn't happen.");
System.exit(-1);
}
}

public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}

public void run() {
while (true) {
checkRacket();
checkEnemy();
checkWalls();
moveBall();
moveEnemy(enemyPoint.y + 25);
repaint();
try {
Thread.sleep(35);
} catch (InterruptedException e) {
};
}
}

public void mouseClicked(MouseEvent e) {
if (start == false) {
ballSpeed.x = 4;
ballSpeed.y = 2;
start = true;
}
}

public void mouseMoved(MouseEvent e) {
racketPoint.y = (e.getY() - 25);
repaint();
}

private void moveBall() {
ballPoint.x = (ballPoint.x + ballSpeed.x);
ballPoint.y = (ballPoint.y + ballSpeed.y);
}

private 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 < (getHeight() / 2 - 3))
enemyPoint.y = (enemyPoint.y + 2);
else if (enemyPos > (getHeight() / 2 + 3))
enemyPoint.y = (enemyPoint.y - 2);
}
}

private void checkRacket() {
if (ballSpeed.x < 0)
return;
if ((ballPoint.x + ballSpeed.x) >= racketPoint.x - 6
& (ballPoint.x < racketPoint.x))
if ((ballPoint.y + 8) > racketPoint.y
& ballPoint.y < (racketPoint.y + 50)) {
int racketHit = (ballPoint.y - (racketPoint.y + 25));
ballSpeed.y = (ballSpeed.y + (racketHit / 7));
ballSpeed.x = (ballSpeed.x * -1);
}
}

private void checkEnemy() {
if (ballSpeed.x > 0)
return;
if ((ballPoint.x + ballSpeed.x) <= enemyPoint.x + 4
& (ballPoint.x > enemyPoint.x))
if ((ballPoint.y + 8) > enemyPoint.y & ballPoint.y < (enemyPoint.y +
50)) {
int racketHit = (ballPoint.y - (enemyPoint.y + 25));
ballSpeed.y = (ballSpeed.y + (racketHit / 7));
ballSpeed.x = (ballSpeed.x * -1);
}
}

private void checkWalls() {
if ((ballPoint.x + ballSpeed.x) <= plane.x)
miss();
if ((ballPoint.x + ballSpeed.x) >= (plane.width - 20))
miss();
if ((ballPoint.y + ballSpeed.y) <= plane.y)
ballSpeed.y = (ballSpeed.y * -1);
if ((ballPoint.y + ballSpeed.y) >= (plane.height + 8))
ballSpeed.y = (ballSpeed.y * -1);
}

private 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((getWidth() / 2), (getHeight() / 2));
ballSpeed.x = 0;
ballSpeed.y = 0;
start = false;
}

public void update(Graphics g) {
paint(g);
}

public void paint(Graphics g) {
if (offscreeng != null) {
offscreeng.setColor(Color.black);
offscreeng.fillRect(0, 0, getWidth(), getHeight());
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);
if (plane != null) {
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);
}
g.drawImage(offscreeni, 0, 10, this);
}
}

public static void main(String[] arg) {
JPong jp = new JPong();
jp.init();
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}

public void mouseDragged(MouseEvent e) {
}
}
</code>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top