Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-07-2009, 11:51 AM
Member
 
Join Date: Apr 2008
Posts: 42
Rep Power: 0
pheonix is on a distinguished road
Default How to write numbers around a circle
Hi
I am using this code to write numbers around a circle (like a radar). The numbers are generated and written in the GUI but not in the in the way that I want it to be. (I want it to be like a watch and the numbers are around the circumference of the circle)

So I if anyone could modify this code or give an easier solution.

Thank you.
/////////////////////////////
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TestingPoint{
	public static void main(String[] args){
		MyGraph w = new MyGraph();
		w.setVisible(true);
	}
}

class MyGraph extends JFrame{
	public MyGraph(){
		super("Graph");
		setSize(600,600);
		setLayout(new FlowLayout());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public void paint(Graphics g){
		super.paint(g);
		g.drawOval(100,100,400,400);
		
		double safeP = 59;
		int xCor = 0;
		int yCor = 0;
		for(int i = 0; i <= (safeP - 1); i = i + 5){
			//double i = 0;
			double radAngle = ((double)i/(safeP)) * Math.PI * 2.0;
			double degAngle = radAngle * (180.0/Math.PI);
			xCor = (int)((200.0) + (250.0 * Math.cos(degAngle)));
			yCor = (int)((200.0) + (250.0 * Math.sin(degAngle)));
			g.drawString(Integer.toString(i), Math.abs(xCor), Math.abs(yCor));
			//g.drawString(Integer.toString(xCor), xCor, yCor);
		}
	}
}

Last edited by Fubarable; 06-07-2009 at 01:28 PM. Reason: added code tags
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 06-07-2009, 01:30 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,506
Rep Power: 8
Fubarable is on a distinguished road
Default
To the original poster: I added code tags to your posted code to make it more readable. Please try to use these in future posts. Thanks!
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-07-2009, 02:01 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,506
Rep Power: 8
Fubarable is on a distinguished road
Default
Also,
1) Have a look at the Math library in the API. Does it use radians or degrees when calculating sin and cos?
2) You translate your numbers by the radius, but you also need to take into account the translation of your circle by 100 in both x and y directions.
3) Draw on a JPanel and not directly on the JFrame. Override JPanel's paintComponent, not the paint method.
4) Avoid magic numbers and instead use constants or variables. It will make your calcs much easier to debug. For instance,
Code:
//... code deleted.  I didn't like it.
edit: I hate to give exact solutions, but I'm not sure if you used my previous code which I don't like as it had some unnecessary redundancies. Here is I think a better solution:

Code:
import java.awt.*;
import javax.swing.*;

public class TestingPoint2 {
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
	MyGraph2 graph = new MyGraph2();

	JFrame frame = new JFrame("Graph");
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.getContentPane().add(graph);
	frame.pack();
	frame.setLocationRelativeTo(null);
	frame.setVisible(true);
      }
    });
  }
}

class MyGraph2 extends JPanel {
  private static final int APP_WIDTH = 600;
  private static final int RADIUS = 200;
  private static final int DELTA_RADIUS = 25;
  private static final int OUTER_RADIUS = RADIUS + DELTA_RADIUS;
  private static final int CENTER_X = 300;
  private static final int CENTER_Y = 300;
  private static final int SAFE_P = 60;
  private static final int LABEL_FONT_SIZE = 32;

  public MyGraph2() {
    setPreferredSize(new Dimension(APP_WIDTH, APP_WIDTH));
    
    // one of the few times I recommend absolute/null positioning
    setLayout(null);

    for (int i = 0; i < SAFE_P; i += 5) {
      JLabel label = new JLabel(String.valueOf(i));
      label.setFont(new Font(Font.SANS_SERIF, Font.BOLD, LABEL_FONT_SIZE));
      Dimension labelSize = label.getPreferredSize();

      double radAngle = ((double) i / (SAFE_P)) * Math.PI * 2.0;
      int xCor = (int) (CENTER_X + (OUTER_RADIUS * Math.cos(radAngle)));
      int yCor = (int) (CENTER_Y + (OUTER_RADIUS * Math.sin(radAngle)));

      // To center the label at location
      xCor -= labelSize.width / 2;
      yCor -= labelSize.height / 2;

      label.setSize(labelSize);
      label.setLocation(xCor, yCor);
      add(label);
    }
  }

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

