Java 如何更改for循环中的设置颜色?
我的圆圈会改变颜色,但最小的圆圈始终保持红色,而最大的圆圈则不保持蓝色。每当我尝试更改代码时,所有的圆都会变成相同的颜色,移动滑块时只需更改阴影即可。我想有更多的阴影之间的红色和蓝色滑块增加。你能解释一下我需要做的改动吗?我了解我所做的一切,除了变色部分Java 如何更改for循环中的设置颜色?,java,colors,draw,fill,Java,Colors,Draw,Fill,我的圆圈会改变颜色,但最小的圆圈始终保持红色,而最大的圆圈则不保持蓝色。每当我尝试更改代码时,所有的圆都会变成相同的颜色,移动滑块时只需更改阴影即可。我想有更多的阴影之间的红色和蓝色滑块增加。你能解释一下我需要做的改动吗?我了解我所做的一切,除了变色部分 public class SliderLab1 extends JFrame { JSlider slider; GPanel graphicsPanel; public SliderLab1() { super("Slider/Shap
public class SliderLab1 extends JFrame {
JSlider slider;
GPanel graphicsPanel;
public SliderLab1() {
super("Slider/Shapes Demo");
setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
JPanel outerPanel = new JPanel();
graphicsPanel = new GPanel(); //for shapes
JPanel sliderPanel = new JPanel();
setup(sliderPanel);
outerPanel.add(graphicsPanel);
outerPanel.add(sliderPanel);
add(outerPanel);
pack();
setVisible(true);
}
private void setup(JPanel p) {
slider = new JSlider(
JSlider.VERTICAL,4,20,8);
slider.setMinorTickSpacing(1);
slider.setMajorTickSpacing(4);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.addChangeListener( new SLstn() );
p.add(slider);
}
private class SLstn implements ChangeListener {
public void stateChanged(ChangeEvent e) {
int x = slider.getValue();
System.out.println(x);
graphicsPanel.setCount(x);
graphicsPanel.repaint();
}
}
private class GPanel extends JPanel {
int count = 8;
int size = 400;
int stepSize = (int)size/count;
public GPanel() {
setPreferredSize(
new Dimension(size,size));
setBackground(Color.BLACK);
}
public void setCount(int n) {
count=n;
stepSize = (int)size/count;
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(3) );
for(int i = count; i>=0; i--){
Color myC = new Color(((255/stepSize)*(stepSize-i)), 0, (255/stepSize)*i );
g.setColor(myC);
for (int j = 0; j <= count; j++) {
g.drawOval(0,(size-(stepSize*i)),stepSize*i,stepSize*i);
g.fillOval(0,(size-(stepSize*i)),stepSize*i,stepSize*i);
}
}
}
}
public static void main(String[] args) {
new SliderLab1();
}
}
public类SliderLab1扩展了JFrame{
滑动滑块;
GPanel图形板;
公共幻灯片AB1(){
超级(“滑块/形状演示”);
setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
JPanel outerPanel=新的JPanel();
graphicsPanel=new GPanel();//用于形状
JPanel sliderPanel=新的JPanel();
设置(滑动面板);
外部面板添加(图形面板);
添加(滑动面板);
添加(外部面板);
包装();
setVisible(真);
}
专用无效设置(JPanel p){
滑块=新的JSlider(
垂直滑动,4,20,8);
滑块。setMinorTickSpacing(1);
滑块。设置MajortickSpacing(4);
slider.setPaintTicks(真);
slider.setPaintLabels(true);
slider.addChangeListener(新的SLstn());
p、 添加(滑块);
}
私有类SLstn实现ChangeListener{
公共无效状态已更改(更改事件e){
int x=slider.getValue();
系统输出println(x);
图形设置计数(x);
graphicsPanel.repaint();
}
}
私有类GPanel扩展了JPanel{
整数计数=8;
int size=400;
int步长=(int)大小/计数;
公共GPanel(){
设置首选大小(
新尺寸(尺寸、尺寸);
挫折背景(颜色:黑色);
}
公共无效集合计数(整数n){
计数=n;
步长=(int)大小/计数;
}
公共空间涂料(图g){
超级油漆(g);
Graphics2D g2d=(Graphics2D)g;
g2d.设定行程(新基本行程(3));
对于(int i=count;i>=0;i--){
颜色myC=新颜色((255/步长)*(步长-i)),0,(255/步长)*i);
g、 setColor(myC);
对于(int j=0;j只需替换:
Color myC = new Color(((255/stepSize)*(stepSize-i)), 0, (255/stepSize)*i );
作者:
此时您不想使用步长
,使用计数
应该可以做到这一点:)
我希望有帮助
final Color myC = new Color(((255 / count) * (count - i)), 0, (255 / count) * i);