0

I wanna write a Snake game with javaFX , but there is exception that I don't know about and I want to know how to fix it. ( i know it's not complete yet )

and I want to know , the class that extends Application ( with start override ) is exactly the main in other programs? as you see , here is not static void main BC I didn't need, but if i want to add main where shoud i do?

it's the 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)

and my code is :

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 sor@

                    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; // sor@
                }

            }
        };

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

    }
}
nidia95
  • 1
  • 1
  • 2

1 Answers1

1

The standard Oracle Java Runtime Environment can execute Application subclasses directly from the command line, even if they do not contain a main method. So assuming you are using a standard JRE, from the command line you can execute

java Main_Snake

and it will run (assuming no other errors, etc).

Other environments, and most IDEs, don't support this execution mode, so if you want to run in those environments (including running in Eclipse, for example), you need a main(...) method which launches your JavaFX application. So just add

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

to the Main_Snake class.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • sry,I didn't understand where shoud I use java Main_Snake . and about second way I Used but there is that exception yet and be more @James_D – nidia95 Jan 05 '16 at 21:58
  • As I said, you can execute from the command line using `java Main_Snake`. I don't believe you are getting the same error (saying you don't have a `main` method) if you add a `main` method to your class. – James_D Jan 05 '16 at 22:08
  • Exception in Application constructor Exception in thread "main" java.lang.RuntimeException: Unable to construct Application instance: class Snake.Main_Snake at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:910) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(LauncherImpl.java:187) at java.lang.Thread.run(Thread.java:747) – nidia95 Jan 05 '16 at 22:24
  • yes sry there is a little diffrent. Caused by: java.lang.StackOverflowError at Snake.Snake.(Snake.java:7) at Snake.Main_Snake.(Main_Snake.java:25) at Snake.Snake.(Snake.java:7) – nidia95 Jan 05 '16 at 22:28
  • So that's a different question, and you haven't posted the code in order to diagnose it. Assuming you don't want to post a new question on here every single time you encounter an error, it might help to read http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – James_D Jan 05 '16 at 23:06