user3266259
user3266259

Reputation: 399

Java: draw a rectangular spiral with drawLine

I'm working on a Java programming exercise where I'm supposed to draw a rectangular spiral with the drawLine method. Here's what I have so far, but the spiral comes up looking incomplete.

import java.awt.Graphics; 
import javax.swing.JPanel;
import javax.swing.JFrame;

public class RectSpiral extends JPanel
{
    public void paintComponent(Graphics g)
    {
        int width = getSize().width;
        int height = getSize().height;

        int widthCenter = width / 2;
        int heightCenter = height / 2;

        for (int i = 0; i < 4 ; i++)
        {
            g.drawLine(widthCenter + (20 * i), heightCenter + (20 * i), widthCenter + (20 * i), heightCenter + 20 + (20 * i));
            g.drawLine(widthCenter + (20 * i), heightCenter + 20 + (20 * i), widthCenter - 20 - (20 * i), heightCenter + 20 + (20 * i));
            g.drawLine(widthCenter - 20 - (20 * i), heightCenter + 20 + (20 * i), widthCenter - 20 - (20 * i), heightCenter - 20 - (20 * i));
            g.drawLine(widthCenter - 20 - (20 * i), heightCenter - 20 - (20 * i), widthCenter + 20 + (20 * i), heightCenter - 20 - (20 * i));
        }
    }

    public static void main(String[] args)
    {
        RectSpiral panel = new RectSpiral();

        JFrame application = new JFrame();

        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.add(panel);      
        application.setSize(300, 300); 
        application.setVisible(true);     
    }
}

This is the output:

enter image description here

I've tried adding a fifth line withing the for loop, but the result still comes up incomplete. Any help is appreciated, thank you!

Upvotes: 2

Views: 8821

Answers (2)

tunidevone
tunidevone

Reputation: 1

Try this solution:

package mainpack;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JPanel;

public class SpiralQuadPanel extends JPanel {

    private static final long serialVersionUID = 1L;

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D)g;

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);

        int x = getWidth() / 2;
        int y = getHeight() / 2;

        for (int i = 0; i < 8; i++) {

            int t = i * 20;

            g2d.drawLine(x + t, y + t, x + t, y + 20 + t);
            g2d.drawLine(x + t, y + 20 + t, x - 20 - t , y + 20 + t);
            g2d.drawLine(x - 20 - t, y + 20 + t, x - 20 - t, y - 20 - t);
            g2d.drawLine(x - 20 - t, y - 20 - t, x + 20 + t, y - 20 - t);
            g2d.drawLine(x  + 20 + t, y - 20 - t, x + 20 + t, y + 20 + t);
        }
    }
}

Upvotes: 0

Pshemo
Pshemo

Reputation: 124225

In your first g.drawLine you need to subtract (20 * i) from center of y, to not start

|
|
|
center
|
|<-center + (20 * i)
|

but here:

|
|<-center - (20 * i)
|
center
|
|
|

so use

g.drawLine(widthCenter + (20 * i), heightCenter - (20 * i),
//                                              ^---------------change
           widthCenter + (20 * i), heightCenter + 20 + (20 * i));

Upvotes: 3

Related Questions