How to write numbers around a circle

• 06-07-2009, 10:51 AM
pheonix
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);                 }         } }```
• 06-07-2009, 12:30 PM
Fubarable
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!
• 06-07-2009, 01:01 PM
Fubarable
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);   } }```
• 06-07-2009, 04:02 PM
pheonix
Thank you my friend I really appreciate this help

Regards
• 06-10-2009, 05:00 PM
pellebye
I find this very interesting, too. What do you mean with "magic numbers"?
• 06-10-2009, 06:32 PM
Fubarable
Quote:

Originally Posted by pellebye
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?
• 06-10-2009, 07:01 PM
pellebye
Ok I understand. Thank you!
• 06-10-2009, 09:59 PM
Singing Boyo
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.
• 06-11-2009, 10:20 AM
pellebye
"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?