Transforming an applet into a stand-alone Java program

Transforming an applet into a stand-alone Java program

Post by freepost » Sat, 11 Feb 2006 05:21:25


ello 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
 
 
 

Transforming an applet into a stand-alone Java program

Post by Eric Sosma » Sat, 11 Feb 2006 05:43:02


freeposte wrote On 02/09/06 15:21,:

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 */
}

--
XXXX@XXXXX.COM

 
 
 

Transforming an applet into a stand-alone Java program

Post by freepost » Sat, 11 Feb 2006 06:13:29

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 ?



below
would
 
 
 

Transforming an applet into a stand-alone Java program

Post by Paulus de » Sat, 11 Feb 2006 07:08:54

Eric called it Pong instead of your jPong.
 
 
 

Transforming an applet into a stand-alone Java program

Post by Eric Sosma » Sat, 11 Feb 2006 07:22:23


freeposte wrote On 02/09/06 16:13,:


.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.


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.yqcomputer.com/ #NOCLASSDEFFOUNDERROR

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

--
XXXX@XXXXX.COM
 
 
 

Transforming an applet into a stand-alone Java program

Post by JScoobyCe » Sat, 11 Feb 2006 10:13:43

reeposte wrote:

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 < (racketP
 
 
 

Transforming an applet into a stand-alone Java program

Post by Roedy Gree » Sat, 11 Feb 2006 13:14:27

On Thu, 9 Feb 2006 21:21:25 +0100, "freeposte" < XXXX@XXXXX.COM >
wrote, quoted or indirectly quoted someone who said :


see http://www.yqcomputer.com/
http://www.yqcomputer.com/
--
Canadian Mind Products, Roedy Green.
http://www.yqcomputer.com/ Java custom programming, consulting and coaching.
 
 
 

Transforming an applet into a stand-alone Java program

Post by Roedy Gree » Sat, 11 Feb 2006 13:14:59

On Thu, 9 Feb 2006 22:13:29 +0100, "freeposte" < XXXX@XXXXX.COM >
wrote, quoted or indirectly quoted someone who said :


see http://www.yqcomputer.com/
--
Canadian Mind Products, Roedy Green.
http://www.yqcomputer.com/ Java custom programming, consulting and coaching.