如何在 canvas 上绘制多个对象?
How to draw multiple objects on a canvas?
我试图在 canvas 中绘制多个对象,但一直出现运行时错误,下面是我的 GraphicalObject Class:
/*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 class:
/**
* 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();
}
}
问题是您在 null
对象上调用 GraphicalObject.draw(GraphicsContext)
。
发生这种情况是因为您在 GraphicalObjectCanvas.add()
方法的末尾调用 GraphicalObjectCanvas.draw()
并且 GraphicalObjectCanvas.draw()
中的 for
循环使用 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??);
}
我试图在 canvas 中绘制多个对象,但一直出现运行时错误,下面是我的 GraphicalObject Class:
/*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 class:
/**
* 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();
}
}
问题是您在 null
对象上调用 GraphicalObject.draw(GraphicsContext)
。
发生这种情况是因为您在 GraphicalObjectCanvas.add()
方法的末尾调用 GraphicalObjectCanvas.draw()
并且 GraphicalObjectCanvas.draw()
中的 for
循环使用 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??);
}