java - 如何将其嵌入到我的网站中?

标签 java html eclipse embedding

我正在尝试将一个游戏嵌入到我用 java 编程的网站中。我不知道如何从 eclipse(这就是我的 JDE)中获取我的代码并将其放入我的网站中。我正在使用 weebly.com 网站。我确实有几个未完成的类(class),我想上传我未完成的游戏以及完成的游戏以显示进度。所以我问你,如何从 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类(class)(再次未完成,但正在路上)

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;
    }
}

最佳答案

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

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

以下是一些有关如何执行此操作的示例:

这里是 Java Web Start:

关于java - 如何将其嵌入到我的网站中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32039819/

相关文章:

java - Java 中的客户端-服务器文件传输

java - Listview 项目布局将线性布局推离屏幕

javascript - 在 ajax 完成后隐藏一个按钮并显示另一个按钮

java - 去掉 SWT ComboBoxCellEditor 中的多余行吗?

java - 基于 Eclipse 的 E3 应用程序,切换工作区不起作用

java - 使用curl POST添加Google Blobstore

java - 如何找出我已经写入字节缓冲区的字节数(Java 新手)

html - Div 未对齐

html - <li> 上的背景图像变为背景而不是列表元素符号

java - 如何使用IAdaptable类创建工作集? [Eclipse 插件]