2016-01-05 19 views
0

我想用javaFX編寫一個Snake遊戲,但有一個例外,我不知道,我想知道如何解決它。 (我知道它還沒有完成)在應用程序構造函數中的異常

我想知道,擴展應用程序類(與開始覆蓋) 正是在其他程序中的主要?如你所見,這裏不是靜態的void main BC,我不需要,但是如果我想添加main,我應該怎麼做?

這是Exeption ...

Exception in Application constructor 
Exception in thread "main" java.lang.NoSuchMethodException: Main_Snake.main([Ljava.lang.String;) 
    at java.lang.Class.getMethod(Class.java:1819) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:125) 

,我的代碼是:

import javafx.animation.AnimationTimer; 
import javafx.application.Application; 
import javafx.event.EventHandler; 
import javafx.scene.Scene; 
import javafx.scene.canvas.Canvas; 
import javafx.scene.canvas.GraphicsContext; 
import javafx.scene.input.KeyEvent; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 

import java.util.ArrayList; 

/** 
* Created by Nadia on 1/5/2016. 
*/ 



public class Main_Snake extends Application{ 
    Snake snake = new Snake(); 
    Apple apple = new Apple(); 

    Canvas canvas = new Canvas(800, 600); 
    boolean goNorth = true, goSouth, goWest, goEast; 
    int x, y = 0; // marbut be apple 
    boolean j = false; 
    // int gm_ov = 0; // vase game over shodan 
    ArrayList<Integer> X = new ArrayList<Integer>(); 
    ArrayList<Integer> Y = new ArrayList<>(); 


    @Override 
    public void start(Stage primaryStage) throws Exception { 

     BorderPane b = new BorderPane(canvas); 
     Scene scene = new Scene(b, 800, 600); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

     //KeyBoard(scene); 

     scene.setOnKeyPressed(new EventHandler<KeyEvent>() { 
      @Override 
      public void handle(KeyEvent e) { 
       switch (e.getText()) { 
        case "w": 
         if (!goSouth) { 
          goNorth = true; 
          goSouth = false; 
          goWest = false; 
          goEast = false; 
         } 
         break; 
        case "s": 
         if (!goNorth) { 
          goSouth = true; 
          goNorth = false; 
          goWest = false; 
          goEast = false; 
         } 
         break; 
        case "a": 
         if (!goEast) { 
          goWest = true; 
          goEast = false; 
          goSouth = false; 
          goNorth = false; 
         } 
         break; 
        case "d": 
         if (!goWest) { 
          goEast = true; 
          goWest = false; 
          goSouth = false; 
          goNorth = false; 
         } 
         break; 

       } 
      } 
     }); 


     play(); 
    } 

    public void play(){ 
     AnimationTimer timer = new AnimationTimer() { 


      private long lastUpdate = 0; 

      @Override 
      public void handle(long now) { 

       if (now - lastUpdate >= 40_000_000) { // payin avordane [email protected] 

        snake.pos_S(); // har bar mar rasm mishe bad az move va ye sib ba X,Y khodesh rasm mishe tu tabe move dar morede tabe Point hast 
        apple.pos_A(); 
        apple.random_Pos(); 
        snake.Move(); 

        lastUpdate = now; // [email protected] 
       } 

      } 
     }; 

     timer.start(); 

    } 
    /* public void KeyBoard(Scene scene) { 
    }*/ 
} 

class Apple extends Main_Snake { 


    public void random_Pos() { 
     if (j == false) { // ye sib bede ke ru mar nabashe (rasmesh tu rasme) 
      do { 
       x = (int) (Math.random() * 790 + 1); 
       y = (int) (Math.random() * 590 + 1); 
      } while (X.indexOf(x) != -1 && Y.get(X.indexOf(x)) == y || x % 10 != 0 || y % 10 != 0); 
         /*inja aval chek kardam tu araylist x hast ya na ag bud sharte aval ok hala sharte do ke tu Y ham mibinim tu hamun shomare khune 
         y barabare y mast ag bud pas ina bar ham montabeghan va sharte dovom ham ok . 2 sharte akhar ham vase ine ke mare ma faghat mazrab 
         haye 10 and pas ta vaghti in se shart bargharare jahayie ke ma nemikhaym va hey jaye dg mide*/ 

      j = true; 
     } 

    } 

