Turtle Graphics
重要的要求就是画出一些图案,比如画出一个正方形或者画出自己喜欢的东西,利用颜色或者其他的构造自己喜欢的图案,直接调用就可以了。
public TurtleGUI(List<Action> actionList, int canvasWidth, int canvasHeight) {
super("TurtleGUI");
super("TurtleGUI");
this.actionList = actionList;
this.canvasWidth = canvasWidth;
this.canvasHeight = canvasHeight;
this.originX = (canvasWidth - 1) / 2;
this.originY = (canvasHeight - 1) / 2;
this.canvasWidth = canvasWidth;
this.canvasHeight = canvasHeight;
this.originX = (canvasWidth - 1) / 2;
this.originY = (canvasHeight - 1) / 2;
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
Container cp = this.getContentPane();
GroupLayout layout = new GroupLayout(cp);
cp.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
Container cp = this.getContentPane();
GroupLayout layout = new GroupLayout(cp);
cp.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
currentActionLabel.setText("Currently performing: ");
canvas = new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_RGB);
graphics = canvas.createGraphics();
graphics.setBackground(CANVAS_BG_COLOR);
graphics.clearRect(0, 0, canvasWidth, canvasHeight);
graphics.setStroke(new BasicStroke(1.0f));
graphics = canvas.createGraphics();
graphics.setBackground(CANVAS_BG_COLOR);
graphics.clearRect(0, 0, canvasWidth, canvasHeight);
graphics.setStroke(new BasicStroke(1.0f));
drawLabel = new JLabel(new ImageIcon(canvas));
stoppedAnimation(); // initialize interface elements
runButton.addActionListener(new ActionListener() {
private AnimationThread animationThread;
public void actionPerformed(ActionEvent e) {
if (!isRunning) {
runButton.setText("Stop");
isRunning = true;
animationThread = new AnimationThread();
animationThread.execute();
} else {
animationThread.cancel(true);
}
}
});
private AnimationThread animationThread;
public void actionPerformed(ActionEvent e) {
if (!isRunning) {
runButton.setText("Stop");
isRunning = true;
animationThread = new AnimationThread();
animationThread.execute();
} else {
animationThread.cancel(true);
}
}
});
layout.setHorizontalGroup(layout.createParallelGroup()
.addComponent(drawLabel)
.addGroup(layout.createSequentialGroup()
.addComponent(runButton)
.addComponent(currentActionLabel)
.addComponent(currentAction)));
layout.setVerticalGroup(layout.createSequentialGroup()
.addComponent(drawLabel)
.addGroup(layout.createParallelGroup(Alignment.CENTER)
.addComponent(runButton)
.addComponent(currentActionLabel)
.addComponent(currentAction)));
.addComponent(drawLabel)
.addGroup(layout.createSequentialGroup()
.addComponent(runButton)
.addComponent(currentActionLabel)
.addComponent(currentAction)));
layout.setVerticalGroup(layout.createSequentialGroup()
.addComponent(drawLabel)
.addGroup(layout.createParallelGroup(Alignment.CENTER)
.addComponent(runButton)
.addComponent(currentActionLabel)
.addComponent(currentAction)));
pack();
}
}
下面是调用程序
public DrawableTurtle() {
this.currentPosition = new Point(0, 0);
this.currentHeading = 0.0;
this.currentColor = PenColor.BLACK;
this.lines = new ArrayList<>();
this.actionList = new ArrayList<>();
}