fillRect() in Schleife spukt nur Fehler aus

werdas34

Bekanntes Mitglied
Hallo,

ich habe ein mehrdimensionales Array bestehend aus 0 und 1 und möchte in JavaFX dann dazu eine Grafik basteln.
Habe die Grundstruktur von meinem Prof bekommen(siehe Bild). Nun möchte ich das bei jeder 1 ein Feld blau markiert wird. Meine Heransgehensweise war das Mehrdimensionale Array durchzulaufen und mittels if-Abfrage dann das Feld zu bepinseln.
Nur bekomme ich so ganz viele Exceptions:
Java:
// Given a Wolfram Code (eg WC 90, 0 <= WC <= 255) of a 1-d cellular automaton and the number of generations,
// generate and display the resulting configurations.
// Use JavaFX to graphically display the configurations.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;

public class CA1 extends Application  // class CA MUST NOT have a constructor (else java.lang.reflect.InvocationTargetException)
{
    public static final int WC = 30;  // Wolfram Code for 1-d cellular automata
    public static final int GENS = 10;  // number of generations
    public static final int range = 2 * GENS + 1;

   
    public static final int SF = 20;  // scale factor: 1 cell = SF*SF pixels
   
    static int[][] cell ;  // global variable for communication with start(stage) method
   
    //*****************************************************************************************
    public static void main(String[] args)
    {   
        System.out.println("WC-" + WC + ", gens = " + GENS);
        int[][] cell = new int[][] {
   
                    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0},
                    {0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0},
                    {0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0},
                    {0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0}
        };

    //    System.out.println(cell[0][GENS] == 1);

       
        // derive the rules of the CA from the given WC
       
        // fill the cell array according to the rules
       
        // print cell[][]

       
        Application.launch();  // start graphics
    }
   
   
    //*****************************************************************************************
    // method required by JavaFX Application to draw graphics
    // cp. https://docs.oracle.com/javafx/2/canvas/jfxpub-canvas.htm
    @Override
    public void start(Stage myStage)
    {
        String myTitle = "1-d CA:  WC-" + WC + ";  " + GENS + " Generations";
        myStage.setTitle(myTitle);
        Group root = new Group();
       
        int canSize = (2*GENS+1)*SF;
        Canvas canvas = new Canvas(canSize, canSize);
       
        GraphicsContext gc = canvas.getGraphicsContext2D();
        gc.setStroke(Color.GREY);
        gc.setFill(Color.BLUE);  // color of living cell
        gc.setLineWidth(1);
       
        // draw horizontal lines: two per generation
        for (int row=1; row<=GENS; row++)
        {
            gc.strokeLine(0, (2*row-1)*SF, canSize, (2*row-1)*SF);
            gc.strokeLine(0, (2*row  )*SF, canSize, (2*row  )*SF);
        }
        // draw vertical line segments: one for each generation
        for (int col=0; col<2*GENS+1; col++)
        {
            for (int row=1; row<=GENS; row++)
                gc.strokeLine(col*SF, (2*row-1)*SF, col*SF, (2*row)*SF);
        }   

        // draw cell[][]
        // gc.fillRect(0,20,1*SF,1*SF); //draws rectangles
        // gc.fillRect(20,60,1*SF,1*SF); //draws rectangles
        
        int x = 0, y = 0;
        for (int zeile = 0; zeile < cell.length; zeile++) {
            for (int spalte = 0; spalte < cell[zeile].length; spalte++) {
                if(cell[zeile][spalte] == 1) {
                    gc.fillRect(x, y, SF, SF);
                }
                y = y + 40;
            }
            x = x + 20;
        }
        
        

          
        root.getChildren().add(canvas);
        myStage.setScene(new Scene(root));
        myStage.show();
    }   
   
}

Das ist die Schleife:
Java:
        int x = 0, y = 0;
        for (int zeile = 0; zeile < cell.length; zeile++) {
            for (int spalte = 0; spalte < cell[zeile].length; spalte++) {
                if(cell[zeile][spalte] == 1) {
                    gc.fillRect(x, y, SF, SF);
                }
                y = y + 40;
            }
            x = x + 20;
        }

Ich habe bisjetzt noch nie mit JavaFX gearbeitet, von dem her wird die Lösung wahrscheinlich recht simpel sein.

Hier noch die Exceptions:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at CA1.start(CA1.java:92)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Exception running application CA1
 

Anhänge

  • Grundgerüst.PNG
    Grundgerüst.PNG
    6,8 KB · Aufrufe: 19
  • so solls am Ende ausschauen.PNG
    so solls am Ende ausschauen.PNG
    11,2 KB · Aufrufe: 17
Die lokale Variable int[][] cell in der main() verdeckt das statische Feld static int[][] cell der Klasse CA1. Somit wird bei Zugriff auf dieses Feld in Zeile 92 eine NPE geworfen. Du solltest die Deklaration einer neuen lokalen Variable in der main() Methode vermeiden und stattdessen auf das Feld zuweisen.
 
Danke für deine Antwort. Da war wohl meine Dummheit im Weg.
Ich habe nun die Schleife angepasst, aber er markiert nur das erste Feld richtig. Dann passiert garnichts. Kommentiere ich x = x +20 aus dann markiert er links jede Zeile.
Java:
   int x = 0, y = 20;
        for (int zeile = 0; zeile < cell.length; zeile++) {
            for (int spalte = 0; spalte < cell[zeile].length; spalte++) {
                if(cell[zeile][spalte] == 1) {
                    gc.fillRect(x, y, SF, SF);
                }
                x = x + 20;
            }
            y = y + 40;
        }
Wäre super wenn du mir da noch nen Tipp geben kannst.
 

Neue Themen


Zurück
Oben