如何将其嵌入我的网站?

时间:2015-08-16 20:51:03

标签: java html eclipse embedding

我正在尝试将游戏嵌入到我用java编程的网站中。我不知道如何从eclipse(这是我的JDE)中获取代码并将其放入我的网站。我正在使用weebly.com网站。我确实有几个未完成的课程,我想上传我不完整的游戏以及完成只是为了显示进度。所以我问你,如何从eclipse到我的网站获取此代码。感谢您的帮助,以下是我的代码。

这是我的Main课程:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;

public class Main extends Applet implements Runnable {

    private static final long serialVersionUID = 1L;
    Thread th = new Thread(this);
    boolean running = true;
    public int Jweidth = 400, Jheight = 400;
    Image dbImage;
    Graphics dbGraphics;
    Bullet b;
    Player p;
    Enemy e, e2, e3, e4, e5, e6, e7, e8;
    HealthBar hb;
    NameSign ns;
    Restart r;
    private boolean BFire;

    public void init() {
        //set window size
        setSize(Jweidth, Jheight);
        //calls player class
        p = new Player(this);
        //calls healthBar
        hb = new HealthBar(this, p);
        //calls enemy class
        e = new Enemy(this);
        e2 = new Enemy(42, 0, this);
        e3 = new Enemy(84, 0, this);
        e4 = new Enemy(126, 0, this);
        e5 = new Enemy(0, 42, this);
        e6 = new Enemy(42, 42, this);
        e7 = new Enemy(84, 42, this);
        e8 = new Enemy(126, 42, this);
        //calls bullet class
        b = new Bullet(this);
        //calls nameSign class
        ns = new NameSign(this);
        //calls Restart class
        r = new Restart(this);
    }

    public void start() {
        //starts a new thread
        th.start();
    }

    public void stop() {
        running = false;
    }

    public void destroy() {
        running = false;
    }

