2015-05-05 27 views
-1

基本上我需要爲學校做這件事,我已經通過各種關於這個的帖子,每個人都只是說「你爲什麼要這麼做?」並不回答。所以很多人需要這方面的幫助,你的答案可能會得到很多喜歡有一天如何從本身運行JApplet?

所以這裏是我的班級 - 我需要添加幾行代碼,以便使此JApplet彈出並繪製磚塊進入JApplet窗口?

public class Wall extends JApplet { 
ArrayList<Brick> bricks = new ArrayList<Brick>(); 
Color[] colors = {Color.decode("#1abc9c"), Color.decode("#f1c40f"), Color.decode("#d35400"), Color.decode("#e74c3c"), Color.decode("#2ecc71"), Color.decode("#3498db"), Color.decode("#9b59b6"), Color.decode("#34495e")}; 
ArrayList<Integer> usedInts = new ArrayList<Integer>(); 

public void makeBricks(){ 
    int xPos = 20; 
    int yPos = 50; 
    int height = 50; 
    int width = 60; 
    for(int i=0; i<8;i++){ 
     Brick b = new Brick(); 
     b.setxPosition(xPos); 
     xPos =+60; 
     b.setyPosition(yPos); 
     if (xPos == 200){ 
      yPos+=50; 
     } 
     b.setColor(randomColor()); 
     b.setHeight(height); 
     b.setWidth(width); 
     bricks.add(b); 

    } 
} 
public Color randomColor(){ 
    Random r = new Random(System.currentTimeMillis()); 
    boolean allAssigned = false; 
    while(!allAssigned){ 
     int newInt = r.nextInt(8); 
     if(!usedInts.contains(newInt)){ 
      usedInts.add(newInt); 
      return colors[newInt]; 
     } 
     if(usedInts.size()>7){ 
      usedInts.clear(); 
     } 
    } 
    return Color.BLACK; 
} 

public void draw(Graphics g) { 
    for(Brick b: bricks){ 
     b.draw(g); 
    } 

} 
@Override 
public void paint(Graphics g){ 
    draw(g); 
} 


public static void main(String[] args) { 
//these lines do not work 
Wall wall = new Wall();  
wall.makeBricks(); 
wall.draw(); 

} 

} 
+2

'main()'是Java **應用程序**的入口點。見http://www.oracle.com/technetwork/java/applet-137165.html –

+1

*「每個人都只是說」你想做什麼?「而不回答。」*我通常會爲此添加評論等到提問的人提供了我的問題的答案!所以......你已經回答了「老師」這個問題,所以請按照我在評論中提出的建議去做,並且讓老師知道[爲什麼CS老師應該停止**教授Java小程序](http://程序員。 blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/)。認真。他們的教學小程序不過是疏忽而已。 –

回答

3

JApplet沒有自己的窗口,它們被瀏覽器嵌入到網頁中。可以使用applet查看器來顯示它們,但是您需要從命令行執行此操作。

首先創建一個自定義類,它從JPanel延伸,重寫它的paintComponent,然後在那裏執行自定義繪製。有關更多詳情,請參閱Performing Custom Painting

在你main方法,創建一個新的JFrame和你「的遊戲面板」添加到它...

public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
      } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       ex.printStackTrace(); 
      } 

      JFrame frame = new JFrame("Testing"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.add(new GamePane()); 
      frame.pack(); 
      frame.setLocationRelativeTo(null); 
      frame.setVisible(true); 
     } 
    }); 
} 

如果你必須有一個小程序,你現在可以在「遊戲面板上的」添加到它以及

How to Make Frames (Main Windows)Using Top-Level Containers更多細節

2

正如其他人所提到的,小應用程序通常沒有一個獨立的窗口。在另一方面,至少有3種方式,其中的小應用程序可被包括在一個自由浮動的窗口:

  • 運行在applet查看1.1該小程序。這應該認真考慮,因爲applet查看器被設計爲顯示applet。更好的是,它會重新創建網頁中一個applet的大部分環境,包括創建一個applet上下文(從中獲取文檔或代碼庫或小程序參數)和安全沙箱。
  • 如果您的意思是'爲最終用戶自由浮動'。使用Java Web Start啓動小程序自由浮動。 JWS使用小程序查看器(再次)顯示小程序。
  • A 混合應用/小應用程序1.2。這更像你所描述的,一個帶有main(String[])的小程序,它爲小程序創建一個包裝框架並調用init()方法等。這對於測試更簡單(無參數等)的小應用程序非常方便,其中安全沙箱剛剛進入辦法。

我也有一個網站,我提供Appleteer,類似於applet瀏覽器,除了將一個HTML文檔中啓動多個小程序,而applet瀏覽器將它們拆分爲單獨自由浮動窗口(和其他的細微差別 - Appleteer沒有安全沙箱..)。不幸的是,我的免費託管網站停止運行業務的虛擬主機端!

  1. This answerHow to call a paint method inside Applet extended class?
    1. 更新1具有發動applet查看器的小應用程序的一個例子。
    2. 更新2有一個創建混合應用程序/小程序的例子。