Search code examples
javashapesbluej

How to draw a spiral and polygon in Java using the following Pen class and canvas class


I am trying to draw a polygon where the user specifies the number of sides and a spiral using the following classes. I have got a square working but cannot figure out how to draw a polygon. This is the class, which creates a Pen that can be used to draw things

import java.awt.Color;
import java.util.Random;
public class Pen
{
    // constants for randomSquiggle method
    private static final int SQIGGLE_SIZE = 40;
    private static final int SQIGGLE_COUNT = 30;

    private int xPosition;
    private int yPosition;
    private int rotation;
    private Color color;
    private boolean penDown;
    private Canvas canvas;
    private Random random;
    /**
     * Create a new Pen with its own canvas. The pen will create a new canvas for 
     * itself to draw on, and start in the default state (centre of canvas, direction
     * right, color black, pen down).
     */
    public Pen()
    {
        this (280, 220, new Canvas("My Canvas", 560, 440));
    }
    /**
     * Create a new Pen for a given canvas. The direction is initially 0 (to the right),
     * the color is black, and the pen is down.
     *
     * @param xPos  the initial horizontal coordinate of the pen
     * @param yPos  the initial vertical coordinate of the pen
     * @param drawingCanvas  the canvas to draw on
     */
    public Pen(int xPos, int yPos, Canvas drawingCanvas)
    {
        xPosition = xPos;
        yPosition = yPos;
        rotation = 0;
        penDown = true;
        color = Color.BLACK;
        canvas = drawingCanvas;
        random = new Random();
    }
    /**
     * Move the specified distance in the current direction. If the pen is down, 
     * leave a line on the canvas.
     * 
     * @param distance  The distance to move forward from the current location.
     */
    public void move(int distance)
    {
        double angle = Math.toRadians(rotation);
        int newX = (int) Math.round(xPosition + Math.cos(angle) * distance);
        int newY = (int) Math.round(yPosition + Math.sin(angle) * distance);

        moveTo(newX, newY);
    }
    /**
     * Move to the specified location. If the pen is down, leave a line on the canvas.
     * 
     * @param x   The x-coordinate to move to.
     * @param y   The y-coordinate to move to.
     */
    public void moveTo(int x, int y)
    {
        if (penDown) {
            canvas.setForegroundColor(color);
            canvas.drawLine(xPosition, yPosition, x, y);
        }
        xPosition = x;
        yPosition = y;
    }
    /**
     * Turn the specified amount (out of a 360 degree circle) clockwise from the current 
     * rotation.
     * 
     * @param degrees  The amount of degrees to turn. (360 is a full circle.)
     */
    public void turn(int degrees)
    {
        rotation = rotation + degrees;
    }
    /**
     * Turn to the specified direction. 0 is right, 90 is down, 180 is left, 270 is up.
     * 
     * @param angle  The angle to turn to.
     */
    public void turnTo(int angle)
    {
        rotation = angle;
    }
    /**
     * Set the drawing color.
     * 
     * @param newColor  The color to use for subsequent drawing operations.
     */
    public void setColor(Color newColor)
    {
        color = newColor;
    }
    /**
     * Lift the pen up. Moving afterwards will not leave a line on the canvas.
     */
    public void penUp()
    {
        penDown = false;
    }
    /**
     * Put the pen down. Moving afterwards will leave a line on the canvas.
     */
    public void penDown()
    {
        penDown = true;
    }*

and this is the class I am calling the methods from the Pen class into and this is also the class I am trying to create a method, which draws a polygon where the number of sides are specified by the user and a spiral, which is square shaped like this http://vector.me/browse/291037/square_spiral

 import java.awt.Color;
    import java.util.Random;


    public class DrawDemo
    {
        private Canvas myCanvas;
        private Random random;

        /**
         * Prepare the drawing demo. Create a fresh canvas and make it visible.
         */
        public DrawDemo()
        {
            myCanvas = new Canvas("Drawing Demo", 500, 400);
            random = new Random();
        }

        /**
         * Draw a square on the screen.
         */
        public void drawSquare()
        {
            Pen pen = new Pen(320, 260, myCanvas);
            pen.setColor(Color.BLUE);

            square(pen);
        }

           /**
         * Draw a Triangle on the screen.
         */
        public void drawTriangle()
        {
            Pen pen = new Pen(320, 260, myCanvas);
            pen.setColor(Color.GREEN);

            triangle(pen);
        }

            /**
         * Draw a Pentagon on the screen.
         */
        public void drawPentagon()
        {
            Pen pen = new Pen(250, 200, myCanvas);
            pen.setColor(Color.GREEN);

            pentagon(pen);
        }

        /**
         * Draw a wheel made of many squares.
         */
        public void drawWheel()
        {
            Pen pen = new Pen(250, 200, myCanvas);
            pen.setColor(Color.RED);

            for (int i=0; i<36; i++) {
                square(pen);
                pen.turn(10);
            }
        }

        /**
         * Draw a square in the pen's color at the pen's location.
         */
        private void square(Pen pen)
        {
            for (int i=0; i<4; i++) {
                pen.move(100);
                pen.turn(90);
            }
        }

          /**
         * Draw a triangle in the pen's color at the pen's location.
         */
        private void triangle(Pen pen)
        {
            for (int i=0; i<3; i++) {
                pen.move(100);
                pen.turn(120);
            }
        }

            private void pentagon(Pen pen)
        {
            for (int i=0; i<5; i++) {
                pen.move(100);
                pen.turn(-72);
            }
        }

        /**
         * Draw some random squiggles on the screen, in random colors.
         */
        public void colorScribble()
        {
            Pen pen = new Pen(250, 200, myCanvas);

            for (int i=0; i<10; i++) {
                // pick a random color
                int red = random.nextInt(256);
                int green = random.nextInt(256);
                int blue = random.nextInt(256);
                pen.setColor(new Color(red, green, blue));

                pen.randomSquiggle();
            }
        }

        /**
         * Clear the screen.
         */
        public void clear()
        {
            myCanvas.erase();
        }
    }

Solution

  • Polygen: Do you mean regular polygon? The angle you need to turn will be 180°-(((n-2)×180)°)/n which reduce to (360°)/n, where n is the number of side.

    Spiral:

    (Visual explaining picture at the end)

    You need a initial length, i, which is the length of the first line you draw, and w, which is the space between lines.

    Start from top-left. Do the following things until i become less than or equal to 0:

    • Move i
    • Turn 90°
    • Move i
    • Turn 90°
    • i = i - w

    Explained by picture