2017-10-12 124 views
1

我試圖繪製多個對象在一個畫布,但總是收到一個運行時錯誤,下面是我的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); 
} 
} 

我想不通我要去哪裏錯了,我想我的循環沒關係......我可以繪製一個矩形,但如果我說我想繪製兩個矩形,它會讓我輸入一個對象的信息,然後給出運行時錯誤。這是我的畫布類:

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

回答

1

的問題是,你是一個null對象上調用GraphicalObject.draw(GraphicsContext)

這是因爲你在GraphicalObjectCanvas.add()方法的末尾呼籲GraphicalObjectCanvas.draw()for環路GraphicalObjectCanvas.draw()使用numObjects來確定指數的範圍。但是,執行for循環時,您尚未創建對象並將其分配給數組graphobs的所有索引。

爲了解決這個問題,無論是在你的GraphicalObjectCanvas.add()方法結束不調用GraphicalObjectCanvas.draw(),或更改GraphicalObjectCanvas.draw()for環路i < index而非i < numObjects

例如:

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

它的工作,謝謝! – Hat