[英]Setting an initial location to JFrame, using “super” keyword
我在使用方法重載為屏幕設置“烏龜”(實際上是三角形)的初始位置時遇到了麻煩。 我的意思是使用super關鍵字在新Dynamicturtle構造函數中調用Turtle,然后從新Dynamic Turtle構造函數的新參數設置Turtle的位置,然后繪制Turtle。 有提示要使用move方法來實現這一目標嗎?
然后,它將創建一個新的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(); } }
謝謝您提前提供的幫助。
將構造函數更改為此:
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();
的重復行this.drawTurtle();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.