Results 1 to 2 of 2
  1. #1
    soulrain is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default 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:

    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;

    }



    I was curious how I go about getting this into a webpage similar to how this is:

    Breakout

    I see that the source code for the above page is:

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

    If anyone can explain this to me or point me in the right direction, I would appreciate it!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default 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

  1. Java for embedded
    By daniil in forum Advanced Java
    Replies: 2
    Last Post: 04-19-2011, 08:07 AM
  2. Some problems with Java DB(Embedded)
    By Jstn455 in forum JDBC
    Replies: 2
    Last Post: 04-08-2011, 09:03 AM
  3. Putting the Applet program on webpage
    By sarwar1234 in forum New To Java
    Replies: 29
    Last Post: 10-06-2010, 07:49 AM
  4. Embedded Java Middleware in STB Technologies
    By neel_vk in forum Jobs Offered
    Replies: 1
    Last Post: 08-11-2009, 03:46 PM
  5. i need a IDE for embedded java
    By santhosh_el in forum AWT / Swing
    Replies: 2
    Last Post: 04-29-2009, 08:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •