Hey Guys - i am really new to Java and am having some serious problems trying to figure out how to use the painting stuff. My problem i am trying to get to work is that i need to create a 'circle' by using lines from X number of points. This should look something like this:

http://img696.imageshack.us/img696/1830/circlek.gif

My question is how on EARTH do i get this to happen? I am issues just drawing my own LINE, let alone using trig to calculate where the 12 points need to be.

So maybe help me start small? Help me with a very basic way to draw multiple lines with changing coordinates? Something like that? Any helps would be fantastic... as right now, i am completely clueless! Thanks for you help!

T

Recommended Answers

All 8 Replies

Here's a very simple line example

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

public class LinePaintDemo extends JPanel{

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        
        int width = getWidth();
        int height = getHeight();
        int midX = width/2;
        int midY = height/2;
        for (int x=0; x<=midX; x+=3){
            g.drawLine(x, 0, midX, midY);
        }
        for (int x=midX; x<=width; x+=3){
            g.drawLine(x, height, midX, midY);
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new LinePaintDemo());
                frame.setSize(300,300);
                frame.setVisible(true);
            }
        });
    }
}

For your circle, if you vary the angle in a loop, the trig for the line endpoints is trivial.

For this, i have to draw lines from each point on the circles circumfrance to every other point. I have figured that i could use the following to get my first point:

x = radius * cos(A)
y = radius * sin(A)

A = 360/N (N = the number of points to draw from)

So i did this, N = 6, radius = 50.

And got this point: (25, 43.3) - this gives me the point if the circles center was at (0,0). But in my case, the circles center is (150,150), which is the middle of my panel. I add 150 to each point to give me my actual point of:
(175, 193)

How do i use this point to get another point? Is this possible? I know i could probably do it using trig, maybe even the forumulas i mentioned above, but no matter what i try, i just can't figure it out.

Any ideas or help would be greatly appreciated! Thank you. I am working on it as i write this - so i will post any updates if i figure it out...

Your math is basically sound. Keep in mind that the Java sin() & cos() functions use radians instead of degrees and that y increase from 0 to height from top to bottom.

Once you have your starting point, keep an extra pair of variables for previousX and previousY. Each iteration, you are calculating x and y and drawing a line from your previous coord pair to (x,y) and then storing x,y in the previous variables.

I am completely confused, sort of... i understand completely how to get my first point. Thats 'easy'... despite the fact my brain now hurts. The 'second' point that is obviously needed to draw a line... seems to be much harder to figure out. I know the center of my circle point, my first point on the circumfrance of the circle, but now i need my 2nd point on the circle.... i have googled radians to try to understand how to get another point, but it just isn't happening.

Sorry if this seems lazy, but im sooo frustrated at this. It shouldn't be this hard - either im really slow and my brain isn't functioning, or im missing some really simple math/logic to get this complete.

Thanks in advance once again for your help.

Here is my attempt at getting this to work:

int width = getWidth();
     int height = getHeight();
     int midX = width / 2;
     int midY = height / 2;
     int x = 0, y = 0, xNew = 0, yNew = 0;
     int radius = 100;
     int numberPoints = 4;
     
     double A = (Math.PI * 2) / (numberPoints);
     System.out.println("A = " + A);
     x = width / 2;
     y = height / 2 - radius;
     
     System.out.println("first x = " + x + " first y = " + y);
     
     for (int i = 0; i <= numberPoints; i++)
     {
    	 xNew = (int) (x + (radius * Math.cos(A)));
    	 yNew = (int) (y + (radius * Math.sin(A)));
    	 
    	 System.out.println(x + " " + y);
    	 System.out.println("xnew = " + xNew + " ynew = " + yNew);
    	 g.drawLine(x, y, xNew, yNew);
    	 
    	 x = xNew;
    	 y = yNew;
    	 
     }

Please rip it apart, tell me im horrible, whatever you need to do/say to help me figure this out! Thank you again for your help, it is really, REALLY appreciated!

This is my 'console' output - from my System.out statements:
A = 1.5707963267948966
first x = 200 first y = 100
200 100
xnew = 200 ynew = 200
200 200
xnew = 200 ynew = 300
200 300
xnew = 200 ynew = 400
200 400
xnew = 200 ynew = 500
200 500
xnew = 200 ynew = 600

Maybe this will make sense as to what is happening to the lines when i draw them etc!?

You are taking the sin/cos of the same angle each iteration. You need to increment that angle.

I would also recommend starting (x,y) at your first point and starting your loop at 1.

You are taking the sin/cos of the same angle each iteration. You need to increment that angle.

I would also recommend starting (x,y) at your first point and starting your loop at 1.

Alright, that being the case, i tried doing this:

int[][] points = new int[4][2];
      int width = frame.getWidth();
      int height = frame.getHeight();
      int midX = width / 2;
      int midY = height / 2;
      int x = 0, y = 0, xNew = 0, yNew = 0;
      int radius = 100;
      int numberPoints = 4;
      double A = (Math.PI * 2) / (numberPoints);
      
      x = width / 2;
      y = height / 2 - radius;

	  points[0][0] = x;
	  points[0][1] = y;
      
      int index = 1;
      for (int i = 1; i < 4; i++)
      {
    	  points[index][0] = (int) (x + (radius * Math.cos(A)));
    	  points[index][1] = (int) (y + (radius * Math.sin(A)));
    	  
     	  x = points[index][0];
    	  y = points[index][1];
    	 
    	  System.out.println("A: " + A + "I: " + i);
    	  System.out.println("X: " + points[index][0] + " Y: " + points[index][1]);
          index++;
    	  A+=A;
    	  }

I get the following for output of my IDE console:
Starting x: 200Starting y: 100
A: 1.5707963267948966I: 1
X: 200 Y: 200
A: 3.141592653589793I: 2
X: 100 Y: 200
A: 6.283185307179586I: 3
X: 200 Y: 199

So obviously my starting point is correct of (200,100), then for some reason it finds the next point to be (200,200) - which is the center of the circle!!! BUT the third point listed, (100,200) would actually be correct, as this would be the 9 o'clock point on the clock face...assuming 4 points around the circle, with the first point at noon...

Am i making the correct assumptions here? Any more ideas? Thank you once again for continued help with this.

You want to calculate the points based on their offset from the center, so this

[B]x[/B] + (radius * Math.cos(A))

should really be

[B]midX[/B] + (radius * Math.cos(A))

Also remember that cos/sin are still based on the standard cartesian x-y axes where angle 0 is towards unit x=1, y=0 and increases counterclockwise towards the y axis. So you are drawing from "3:00" towards "12:00" as you increase the angle. Based on that, you may want to reconsider the location of your first point.

You really don't need to maintain the array unless you have other uses for the points outside of the drawing method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.