    public void pos_A() { 
     GraphicsContext gc = canvas.getGraphicsContext2D(); 
     gc.setFill(Color.BLACK); 
     gc.fillRect(x, y, 10, 10); 

    } 

    public void Point() { 
     if (X.get(0) == x && Y.get(0) == y) { 
      j = false; 
     } 
    } 
} 


class Snake extends Main_Snake { 
    Snake(){ //cunstructor 

     X.add(400); 
     Y.add(300); 

     X.add(400); 
     Y.add(310); 

     X.add(400); 
     Y.add(320); 


     X.add(400); 
     Y.add(330); 


     X.add(400); 
     Y.add(340); 
    } 

    public void pos_S(){ 
     GraphicsContext gc = canvas.getGraphicsContext2D(); 
     gc.setFill(Color.WHITE); 
     gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight()); 
     apple.pos_A(); 

     // keshidane mar (body yeki ezafe tar az adade morabaA mide) 
     for (int i = X.size() - 1; i >= 0; i--) 
      gc.fillRect(X.get(i), Y.get(i), 10, 10); 

    } 
    public void Move(){ 

     int Px = X.get(X.size() - 1); 
     int Py = Y.get(Y.size() - 1); 

     for (int z = X.size() - 1 ; z > 0 ; z--){ 
      X.remove(z); 
      X.add(z , X.get(z-1)) ; 
      Y.remove(z); 
      Y.add(z , Y.get(z-1)) ; 

     } 

     if (goNorth) { 
      Y.add(0 , Y.get(0) - 10); 
      Y.remove(1); 

     } 
     if (goSouth) { 
      Y.add(0 , Y.get(0) + 10); 
      Y.remove(1); 

     } 
     if (goEast) { 
      X.add(0 , X.get(0) + 10); 
      X.remove(1); 

     } 
     if (goWest) { 
      X.add(0 , X.get(0) - 10); 
      X.remove(1); 

     } 

     apple.Point();  // emtiaz gerefte 
     if (j == false) { 
      X.add(Px); 
      Y.add(Py); 
     } 

     if (X.get(0) > 790){ 
      X.remove(0); 
      X.add(0 , 0); 
     } 
     if (X.get(0) < 0 ){ 
      X.remove(0); 
      X.add(0 , 800); 
     } 
     if (Y.get(0) > 590){ 
      Y.remove(0); 
      Y.add(0 , 0); 
     } 
     if (Y.get(0) < 0 ){ 
      Y.remove(0); 
      Y.add(0 , 600); 
     } 

    } 
} 
+0

我不想使用線程,如果posible。 – nidia95

回答

1

標準的Oracle Java運行時環境可以直接在命令行中執行Application子類,即使他們不包含一個main方法。因此,假如你使用的是標準的JRE,在命令行就可以執行

java Main_Snake 

,它會運行(假定沒有其他錯誤,等等)。

其他環境和大多數IDE不支持這種執行模式,所以如果你想在這些環境中運行(例如在Eclipse中運行),你需要一個啓動你的JavaFX應用程序的方法main(...)。所以只需加上

public static void main(String[] args) { 
    launch(args); 
} 

Main_Snake類。

+0

sry,我不明白在哪裏使用java Main_Snake。和關於第二種方式我使用,但還有那個例外,並更多@James_D – nidia95

+0

正如我所說的,你可以使用'java Main_Snake'從命令行執行。如果你在課堂上加了一個'main'方法,我不相信你會得到同樣的錯誤(說你沒有'main'方法)。 –

+0

異常中的應用構造 異常在線程「主」了java.lang.RuntimeException:無法構建應用實例:類Snake.Main_Snake \t在com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:910) \t at com.sun.javafx.application.LauncherImpl.lambda $ launchApplication $ 156(LauncherImpl.java:187) \t at java.lang.Thread.run(Thread.java:747) – nidia95