-2

该程序可以运行,但它没有输出我想要它输出的内容。看,我想要 7 个同心圆弧,颜色像彩虹。我得到的是红色和黄色的 7 个同心圆弧。我需要这个顺序的颜色:

红色, 黄色, 粉色, 绿色, 紫色, 橙色, 蓝色

我需要添加或更改什么?我正在使用 if else 语句在颜色之间交替。这就是为什么它在红色和黄色之间交替出现的原因。我在另一个 if else 语句中添加了另一组 if else,我想我可以使用它们来进一步交替。

我对java和循环很陌生。我在想我可以使用 for、while、if else 或为颜色循环。

import javax.swing.JPanel;
import java.awt.*;
public class rainbowpanel extends JPanel
{

private final int MAX_WIDTH = 300, NUM_RINGS = 7, RING_WIDTH = 20;



public rainbowpanel ()
{
setBackground (Color.white);
setPreferredSize (new Dimension(300,300));
}




    public void paintComponent (Graphics page)

    {
        super.paintComponent (page);
        int x = 0, y = 0, diameter = MAX_WIDTH;
        page.setColor (Color.yellow);
     for (int count = 0; count < NUM_RINGS; count++)


        {
            if (page.getColor() == Color.yellow) // alternate colors
            page.setColor (Color.red);
            else
            page.setColor (Color.yellow);
            page.fillArc (x, y, diameter, diameter, 0, 180);




            diameter -= (2 * RING_WIDTH);
            x += RING_WIDTH;
            y += RING_WIDTH;
            }



            {
            if (page.getColor() == Color.yellow) // alternate colors
                page.setColor (Color.pink);
            else
                page.setColor (Color.orange);
                   page.fillArc (x, y, diameter, diameter, 0, 180);



            //diameter -= (2 * RING_WIDTH);
            //x += RING_WIDTH;
            //y += RING_WIDTH;
}




            page.setColor (Color.white);
            page.fillArc (x, y, diameter, diameter, 0, 180);
}
}

你必须从这个文件运行它:

import javax.swing.JFrame;
public class rainbow

{

public static void main (String[] args)
{
JFrame frame = new JFrame ("Rainbow");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);


rainbowpanel panel = new rainbowpanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
4

2 回答 2

1

尝试这样的事情:

public void paintComponent (Graphics page)
{
    super.paintComponent (page);
    int x = 0, y = 0, diameter = MAX_WIDTH;

    Color myColors[] = { Color.red, Color.yellow, Color.pink, Color.green, Color.purple, Color.orange, Color.blue };

    for (int count = 0; count < NUM_RINGS; count++)
    {
        page.setColor (myColors[count%NUM_RINGS]);
        page.fillArc (x, y, diameter, diameter, 0, 180);

        diameter -= (2 * RING_WIDTH);
        x += RING_WIDTH;
        y += RING_WIDTH;
    }
}

您应该只需要一个循环即可完成所有绘图。如果您设置的count%NUM_RINGS环数多于颜色数,则将防止数组越界错误,它们将从头开始循环。

于 2013-10-31T12:08:16.713 回答
0

请尝试此代码。这段代码有点棘手。我知道这不是专业的编码方式。

public class rainbowpanel extends JPanel{
private final int MAX_WIDTH = 300, NUM_RINGS = 7, RING_WIDTH = 20;

public rainbowpanel ()
{
    setBackground (Color.white);
    setPreferredSize (new Dimension(300,300));
    }
public void paintComponent (Graphics page)
{
super.paintComponent (page);
int x = 0, y = 0, diameter = MAX_WIDTH;
page.setColor (Color.red);
for (int count = 0; count < 2; count++)
{
if (page.getColor() == Color.yellow) // alternate colors
page.setColor (Color.red);
else
page.setColor (Color.yellow);
page.fillArc (x, y, diameter, diameter, 0, 180);

diameter -= (2 * RING_WIDTH);
x += RING_WIDTH;
y += RING_WIDTH;
}
for (int count = 0; count < 2; count++)
{
if (page.getColor() == Color.blue) // alternate colors
page.setColor (Color.green);
else
page.setColor (Color.blue);
page.fillArc (x, y, diameter, diameter, 0, 180);

diameter -= (2 * RING_WIDTH);
x += RING_WIDTH;
y += RING_WIDTH;
}
for (int count = 0; count < 2; count++)
{
if (page.getColor() == Color.pink) // alternate colors
page.setColor (Color.LIGHT_GRAY);
else
page.setColor (Color.pink);
page.fillArc (x, y, diameter, diameter, 0, 180);

diameter -= (2 * RING_WIDTH);
x += RING_WIDTH;
y += RING_WIDTH;
}
for (int count = 0; count < 1; count++)
{
if (page.getColor() == Color.cyan) // alternate colors
page.setColor (Color.LIGHT_GRAY);
else
page.setColor (Color.cyan);
page.fillArc (x, y, diameter, diameter, 0, 180);

diameter -= (2 * RING_WIDTH);
x += RING_WIDTH;
y += RING_WIDTH;
}

}
} 
于 2013-10-31T12:54:49.043 回答