[英]Shape displaying shapes to JFrame using cartesian coordinates, getting a NullPointerException error
I have read similar links discussing NullPointerExeception
, however I am just lost now, I am new to Java and would greatly respect any help given.我看过类似的链接讨论
NullPointerExeception
,但我只是现在失去了,我是新来的Java,并会极大地尊重给予任何帮助。
I believe this error(from the other links on this website) come from initiating the initial location for the turtle
object to be drawn on the JFrame
.我相信这个错误(来自本网站的其他链接)来自启动要在
JFrame
上绘制的turtle
对象的初始位置。 I cannot however do this and I'm stuck.然而,我不能这样做,我被卡住了。
The program is using a main
class, an abstract Shape
class, and a Square
class that extends the Shape
class.该程序使用了一个
main
类、一个抽象的Shape
类和一个扩展了Shape
类的Square
类。 It is also using a Turtle
class(the turtle draws the line to the screen with a drawLineBetweenPoints
method.它还使用了
Turtle
类(海龟使用drawLineBetweenPoints
方法将线绘制到屏幕上。
Here is the main
class:这是
main
类:
import javax.swing.*;
class Lab4b
{
public static void main(String [] args)
{
JFrame frame = new JFrame();
Canvas canvas = new Canvas();
frame.setTitle("Hello Frame");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(canvas);
CartesianCoordinate position = new CartesianCoordinate(400, 300);
Turtle liam = new Turtle(canvas, position);
Shape square = new Square(canvas, position);
square.draw();
System.out.println("X is: " + square.getSize());
}
}
Here is the Shape
class:这是
Shape
类:
abstract class Shape
{
protected double size = 100;
protected double angle = 0;
protected Canvas canvas;
protected CartesianCoordinate position = new CartesianCoordinate(400, 300);
protected Turtle turtle = new Turtle(canvas, position);
private CartesianCoordinate myLocation, oldLocation;
Shape(Canvas canvas, CartesianCoordinate position)
{
this.canvas = canvas;
Turtle turtle = new Turtle(canvas, position);
this.myLocation = new CartesianCoordinate(0,0);
myLocation = position.copy();
}
//GETTERS
public double getSize()
{
return size;
}
public double getAngle()
{
return angle;
}
//SETTERS
public void setSize(double size)
{
size = this.size;
}
public void setAngle(double angle)
{
angle = this.angle;
}
public void noise()
{
System.out.println("WORKING CORRECTLY");
}
//ABSTRACT METHOD
public abstract void draw();
}
Here is the Square
class:这是
Square
类:
class Square extends Shape
{
Square(Canvas canvas, CartesianCoordinate position)
{
super(canvas, position);
this.draw();
}
public void draw()
{
turtle.putPenDown();
turtle.turn(95);
turtle.move(100);
turtle.putPenDown();
turtle.turn(95);
turtle.move(100);
turtle.putPenDown();
turtle.turn(95);
turtle.move(100);
turtle.putPenDown();
turtle.turn(95);
turtle.move(100);
turtle.putPenDown();
}
}
Finally, here is the Turtle
class: (I've almost copied it's constructor to get it to work with the Shape
class.)最后,这是
Turtle
类:(我几乎复制了它的构造函数以使其与Shape
类一起工作。)
class Turtle
{
private Canvas canvas; // private field reference to a canvas private
private CartesianCoordinate myLocation, oldLocation;
private CartesianCoordinate newPosition;
private boolean penDown = true;
private double Angle;
public Turtle kieranMullen;
public Turtle(Canvas canvas, CartesianCoordinate initLocation)
{
this.canvas = canvas;
this.myLocation = new CartesianCoordinate(0,0);
Angle = 0;
penDown = true;
myLocation = initLocation.copy();
}
public void putPenUp()
{
this.penDown = false;
}
public void putPenDown()
{
this.penDown = true;
}
public void goTo(CartesianCoordinate newPosition)
{
this.newPosition = myLocation;
}
public void turn(double amount)
{
Angle = Angle + amount;
}
public void move(int pixels)
{
double radians = Math.toRadians(Angle);
double dx = pixels * Math.sin(radians);
double dy = pixels * Math.cos(radians);
CartesianCoordinate oldLocation = myLocation.copy();
myLocation.add(dx,dy);
if(penDown)
{
canvas.drawLineBetweenPoints(myLocation, oldLocation);
}
}
public void drawTurtle()
{
this.putPenDown();
this.turn(90);
this.move(10);
this.putPenDown();
this.turn(240);
this.move(20);
this.putPenDown();
this.turn(240);
this.move(20);
this.putPenDown();
this.turn(240);
this.move(10);
this.turn(270);
}
public void unDrawTurtle()
{
canvas.removeMostRecentLine();
canvas.removeMostRecentLine();
canvas.removeMostRecentLine();
canvas.removeMostRecentLine();
}
public void showSquare()
{
this.unDrawTurtle();
Utils.pause(1000);
this.drawTurtle();
Utils.pause(1000);
this.unDrawTurtle();
this.move(100);
this.drawTurtle();
Utils.pause(1000);
this.unDrawTurtle();
this.turn(90);
this.drawTurtle();
Utils.pause(1000);
this.unDrawTurtle();
this.move(100);
this.drawTurtle();
Utils.pause(1000);
this.unDrawTurtle();
this.turn(90);
this.drawTurtle();
Utils.pause(1000);
this.unDrawTurtle();
this.move(100);
this.drawTurtle();
Utils.pause(1000);
this.unDrawTurtle();
this.turn(90);
this.drawTurtle();
Utils.pause(1000);
this.unDrawTurtle();
this.move(100);
this.drawTurtle();
Utils.pause(1000);
this.unDrawTurtle();
this.turn(90);
this.drawTurtle();
Utils.pause(1000);
this.unDrawTurtle();
}
}
The error is错误是
Exception in thread "main" java.lang.NullPointerException
at Turtle.move(Turtle.java:52)
at Square.draw(Square.java:14)
at Square.<init>(Square.java:7)
at Lab4b.main(Lab4b.java:21)
You are need to change the Shape constructor because canvas
is null for the Turtle and you are variable shadowing .您需要更改 Shape 构造函数,因为 Turtle 的
canvas
为 null 并且您是变量 shadowing 。
protected Canvas canvas;
protected Turtle turtle; // <-- Remove declaration here
Shape(Canvas canvas, CartesianCoordinate position)
{
this.canvas = canvas;
this.turtle = new Turtle(canvas, position); // <--- Do it here
// this.myLocation = new CartesianCoordinate(0,0); // <-- Not needed
this.myLocation = position.copy();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.