Results 1 to 9 of 9
  1. #1
    pheonix is offline Member
    Join Date
    Apr 2008
    Posts
    42
    Rep Power
    0

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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    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!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    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,
    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.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.

  4. #4
    pheonix is offline Member
    Join Date
    Apr 2008
    Posts
    42
    Rep Power
    0

    Default

    Thank you my friend I really appreciate this help

    Regards

  5. #5
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote 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:
    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)));
          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?

  7. #7
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    Ok I understand. Thank you!
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  8. #8
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    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!

  9. #9
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    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.

Similar Threads

  1. Replies: 5
    Last Post: 06-07-2014, 06:39 PM
  2. Generate numbers around a circle?
    By pheonix in forum New To Java
    Replies: 4
    Last Post: 06-05-2009, 06:08 PM
  3. java program help. find the radius of a circle
    By megironi in forum New To Java
    Replies: 1
    Last Post: 03-27-2009, 08:09 AM
  4. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-15-2008, 12:46 AM
  5. In which circle is the Point lying?
    By nidhirastogi in forum New To Java
    Replies: 1
    Last Post: 07-03-2008, 12:12 AM

Posting Permissions

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