# Thread: How to write numbers around a circle

1. Member
Join Date
Apr 2008
Posts
42
Rep Power
0

## 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.
/////////////////////////////
Java 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 12:28 PM. Reason: added code tags

2. 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!

3. 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,
Java 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:

Java 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.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 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;

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

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

@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 03:16 PM.

4. Member
Join Date
Apr 2008
Posts
42
Rep Power
0
Thank you my friend I really appreciate this help

Regards

5. Member
Join Date
Apr 2009
Posts
54
Rep Power
0
I find this very interesting, too. What do you mean with "magic numbers"?

6. Originally Posted by pellebye
I find this very interesting, too. What do you mean with "magic numbers"?
Note the difference in readability between this:
Java 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):
Java Code:
```      int xCor = (int) (CENTER_X + (OUTER_RADIUS * Math.cos(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?

7. Member
Join Date
Apr 2009
Posts
54
Rep Power
0
Ok I understand. Thank you!

8. Senior Member
Join Date
Mar 2009
Posts
552
Rep Power
8
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.

9. Member
Join Date
Apr 2009
Posts
54
Rep Power
0
"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?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•