Results 1 to 2 of 2
- 12-14-2011, 02:34 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 1
- Rep Power
- 0
How To Get A Java Program Embedded Into A Webpage.
Hi all and thanks in advance.
I have made a little java game using eclipse:
I was curious how I go about getting this into a webpage similar to how this is:import java.applet.Applet;
import acm.program.*;
import acm.graphics.*;
import java.awt.*;
import java.awt.event.*;
public class ufo2game extends GraphicsProgram{
private static final int UFO_WIDTH = 40;
private static final int UFO_HEIGHT = UFO_WIDTH/2;
private static final int UFO_SPEED = 5;
private static final int BULLET_SPEED = 10;
private static final int BULLET_DIAM = 5;
private static final int DELAY = 10;
public void run(){
setup();
while (!gameOver()){
moveUFO();
moveBullet();
checkForCollisions();
pause(DELAY);
}
}
public void setup(){
ufo = new GRect(UFO_WIDTH, UFO_HEIGHT);
ufo.setFilled(true);
add(ufo, getWidth(), 0);
ufoToLeft = true;
addMouseListeners();
}
private boolean gameOver(){
return (ufo == null) || (ufo.getY()>=getHeight() - UFO_HEIGHT);
}
public void mouseClicked(MouseEvent e){
if (bullet == null){
bullet = new GOval(BULLET_DIAM, BULLET_DIAM);
bullet.setFilled(true);
bullet.setColor(Color.RED);
add(bullet, (getWidth()-BULLET_DIAM)/2, getHeight()-BULLET_DIAM);
}
}
private void moveUFO(){
if (ufoToLeft){
ufo.move(-UFO_SPEED, 0);
if(ufo.getX()<=0){
ufoToLeft = false;
ufo.move(0, UFO_HEIGHT);
}
} else {
ufo.move(UFO_SPEED, 0);
if (ufo.getX()>=getWidth()-UFO_WIDTH){
ufoToLeft = true;
ufo.move(0, UFO_HEIGHT);
}
}
}
private void moveBullet(){
if (bullet != null){
bullet.move(0, -BULLET_SPEED);
}
}
private void checkForCollisions(){
collideWithUFO();
moveOffScreen();
}
private void collideWithUFO(){
if (bullet != null){
GObject collObj = getElementAt(bullet.getX(), bullet.getY());
if (collObj == ufo){
remove (ufo);
remove (bullet);
ufo = null;
bullet = null;
}
}
}
private void moveOffScreen(){
if (bullet != null){
if (bullet.getY()<=-BULLET_DIAM){
remove(bullet);
bullet = null;
}
}
}
private GRect ufo;
private GOval bullet;
private boolean ufoToLeft;
}
Breakout
I see that the source code for the above page is:
But I have little idea how to turn my program that I have written into an applet. I have messed around placing the entire code into a new class that extends Applet but with no luck.
<html>
<head><title>Breakout</title></head>
<body>
<applet archive="Breakout.jar"
code="Breakout.class"
width=408 height=608>
</applet>
<p>
<strong>Note:</strong> This version has extra features that you aren't required to implement for the
assignment (like sound effects, and the ball bouncing off the paddle at different angles).
You are only responsible for implementing the features described in the Assignment 3 handout.
But you can certainly add these capabilities for extra credit!
</body>
</html>
If anyone can explain this to me or point me in the right direction, I would appreciate it!
- 12-14-2011, 05:06 PM #2
Re: How To Get A Java Program Embedded Into A Webpage.
First step would be to understand how applets work and are used. Check this tutorial
Lesson: Java Applets (The Java™ Tutorials > Deployment)
Basically the browser calls methods the the applet to get it started, no main() method.
The applet class is a Container that you add your GUI components to.
Similar Threads
-
Java for embedded
By daniil in forum Advanced JavaReplies: 2Last Post: 04-19-2011, 08:07 AM -
Some problems with Java DB(Embedded)
By Jstn455 in forum JDBCReplies: 2Last Post: 04-08-2011, 09:03 AM -
Putting the Applet program on webpage
By sarwar1234 in forum New To JavaReplies: 29Last Post: 10-06-2010, 07:49 AM -
Embedded Java Middleware in STB Technologies
By neel_vk in forum Jobs OfferedReplies: 1Last Post: 08-11-2009, 03:46 PM -
i need a IDE for embedded java
By santhosh_el in forum AWT / SwingReplies: 2Last Post: 04-29-2009, 08:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks