问题描述
Java 中的 JSlider 有问题我画了一个圆 A,我想把另一个圆 B 放在第一个圆 A 里面.我想把第二个圆 B 的 CENTER 放在与中心相同的坐标上第一个圆A的,然后我想用JSlider来增加或减少圆B的半径. 问题是,当你增加或减少滑块时,圆B的中心不会与A的中心对齐.基本上,我想要两个中心相同的圆圈.有人可以指出我的错误吗?
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 the same coordinate with the centre of the first circle A, and then I want to use JSlider to INCREASE or DECREASE the radius of circle B. The trouble is, when you increase or decrease the slider, the CENTRE of circle B does not stay aligned with the centre of A. Basically, I want two circles with the SAME centre. Can someone point out my mistake, please?
slider1 = new JSlider(JSlider.HORIZONTAL,10,100,10);
window.add(slider1);
slider1.addChangeListener(this);
Graphics paper = panel.getGraphics();
int slider1Value = slider1.getValue();
paper.setColor(Color.white);
paper.fillRect(0, 0, 500, 500);
paper.setColor(Color.pink);
paper.fillOval(20,20,100,100); // this is circle A
paper.drawOval(60,60,slider1Value,slider1Value); // this is circle B slider
推荐答案
因为你必须改变圆圈左上角的位置.如果你改变半径,圆圈会更大/更小,所以很明显,如果你不改变左上角的位置,两个圆圈的中心不会对齐
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
这篇关于JSlider -- 需要建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!