java - 如何在 Canvas 上绘制多个对象?

标签 java object javafx draw

我尝试在 Canvas 中绘制多个对象,但不断收到运行时错误,下面是我的 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 循环没问题...我可以绘制一个矩形,但如果我说我想绘制两个,它可以让我输入信息一个对象,然后给出运行时错误。这是我的 Canvas 类:

/**
 * 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??);
}

关于java - 如何在 Canvas 上绘制多个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46714792/

相关文章:

JavaFX:实时修改按钮文字

java - 使用 ConcMarkSweepGC 的连续 CMS 收集

java - 创建类和方法头

java - 生成错误 : Cannot find the class file for org. openqa.selenium.internal.Locatable

javascript - 合并和重新排序两个对象数组的问题

excel - Excel.ThisWorkbook 和 ThisWorkbook 之间的区别?

java - 如果温度对象表示低于冰点的温度,则返回 true 的方法(JAVA)

java - 进度指示器一直旋转

java - 手动关闭java swing窗口

javafx - 如何为 RadioButton 分配隐藏的整数值?