JSlider — Advice needed

后端 未结 2 988
梦谈多话
梦谈多话 2021-01-21 20:41

I have a problem with JSlider in Java I have drawn a circle A, and I want to put ANOTHER circle B inside the first circle A. I want to place the CENTRE of the second circle B at

相关标签:
2条回答
  • 2021-01-21 20:59

    Theory

    From Graphics.drawOval() javadoc. When you draw a circle their (x,y) coordinates are not its center but its top-left corner. In order to make your B circle aligned with A circle, you need to calculate its (x,y) coordinates relatives to the last one:

    Circle A: (x,y) = (20,20); diameter = 100 ==> radius = 50; center = (x + radius, y + radius) = (70,70)
    

    Well, you have your center now: (70,70). Now, slider1Value is your new radius so you need to calculate circle B (x,y) coordinates:

    Circle B: center = (70,70); (x,y) = (centerX - radius, centerY - radius) = (70 - slider1Value, 70 - slider1Value)
    

    Finally, circle B width and height are equals to its diameter:radius * 2 = slider1Value * 2.

    Facts

    Make this change and it will work like a charm:

    paper.drawOval(70 - slider1Value, 70 - slider1Value, 2*slider1Value, 2*slider1Value);
    
    0 讨论(0)
  • 2021-01-21 21:06

    Because you have to change position of top-left "corner" of circle. If you change radius, the circle is bigger/smaller so it's obvious if you don't change position of top-left cornet, centers of 2 circles won't be aligned

    0 讨论(0)
提交回复
热议问题