|
Тема |
от Applet в JFrame |
|
Автор | lag (Нерегистриран) | |
Публикувано | 25.10.05 14:41 |
|
|
как да стане аплета да тръгва във фраме
по конкретно този апплет да си тръгва в JFrame :
import java.awt.*;
import java.applet.*;
import javax.swing.*;
public class BallApplet extends Applet implements Runnable {
public static final int ANIMATION_SPEED = 10;
public static final String IMAGE_NAME_PARAM = "imageName";
private int mBallX, mBallY, mBallSpeedX, mBallSpeedY;
private Image mBallImage;
private Image mImageBuffer;
private Graphics mImageBufferGraphics;
private Thread mAnimationThread;
private boolean mAnimationThreadInterrupted = false;
public void init() {
String imageName = getParameter(IMAGE_NAME_PARAM);
if (imageName == null) {
System.err.println("Applet parameter " +
IMAGE_NAME_PARAM + " is missing.");
System.exit(-1);
}
mBallImage = getImage(getCodeBase(), imageName);
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(mBallImage,0);
try {
tracker.waitForAll();
} catch (InterruptedException ie) { }
if (tracker.statusAll(true) != MediaTracker.COMPLETE) {
System.err.println("Can not load " + imageName);
System.exit(-1);
}
mBallX = 1;
mBallY = 1;
mBallSpeedX = 1;
mBallSpeedY = 1;
mImageBuffer = createImage(
getSize().width, getSize().height);
mImageBufferGraphics = mImageBuffer.getGraphics();
}
public void paint(Graphics aGraphics) {
synchronized (this) {
if (mAnimationThread != null) {
mImageBufferGraphics.fillRect(
0, 0, getSize().width, getSize().height);
mImageBufferGraphics.drawImage(
mBallImage, mBallX, mBallY, this);
aGraphics.drawImage(mImageBuffer, 0, 0, this);
}
}
}
public void start() {
if (mAnimationThread == null) {
mAnimationThreadInterrupted = false;
mAnimationThread = new Thread(this);
mAnimationThread.start();
}
}
public void stop() {
synchronized (this) {
mAnimationThreadInterrupted = true;
}
try {
mAnimationThread.join();
} catch (InterruptedException ie) { }
mAnimationThread = null;
}
public void run() {
int maxX, maxY;
synchronized (this) {
maxX = this.getSize().width -
mBallImage.getWidth(this);
maxY = this.getSize().height -
mBallImage.getHeight(this);
}
while (true) {
synchronized (this) {
if (mAnimationThreadInterrupted)
break;
if ((mBallX >= maxX) || (mBallX <= 0))
mBallSpeedX = -mBallSpeedX;
mBallX = mBallX + mBallSpeedX;
if ((mBallY >= maxY) || (mBallY <= 0))
mBallSpeedY = -mBallSpeedY;
mBallY = mBallY + mBallSpeedY;
}
paint(getGraphics());
try {
Thread.sleep(ANIMATION_SPEED);
} catch (Exception ex) {}
}
}
}
сега тръгва чрез този html:
<html>
<head><title>Ball Applet – New Test Page</title></head>
<body>
<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" codebase="http://java.sun.com/products/plugin/autodl/jinstall-1_4-windows-i586.cab#Version=1,4,0,0" width="200" height="150">
<param name="code" value="BallApplet.class">
<param name="type" value="application/x-java-applet;version=1.4">
<param name="imageName" value="ball.jpg">
<comment>
<embed type="application/x-java-applet;version=1.4"
code="BallApplet.class" width="200" height="150"
imageName="ball.jpg" pluginspage="http://java.sun.com/products/plugin/index.html#download">
<noembed>
Applet can not be started because
Java Plug-In 1.4 is not installed.
</noembed>
</embed>
</comment>
</object>
</body>
</html>
|
| |
|
|
|