0

I have looked around the site and did not find the answer to my question. So any help is appreciated.

I want to use drawString to write individual text strings that each end at the same distance from a point that is the center of a circle. The individual strings are offset from each other by an angle.

The following code snippet does draw the individual strings at a certain distance - 120 points - from the center of the circle:


 public void paintComponent(Graphics g) {
    
Graphics2D g2 =(Graphics2D) g;

         
    g2.setColor(Color.black);
    Font font = new Font(null, Font.PLAIN, 14);                      


    AffineTransform affineTransform1 = new AffineTransform();
    affineTransform1.rotate(Math.toRadians(-75), 0, 0);
    affineTransform1.translate(-120, 0); 
    Font rotatedFont1 = font.deriveFont(affineTransform1); 
    g2.setFont(rotatedFont1);
    g2.drawString("text here", 400, 400); 


    AffineTransform affineTransform2 = new AffineTransform();
    affineTransform2.rotate(Math.toRadians(-60), 0, 0);
    affineTransform2.translate(-120, 0); 
    Font rotatedFont2 = font.deriveFont(affineTransform2); 
    g2.setFont(rotatedFont2);
    g2.drawString("test", 400, 400); 
        
        
    AffineTransform affineTransform3 = new AffineTransform();
    affineTransform3.rotate(Math.toRadians(-45), 0, 0);
    affineTransform3.translate(-120, 0); 
    Font rotatedFont3 = font.deriveFont(affineTransform3); 
    g2.setFont(rotatedFont3);
    g2.drawString("About this", 400, 400); 

        
        
    g2.dispose();

120 points from the center of the circle at 400, 400 is where each string begins. I want to draw each string so that it ends at a certain distance from the center of the circle, say 75 points.

It would be kind of a right justification for each string.

Is there a way with .drawString to do this?

  • 1
    You use the `FontMetrics` of the `Graphics` class to calculate the width of the String. Once you know the width you can adjust the "x" value to paint the text based on your alignment requirements. Also, you should NOT apply transforms on the Graphics object passed to the paintComponent() method. Instead use: `Graphics2D g2 = (Graphics2D)g.create()` to get a Graphics2D object; – camickr Mar 13 '21 at 20:11
  • Thanks for the tip. I will check out these options. – BostonKevin Mar 13 '21 at 23:04
  • Thanks a bunch! Using FontMetrics I was able to get the string width and then add it to the radius. The output looks great. – BostonKevin Apr 03 '21 at 17:09
  • Well, i spoke too soon. Would you know of any other technique to do this? I have used FontMetrics with mixed results. I have used FontMetrics.getStringBounds and FontMetrics,.charsWidth to determine the width of the string to be painted. (In the latter case, I did, of course, change the string to a char array before getting the width.) Using either technique, the distance from the center to the end of the string varied somewhat. Any ideas would be appreciated. Thanks! – BostonKevin May 13 '21 at 09:55
  • *It would be kind of a right justification for each string.* - not understanding the problem. If the width of the panel is 200 you calculate the width of the string and subtract that value from 200. Then you invoke the drawString(...) method using the starting location. Post a proper [mre] demonstrating the problem. We should bed able to copy/paste/compile and test any code you post. – camickr May 13 '21 at 14:43
  • The _variations_ that I mentioned earlier are, at second glance, trivial. Thanks for your help. They soltion that you proposed and that I implemented work well enough for my purposes. Thanks again! – BostonKevin May 16 '21 at 18:52

0 Answers0