0
メソッドのオーバーロードを使用して画面に "カメ"(実際は三角形)の初期位置を設定する際に問題があります。私は、スーパーキーワードを使って新しいDynamicturtleコンストラクタでTurtleを呼び出し、新しい動的なタートルのコンストラクタの新しい引数からカメの位置を設定し、次にカメを描画するつもりです。これを達成するために移動方法を使用するヒントが与えられましたか?"super"キーワードを使用して初期位置をJFrameに設定する
これで新しいDynamicturtle変数が作成され、メインクラスでインスタンス化され、JFrameにタートルが描画されます。私はこれを達成する方法に完全に固執しています - 単純な問題のようです。どんな助けでも大歓迎です。
メインクラス。
import javax.swing.*;
class Lab5
{
\t public static void main(String [ ] args)
\t {
\t \t JFrame frame = new JFrame();
\t \t Canvas canvas = new Canvas();
\t \t frame.setTitle("Hello Frame");
\t \t frame.setSize(800, 600);
\t \t frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
\t \t frame.setVisible(true);
\t \t frame.add(canvas);
\t \t int deltaTime = 20; // milliseconds
\t \t // game loop
\t \t
\t \t Dyanmicturtle jig = new Dyanmicturtle(canvas, 400, 300);
\t \t jig.drawTurtle();
\t \t while(true)
\t \t {
\t \t Utils.pause(deltaTime);
\t \t }
\t }
}
DynamicTurtleクラス:
class DynamicTurle extends Turtle
{
\t private int speed = 100;
\t private int time;
\t private double xPos, yPos;
\t private CartesianCoordinate myLocation;
\t private int Angle = 0;
\t DynamicTurle(Canvas canvas, CartesianCoordinate initLocation)
\t {
\t \t super(canvas, initLocation);
\t \t this.drawTurtle();
\t }
\t DynamicTurle(Canvas canvas, double xPosition, double yPosition) //THE PROBLEM CONSTRUCTOR
\t {
\t \t super(canvas, newPosition);
\t \t CartesianCoordinate newPosition = new CartesianCoordinate (this.xPosition, this.yPosition); \t \t \t
\t \t this.drawTurtle();
\t }
\t public int getSpeed()
\t {
\t \t return this.speed;
\t }
\t public void setSpeed(int speed)
\t {
\t \t this.speed = speed;
\t }
\t public void update(int time)
\t {
\t \t this.unDrawTurtle();
\t \t this.move((speed*100/time)); \t
\t \t this.drawTurtle();
\t }
}
タートルクラス:
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();
}
}
ありがとう、事前に与えられた任意の助け。
ありがとうの繰り返し行を回避
を!エラーもランタイムエラーもなくコンパイルされたものの、タートルをキャンバスに描画しません。ご協力ありがとうございました!非常に感謝! – jHornsby
私の悪い!私はコンパイルする前にメインクラスを保存していない、非常に多くのJorn! – jHornsby