    // to create smooth graphics
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
	RenderingHints.VALUE_ANTIALIAS_ON);

    int x0 = CENTER_X - RADIUS;
    int y0 = CENTER_Y - RADIUS;
    int diameter = 2 * RADIUS;
    g.drawOval(x0, y0, diameter, diameter);
  }
}

Last edited by Fubarable; 06-07-2009 at 04:16 PM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-07-2009, 05:02 PM
Member
 
Join Date: Apr 2008
Posts: 42
Rep Power: 0
pheonix is on a distinguished road
Default
Thank you my friend I really appreciate this help

Regards
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-10-2009, 06:00 PM
Member
 
Join Date: Apr 2009
Posts: 53
Rep Power: 0
pellebye is on a distinguished road
Default
I find this very interesting, too. What do you mean with "magic numbers"?
__________________
-
Life is not the worst thing we have ... in a few minutes my coffee is ready.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-10-2009, 07:32 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,506
Rep Power: 8
Fubarable is on a distinguished road
Default
Originally Posted by pellebye View Post
I find this very interesting, too. What do you mean with "magic numbers"?
Note the difference in readability between this:
Code:
      int xCor = (int) (300 + (225 * Math.cos(radAngle)));
      int yCor = (int) (300 + (225 * Math.sin(radAngle)));
and this (note that CENTER_X & Y as well as OUTER_RADIUS are all constants):
Code:
      int xCor = (int) (CENTER_X + (OUTER_RADIUS * Math.cos(radAngle)));
      int yCor = (int) (CENTER_Y + (OUTER_RADIUS * Math.sin(radAngle)));
It's easy to understand the above in a small class, but imagine the raw numbers sprinkled throughout a large class vs defined constants. Which is easier to debug?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-10-2009, 08:01 PM
Member
 
Join Date: Apr 2009
Posts: 53
Rep Power: 0
pellebye is on a distinguished road
Default
Ok I understand. Thank you!
__________________
-
Life is not the worst thing we have ... in a few minutes my coffee is ready.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-10-2009, 10:59 PM
Senior Member
 
Join Date: Mar 2009
Posts: 392
Rep Power: 2
Singing Boyo is on a distinguished road
Default
Also on "magic numbers" vs defined constants... If you go to change the value of one "magic number", you need to change it throughout your code. If you use a defined constant, you change the initialization of the constant at one place, and thats all you have to do.
__________________
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 06-11-2009, 11:20 AM
Member
 
Join Date: Apr 2009
Posts: 53
Rep Power: 0
pellebye is on a distinguished road
Default
"magic numbers" sounds strange in my ears, but I see the point and obvious it is a good thing to make defined constants. Thank you for making it very clear.

I think this thread should be closed but find nowhere to do it. Maybe it is only pheonix who can close the subject?
__________________
-
Life is not the worst thing we have ... in a few minutes my coffee is ready.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generate numbers around a circle? pheonix New To Java 4 06-05-2009 06:08 PM
java program help. find the radius of a circle megironi New To Java 1 03-27-2009 08:09 AM
printing two smallest numbers from a series of numbers trofyscarz New To Java 2 10-15-2008 12:46 AM
In which circle is the Point lying? nidhirastogi New To Java 1 07-03-2008 12:12 AM
Write an application that displays the numbers 1 to 4 on the same line toby New To Java 1 07-23-2007 05:33 PM


All times are GMT +2. The time now is 05:37 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org