程序可以运行,但它没有输出我想要输出的内容。看,我想要7个同心圆弧,彩色为彩虹。我得到的是7个同心圆弧,红色和黄色。我按此顺序需要颜色:
红色,黄色,粉色,绿色,紫色,橙色,蓝色
我需要添加或更改什么?我正在使用if else语句在颜色之间交替。这就是它在红色和黄色之间交替的原因。我在其他if语句中添加了另一组if else,我想我可以用它们来进一步替换。
我对java和循环很新。我以为我可以使用,如果是,或者为颜色做循环。
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);
}
}
答案 0 :(得分: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
将阻止数组超出界限错误,如果您设置的环数多于颜色,则它们将从头开始循环。
答案 1 :(得分: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;
}
}
}