我无法使用方法重载将“乌龟”(实际上是三角形)的初始位置设置到屏幕上。我打算使用super关键字在新的Dynamicturtle构造函数中调用Turtle,然后从新的动态乌龟构造函数的新参数中设置乌龟的位置,然后绘制乌龟。给出了使用移动方法来实现这一目的的提示?
然后它将创建一个新的Dynamicturtle变量并在主类中实例化它,它会将乌龟绘制到JFrame。我完全坚持如何实现这一目标 - 这似乎是一个简单的问题。任何帮助将不胜感激。
主要课程。
import javax.swing.*;
class Lab5
{
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);
int deltaTime = 20; // milliseconds
// game loop
Dyanmicturtle jig = new Dyanmicturtle(canvas, 400, 300);
jig.drawTurtle();
while(true)
{
Utils.pause(deltaTime);
}
}
}
DynamicTurtle类:
class DynamicTurle extends Turtle
{
private int speed = 100;
private int time;
private double xPos, yPos;
private CartesianCoordinate myLocation;
private int Angle = 0;
DynamicTurle(Canvas canvas, CartesianCoordinate initLocation)
{
super(canvas, initLocation);
this.drawTurtle();
}
DynamicTurle(Canvas canvas, double xPosition, double yPosition) //THE PROBLEM CONSTRUCTOR
{
super(canvas, newPosition);
CartesianCoordinate newPosition = new CartesianCoordinate (this.xPosition, this.yPosition);
this.drawTurtle();
}
public int getSpeed()
{
return this.speed;
}
public void setSpeed(int speed)
{
this.speed = speed;
}
public void update(int time)
{
this.unDrawTurtle();
this.move((speed*100/time));
this.drawTurtle();
}
}
海龟类:
class Turtle
{
private Canvas canvas; // private field reference to a canvas private
private CartesianCoordinate myLocation, oldLocation;
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 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();
}
}
谢谢你提前给出的任何帮助。
答案 0 :(得分:0)
将构造函数更改为:
DynamicTurle(Canvas canvas, double xPosition, double yPosition) {
super(canvas, new CartesianCoordinate (xPosition, yPosition));
this.drawTurtle();
}
或者甚至更好,打电话给你的其他构造函数:
DynamicTurle(Canvas canvas, double xPosition, double yPosition) {
this(canvas, new CartesianCoordinate (xPosition, yPosition));
}
这避免了重复的this.drawTurtle();