Jump to content
  • Advertisement
Sign in to follow this  
tom_mai78101

[Java] Having Difficulty: Drawing a Spiral Line, yet the paintComponent() doesn't seem to add up.

This topic is 2703 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here's the code:

[source lang="java"]
package Homework;

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

public class Lines extends JPanel {
private final int WIDTH;
private final int HEIGHT;
private int center_X;
private int center_Y;

private enum Direction {
UP, RIGHT, DOWN, LEFT
};

private Direction currentFacing;

public static void main(String arg[]) {
Lines l = new Lines(500, 500);
JFrame app = new JFrame();

app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.add(l);
app.setSize(500, 500);
app.setVisible(true);
}

public Lines() {
WIDTH = 500;
HEIGHT = 500;
currentFacing = Direction.UP;
center_X = WIDTH / 2;
center_Y = HEIGHT / 2;
}

public Lines(int width, int height) {
WIDTH = width;
HEIGHT = height;
currentFacing = Direction.UP;
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
boolean up_down = true;
int length = 1;
byte counter = 2;
for(int i = 0; i < WIDTH / 4; i ++ ) {
for(Direction facing : Direction.values()) {
if(up_down)
drawLine(g, length * 50, facing);
else {
drawLine(g, length * 50, facing);
length ++ ;
}
counter -- ;
if(counter == 0) {
counter = 2;
if(up_down == true)
up_down = false;
else
up_down = true;
}
}
}
}

public void drawLine(Graphics g, int length, Direction direction) {
int lineLength = 50;
switch(direction) {
// UP
case UP :
g.drawLine(center_X, center_Y, center_X, center_Y - lineLength);
center_Y -= lineLength;
break;
// DOWN
case DOWN :
g.drawLine(center_X, center_Y, center_X, center_Y + lineLength);
center_Y += lineLength;
break;
// LEFT
case LEFT :
g.drawLine(center_X, center_Y, center_X - lineLength, center_Y);
center_X -= lineLength;
break;
// RIGHT
case RIGHT :
g.drawLine(center_X, center_Y, center_X + lineLength, center_Y);
center_Y += lineLength;
break;
}
}
} // End class Lines


[/source]
[color="#ffffff"]Props to know those what this Java program's purpose is all about.
The main problem is that the Java application couldn't draw lines in a 90-degree spiral. In fact, it seems like the component wasn't added into the application correctly.

Or there's something wrong that I didn't see it. I would like someone to guide me here. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
don't call

app.add(l);

do this:

app.getContentPane
().add(l,BorderLayout.CENTER);

Share this post


Link to post
Share on other sites

don't call

app.add(l);

do this:

app.getContentPane
().add(l,BorderLayout.CENTER);


There was no change. Still nothing appearing.



EDIT: Finally, I found it! All the time, it was my logic that was incorrect. :P In the public void drawLine() method, I accidentally put in center_Y too many times. If one of them is reverted back to center_X, it will start drawing, in an irregular pattern; looping in a very weird fashion. Fixing this, I was able to draw a 90-degree spiral.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!