实验一之画图

Turtle Graphics

重要的要求就是画出一些图案,比如画出一个正方形或者画出自己喜欢的东西,利用颜色或者其他的构造自己喜欢的图案,直接调用就可以了。

public TurtleGUI(List<Action> actionList, int canvasWidth, int canvasHeight) {
        super("TurtleGUI");
        this.actionList = actionList;
        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);
        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));
        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);
                }
            }
        });
        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)));
        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<>();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值