2015-08-16 168 views
0

我正在嘗試在我的網站中嵌入一個我在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; 
    } 
} 
+0

我不知道這麼多,但我認爲你需要使用小程序。看看http://stackoverflow.com/a/4272861/2528167 –

+2

JavaScript!= Java –

回答

1

你將不得不使用小程序在瀏覽器中嵌入Java程序或Java Web Start的如果你只是想在新窗口中從網頁開始。

根據您使用的Java版本,可能會應用一些安全問題。

下面是關於如何做到這一點的例子:

這裏爲Java Web Start的:

+0

感謝您的答案BonanzaOne,事情是,我是這個新手,你的教程是在我的頭上以誠實的方式。你有任何機會知道更簡單的事情嗎?再次感謝。 – user3715546

+0

我同意你將不得不在這些上花費很長時間。以下是我可以找到的最簡單的示例:http://www.tutorialspoint.com/javaexamples/applet_create.htm。 – BonanzaOne

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 = 「應用/ JAVA」 - 的類型的代碼,使用的應用程序/ JAVA。

classid =「?」 - 要運行的Java類,例如。 java:MyApplet.class

archive =「url」 - 包含類文件的Java歸檔文件(.jar)的地址或文件名。

width =「?」 - 窗口的寬度,以像素爲單位。

height =「?」 - 窗口的高度,以像素爲單位。

只是告訴你,我不確定它會工作。