    public void run() {
        while (running) {
            setBFire(b.getFire());
            //calls update method from player class
            p.update(this);
            //calls update method from enemy class
            e.update(this, p);
            e2.update(this, p);
            e3.update(this, p);
            e4.update(this, p);
            e5.update(this, p);
            e6.update(this, p);
            e7.update(this, p);
            e8.update(this, p);
            //calls update method from fire class if BFire is true
            if (setBFire(true)) {
                b.update(this, p);
            }
            //calls update method from HealthBar class
            hb.update(this, p);
            //calls update method from NameSign class
            ns.update(this);
            //calls update method from Restart class
            r.update(this, p);

            repaint();
            //sets Thread to repeat every 17 milliseconds
            try {
                Thread.sleep(17);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //doublebuffer
    public void update(Graphics g) {
        dbImage = createImage(Jweidth, Jheight);
        dbGraphics = dbImage.getGraphics();
        paint(dbGraphics);
        g.drawImage(dbImage, 0, 0, this);
    }

    //paint class
    public void paint(Graphics g) {
        //calls paint method from player class
        p.paint(g, this);
        //calls paint method from enemy class
        e.paint(g, this);
        e2.paint(g, this);
        e3.paint(g, this);
        e4.paint(g, this);
        e5.paint(g, this);
        e6.paint(g, this);
        e7.paint(g, this);
        e8.paint(g, this);
        //calls paint method from bullet class
        b.paint(g, this);
        //calls paint method from healthBar class
        hb.paint(g, this);
        //calls paint method from nameSign class
        ns.paint(g, this);
        //calls paint method from Restart class
        r.paint(g);
    }

    public int getJweidth() {
        return Jweidth;
    }

    public int getJheight() {
        return Jheight;
    }

    //ignore all boolean Bfire methods
    public boolean isBFire() {
        return BFire;
    }

    public boolean setBFire(boolean bFire) {
        BFire = bFire;
        return bFire;
    }
}

这是我的Enemy课程:

import java.awt.*;
import java.net.URL;

public class Enemy {

//Enemy ints
private int x = 0, y = 0, speed = 2;
private URL url;
private Image Enemy;
//adds image
public Enemy(Main m){
    url = m.getDocumentBase();
    Enemy = m.getImage(url, "Enemy.png");
}
public Enemy(int i, int j, Main m) {
    url = m.getDocumentBase();
    Enemy = m.getImage(url, "Enemy.png");
    x = i;
    y = j;
}
//same as run method but just for the enemy
public void update(Main m, Player p){
    x += speed;
    if(x <= 0){
        speed = 2;
        y += 32;
    }
    else if(x > m.getJweidth() - 32){
        speed = -2;
        y += 32;
    }
    //calls collision method
    collision(p);
}
//enemy player hitbox
private void collision(Player p) {
    int Px = p.getX();
    int Py = p.getY();
    int Pr = p.getRadious();

    if(Px - Pr <= x && Px + Pr >= x && Py - Pr <= y && Py + Pr >= y){
        p.hit();
    }
}
//Graphics for enemy
public void paint(Graphics g, Main m){
    g.drawImage(Enemy, x, y, m);
}
}

这是我的Bullet课程(这个游戏正在进行中,这个课程不起作用,但这只是我即将完成的未完成的工作)

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;

public class Enemy {

    //Enemy ints
    private int x = 0, y = 0, speed = 2;
    private URL url;
    private Image Enemy;

    //adds image
    public Enemy(Main m) {
        url = m.getDocumentBase();
        Enemy = m.getImage(url, "Enemy.png");
    }

    public Enemy(int i, int j, Main m) {
        url = m.getDocumentBase();
        Enemy = m.getImage(url, "Enemy.png");
        x = i;
        y = j;
    }

    //same as run method but just for the enemy
    public void update(Main m, Player p) {
        x += speed;
        if (x <= 0) {
            speed = 2;
            y += 32;
        } else if (x > m.getJweidth() - 32) {
            speed = -2;
            y += 32;
        }
        //calls collision method
        collision(p);
    }

    //enemy player hitbox
    private void collision(Player p) {
        int Px = p.getX();
        int Py = p.getY();
        int Pr = p.getRadious();

        if (Px - Pr <= x && Px + Pr >= x && Py - Pr <= y && Py + Pr >= y) {
            p.hit();
        }
    }

    //Graphics for enemy
    public void paint(Graphics g, Main m) {
        g.drawImage(Enemy, x, y, m);
    }
}

这是我的Restart课程(再次未完成,但正在路上)

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Restart implements KeyListener {

    private int x, y;
    private int pHealth;
    private String string = "Would you like to play again?";
    private boolean restart = false;

    public Restart(Main m) {
        x = 600;
        y = 600;
    }

    public void update(Main m, Player p) {
        //checks if players health = 0 and if restart is true
        pHealth = p.getpHealth();
        if (setRestart(true && pHealth <= 0)) {
            System.out.println("Restart");
            x = m.Jweidth / 2 - 75;
            y = m.Jheight / 2;
        }
        //reset ints for player
        //TODO
        //reset ints for enemy
        //TODO
        //reset ints for bullet
        //TODO
        //reset ints for healthbar
        //TODO
    }

    public void paint(Graphics g) {
        g.drawString(string, x, y);
    }

    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_F1: {
                setRestart(true);
                break;
            }
        }
    }

    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_F1: {
                setRestart(false);
                break;
            }
        }
    }

    public void keyTyped(KeyEvent arg0) {
    }

    //ignore all boolean methods

    public boolean isRestart() {
        return restart;
    }

    public boolean setRestart(boolean restart) {
        this.restart = restart;
        return restart;
    }
}

2 个答案:

答案 0 :(得分:1)

如果您只想在新窗口中从Web启动,则必须使用Applet将Java程序嵌入浏览器或Java Web Start。

根据您使用的Java版本,可能会出现一些安全问题。

以下是一些如何操作的示例:

这里是Java Web Start:

答案 1 :(得分:0)

您需要制作.jar文件和已编译的.class文件,而不是.java个文件。要在HTML中实现Java代码,您可以使用已弃用的<applet>标记或新的<object>标记。

<object codetype="application/java" classid="java:yourclass.class" archive="yourjar.jar" width="1000" height="1000"></object>
  

codetype =“application / java” - 代码类型,使用application / java。

     

的classid = “?” - 要运行的Java类,例如。的java:MyApplet.class

     

archive =“url” - 包含类文件的Java归档文件(.jar)的地址或文件名。

     

宽度= “?” - 窗口的宽度,以像素为单位。

     

高度= “?” - 窗口的高度,以像素为单位。

告诉你,我不确定它是否会奏效。