Riešenia (skupina C, 1. týždeň)

Streda

package sk.paz1a.practicals.task1;

import java.awt.Color;
import java.util.Iterator;

import sk.upjs.jpaz2.*;

public class Launcher {

        public static void main(String[] args) {
                // create new "sandbox" - a place where turtles can live
                AnimatedWinPane sandbox = new AnimatedWinPane();

                // create new turtle and add it to the "sandbox"
                SmartTurtle franklin = new SmartTurtle();
                sandbox.add(franklin);

                // create new object inspector
                ObjectInspector oi = new ObjectInspector();
                // ask the inspector to inspect "franklin" and "sandbox"
                oi.inspect(franklin);
                oi.inspect(sandbox);

                franklin.setPenColor(Color.BLUE);

                Turtle cecil = new Turtle();
                sandbox.add(cecil);
                cecil.setPosition(200, 150);
                cecil.setPenColor(Color.RED);
                oi.inspect(cecil);

                SmartTurtle albert = new SmartTurtle();
                sandbox.add(albert);
                /*
                 * franklin.step(75); franklin.turn(90); franklin.step(75); franklin.turn(90);
                 * franklin.step(75); franklin.turn(90); franklin.step(75); franklin.turn(90);
                 */

                /*
                 * for (int i = 0; i < 4; i++) { franklin.step(75); franklin.turn(90); }
                 *
                 * franklin.triangle(75);
                 */




                // you can put other initialization commands here

        }
}

package sk.paz1a.practicals.task1;

import java.awt.Color;

import sk.upjs.jpaz2.*;

public class SmartTurtle extends Turtle {

        // put new methods here
        public void square() {
                for (int i = 0; i < 4; i++) {
                        this.step(75);
                        this.turn(90);
                }
        }

        public void triangle(double size) {
                for (int i = 0; i < 3; i++) {
                        this.step(size);
                        this.turn(120);
                }
        }

        public void rgbTriangle(double size) {
                this.setPenWidth(2);
                this.setPenColor(Color.RED);
                this.step(size);
                this.turn(120);
                this.setPenColor(Color.GREEN);
                this.step(size);
                this.turn(120);
                this.setPenColor(Color.BLUE);
                this.step(size);
                this.turn(120);

        }

        public void square2(double size) {
                for (int i = 0; i < 4; i++) {
                        this.step(size);
                        this.turn(90);
                }
        }

        public void rectangle(double a, double b) {
                for (int i = 0; i < 2; i++) {
                        this.step(b);
                        this.turn(90);
                        this.step(a);
                        this.turn(90);

                }
        }

        public void house(double size) {
                this.square2(size);
                this.step(size);
                this.turn(30);
                this.triangle(size);
                this.turn(-30);
                this.step(-size);
        }

}

 

Stvrtok

package sk.paz1a.practicals.task1;

import java.awt.Color;
import java.util.Iterator;

import sk.upjs.jpaz2.*;

public class Launcher {

        public static void main(String[] args) {
                // create new "sandbox" - a place where turtles can live
                AnimatedWinPane sandbox = new AnimatedWinPane();

                // create new turtle and add it to the "sandbox"
                SmartTurtle franklin = new SmartTurtle();
                sandbox.add(franklin);

                // create new object inspector
                ObjectInspector oi = new ObjectInspector();
                // ask the inspector to inspect "franklin" and "sandbox"
                oi.inspect(franklin);
                oi.inspect(sandbox);

                franklin.setPenColor(Color.BLUE);

                Turtle cecil = new Turtle();
                sandbox.add(cecil);
                cecil.setPosition(200, 150);
                cecil.setPenColor(Color.RED);
                oi.inspect(cecil);

                SmartTurtle albert = new SmartTurtle();
                sandbox.add(albert);
                /*
                 * franklin.step(75); franklin.turn(90); franklin.step(75); franklin.turn(90);
                 * franklin.step(75); franklin.turn(90); franklin.step(75); franklin.turn(90);
                 */

                /*
                 * for (int i = 0; i < 4; i++) { franklin.step(75); franklin.turn(90); }
                 *
                 * franklin.triangle(75);
                 */




                // you can put other initialization commands here

        }
}

package sk.paz1a.practicals.task1;

import java.awt.Color;
import java.util.Iterator;

import sk.upjs.jpaz2.*;

public class SmartTurtle extends Turtle {

        // put new methods here
        public void square() {
                for (int i = 0; i < 4; i++) {
                        this.step(75);
                        this.turn(90);
                }
        }

        public void triangle(double size) {
                for (int i = 0; i < 3; i++) {
                        this.step(size);
                        this.turn(120);
                }
        }

        public void rgbTriangle(double size) {
                this.setPenWidth(2);
                this.setPenColor(Color.RED);
                this.step(size);
                this.turn(120);
                this.setPenColor(Color.GREEN);
                this.step(size);
                this.turn(120);
                this.setPenColor(Color.BLUE);
                this.step(size);
                this.turn(120);

        }

        public void square2(double size) {
                for (int i = 0; i < 4; i++) {
                        this.step(size);
                        this.turn(90);
                }
        }

        public void filledSquare(double size, Color color) {
                this.setFillColor(color);
                this.penUp();
                this.openPolygon();
                for (int i = 0; i < 4; i++) {
                        this.step(size);
                        this.turn(90);
                }
                this.closePolygon();
        }

        public void rectangle(double a, double b) {
                for (int i = 0; i < 2; i++) {
                        this.step(b);
                        this.turn(90);
                        this.step(a);
                        this.turn(90);

                }
        }

        public void house(double size) {
                this.square2(size);
                this.step(size);
                this.turn(30);
                this.triangle(size);
                this.turn(-30);
                this.step(-size);
        }

        public void filledHexagon(double size, Color color) {
                this.setFillColor(color);
                this.penUp();
                this.openPolygon();
                for(int i=0; i < 6;i++) {
                        this.step(size);
                        this.turn(360/6);
                }
                this.closePolygon();
        }

        public void filledHexagon2(double size, Color color) {
                this.setFillColor(color);
                this.penUp();
                this.openPolygon();
                for(int i=0; i < 6;i++) {
                        this.triangle(size);
                        this.turn(360/6);
                }
                this.closePolygon();
        }

        public void centeredSquare(double sideLength) {
                this.penUp();
                this.step(sideLength/2);
                this.turn(90);
                this.step(sideLength/2);
                this.turn(90);
                this.penDown();
                for (int i = 0; i < 4; i++) {
                        this.step(sideLength);
                        this.turn(90);
                }
                this.penUp();
                this.turn(-90);
                this.step(-sideLength/2);
                this.turn(-90);
                this.step(-sideLength/2);
        }       

        public void dashDotDash(double length) {
                this.setPenColor(Color.BLACK);
                this.setFillColor(Color.YELLOW);
                this.step(length/4);
                this.penUp();
                this.step(length/4);
                this.dot(length/8);
                this.step(length/4);
                this.penDown();
                this.step(length/4);


        }

}