我正在尝试在画布中绘制多个对象,但始终遇到运行时错误,以下是我的GraphicalObject类:

/*import javafx.scene.canvas.Canvas;*/
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import java.util.Scanner;

public class GraphicalObject { //class
  private Scanner scan;
  private int x;
  private int y;
  private int width;
  private int height;
  //private double xPoints;
  //private double yPoints;
  //private int numPoints;

  /*
   * constructor
   */
  public GraphicalObject(){
    this.x = 0;
    this.y = 0;
    this.width = 0;
    this.height = 0;
    //this.xPoints = 0;
    //this.yPoints = 0;
    //this.numPoints = 0;
  }

    public GraphicalObject(int x, int y, int width, int height){
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
  }

    //think of get/set methods ie acessor/mutators
    //get x
    //get y
    //get width
    //get height

public void draw(GraphicsContext gc){
  //int numPoints = 5;
  //double[] xPoints = new double[numPoints];
  //double[] yPoints = new double[numPoints];
  //xPoints = [x + (width/2), x + width, x + .8333 width, x + .1667 width, x]
  //yPoints = [y, y + (height/2), y + height, y + height, y + (height/2)]
` gc.setFill(Color.PINK);
  gc.fillRect(this.x, this.y, this.width, this.height);
  //gc.fillPolygon(xPoints, yPoints, numPoints);
}
}


我无法弄清楚哪里出了问题,我认为我的for循环还可以...我可以绘制一个矩形,但是如果我想绘制两个矩形,它可以让我输入一个对象的信息,然后给出运行时错误。这是我的画布类:

/**
 * A basic canvas object that can store and
 * draw graphical object
 */

import javafx.scene.canvas.Canvas;
import javafx.scene.paint.Color;
import javafx.scene.canvas.GraphicsContext;
import java.util.Scanner;

public class GraphicalObjectCanvas extends Canvas {

  // data fields

  // constants
  public static final int C_WIDTH = 500;
  public static final int C_HEIGHT = 500;

  // instance variables

  /**
   * a scanner object to make the Canvas interactive
   */
  private Scanner scan;

  // TO DO: DECLARE YOUR ARRAY OF GRAPHICAL OBJECTS HERE
  // (AND ANY OTHER INSTANCE VARIABLES YOU NEED TO MAINTAIN YOUR ARRAY)
    GraphicalObject[] graphobs;
    private int index;
    private int numObjects;
  /**
   * Creates a canvas for drawing graphical objects
   * with a size of C_WIDTHxC_HEIGHT pixels
   */
  public GraphicalObjectCanvas() {
    super(C_WIDTH, C_HEIGHT);

    // initialize the scanner object
    this.scan = new Scanner(System.in);

    // find out how many objects the user wants to add
    System.out.println("How many graphical objects do you want?");
     numObjects = scan.nextInt();

    // TO DO: DEFINE YOUR ARRAY OF GRAPHICAL OBJECTS HERE
    graphobs = new GraphicalObject[numObjects];


    // for each object they wanted to add...
    for (int i = 0; i < numObjects; i++) {
      this.add();
    }
  }

  public void draw() {

    // clear the picture
    this.clear();

    // get the graphics context from the canvas
    GraphicsContext gc = this.getGraphicsContext2D();

    // TO DO: LOOP THROUGH YOUR ARRAY OF GRAPHICAL OBJECTS
    // AND TELL EACH ONE TO DRAW PASSING THE GRAPHICS CONTEXT (gc) AS INPUT

    for(int i = 0; i < numObjects; i++){
     graphobs[i].draw(gc);
     System.out.println("Object drawn: " + i); //or 1??);
    }
  }

  private void clear() {
    GraphicsContext gc = this.getGraphicsContext2D();
    gc.setFill(Color.WHITE);
    gc.fillRect(0, 0, C_WIDTH, C_HEIGHT);
  }

  private void add() {
    System.out.println("What is the x location of the object?");
    int x = scan.nextInt();
    System.out.println("What is the y location of the object?");
    int y = scan.nextInt();
    System.out.println("What is the width of the object?");
    int width = scan.nextInt();
    System.out.println("What is the height of the object?");
    int height = scan.nextInt();

    // TO DO: USE THE INFORMATION ABOVE TO CREATE A NEW GRAPHICAL OBJECT
    // AND ADD IT TO THE ARRAY OF OBJECTS.
    GraphicalObject gob = new GraphicalObject( x, y, width, height);
    graphobs[index] = gob;
    index++;

    // after adding new object, redraw the canvas
    this.draw();
  }
}

最佳答案

问题是您要在GraphicalObject.draw(GraphicsContext)对象上调用null

发生这种情况的原因是,您在GraphicalObjectCanvas.draw()方法的末尾调用GraphicalObjectCanvas.add(),并且for中的GraphicalObjectCanvas.draw()循环使用numObjects来确定索引范围。但是,当此graphobs循环执行时,您尚未创建对象并将其分配给数组for的所有索引。

要解决此问题,请不要在GraphicalObjectCanvas.draw()方法的末尾调用GraphicalObjectCanvas.add(),或者将for中的GraphicalObjectCanvas.draw()循环更改为i < index而不是i < numObjects

例如:

for(int i = 0; i < index; i++){
    graphobs[i].draw(gc);
    System.out.println("Object drawn: " + i); //or 1??);
}

09-28 12:00