我会坦率的这是一项家庭作业,但是有人可以指导我正确的方向并向我解释代码的某些部分应该如何工作吗?说明在代码和问题下方。
到目前为止,这是我的代码:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Rainbow extends JPanel
{
// Declare skyColor:
private final Color skyColor = Color.CYAN;
public Rainbow()
{
setBackground(skyColor);
}
// Draws the rainbow.
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
// Declare and initialize local int variables xCenter, yCenter
// that represent the center of the rainbow rings:
int xCenter = width/2;
int yCenter = (height * 3) /4;
// Declare and initialize the radius of the large semicircle:
int largeRadius = width/4;
g.setColor(Color.RED);
// Draw the large semicircle:
g.fillArc(xCenter,yCenter,largeRadius,height,0,180);
// Declare and initialize the radii of the small and medium
// semicircles and draw them:
int smallRadius = height/4;
g.setColor(Color.MAGENTA);
g.fillArc(xCenter,yCenter,width,height,0,180);
int mediumRadius = (int) Math.sqrt(smallRadius * largeRadius);
g.setColor(Color.GREEN);
g.fillArc(xCenter,yCenter,width,height,0,180);
// Calculate the radius of the innermost (sky-color) semicircle
// so that the width of the middle (green) ring is the
// arithmetic mean of the widths of the red and magenta rings:
// Draw the sky-color semicircle:
g.fillArc(xCenter,yCenter,width,height,0,180);
}
public static void main(String[] args)
{
JFrame w = new JFrame("Rainbow");
w.setBounds(300, 300, 300, 200);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = w.getContentPane();
c.add(new Rainbow());
w.setVisible(true);
}
}
我的问题:fillArc到底如何工作?我了解参数中包含的内容,但是每个弧之间必须做些什么呢?
如何为每个弧线设置一种颜色?我尝试这样做,最后得到的颜色最接近显示的末尾并覆盖了其他颜色。
我可能会继续编写代码。
这些是方向:
![在此处输入图片描述] [1]
“彩虹”由四个重叠的半圆组成。外圈为红色(Color.RED),中间圈为绿色(Color.GREEN),内圈为洋红色(Color.MAGENTA)。最里面的半圆与背景颜色相同。
请遵循以下说明,并在Rainbow.java中填写空白。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Rainbow extends JPanel
{
//Declare skyColor:
private final Color skyColor = Color.CYAN;
public Rainbow()
{
setBackground(skyColor);
}
// Draws the rainbow.
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
// Declare and initialize local int variables xCenter, yCenter
// that represent the center of the rainbow rings:
int xCenter = width/2;
int yCenter = (height * 3) /4;
// Declare and initialize the radius of the large semicircle:
int largeRadius = width/4;
g.setColor(Color.RED);
// Draw the large semicircle:
g.fillArc(xCenter - largeRadius,yCenter - largeRadius ,largeRadius,largeRadius,0,180);
// Declare and initialize the radii of the small and medium
//semicircles and draw them:
int smallRadius = height/4;
int mediumRadius = (int) Math.sqrt(smallRadius * largeRadius);
g.setColor(Color.GREEN);
g.fillArc(xCenter-(largeRadius+mediumRadius)/2,yCenter- (largeRadius+mediumRadius)/2,mediumRadius,mediumRadius,0,180);
g.setColor(Color.MAGENTA);
g.fillArc(xCenter-(largeRadius+smallRadius)/2,yCenter-(largeRadius+smallRadius)/2,smallRadius,smallRadius,0,180);
// Calculate the radius of the innermost (sky-color) semicircle
// so that the width of the middle (green) ring is the
// arithmetic mean of the widths of the red and magenta rings:
int skyRadius = (int)((2 * Math.sqrt(smallRadius * largeRadius)) - width/4);
// Draw the sky-color semicircle:
g.setColor(skyColor);
g.fillArc(xCenter-skyRadius,yCenter-skyRadius,skyRadius,skyRadius,0,180);
}
public static void main(String[] args)
{
JFrame w = new JFrame("Rainbow");
w.setBounds(300, 300, 300, 200);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = w.getContentPane();
c.add(new Rainbow());
w.setVisible(true);
}
}
最佳答案
fillArc()根据您给它的参数填充圆的一部分。例如您的第一条弧线。
您正在绘制红色的填充圆弧,在本例中为半圆。
//Set the arc color
g.setColor(Color.RED);
// Draw the large semicircle:
g.fillArc(xCenter,yCenter,largeRadius,height,0,180);
这是我们的fillArc。那看起来不像彩虹。为了获得彩虹形状,我们必须在其内部绘制一个较小的弧。在您的情况下,下一个是绿色。因此,在将颜色设置为绿色之后,我们再次执行fillArc。但是我们将半径缩小了一点,所以绿色不会覆盖整个红色部分。
在绘制时请记住,我们在顶部绘制,因此如果您先绘制绿色,则红色会覆盖它。
然后,我们再次在其中绘制另一条弧,但使该弧成为天空的颜色(在这种情况下为白色)。这将创建最终的彩虹形状。因此,我们再次执行fillArc,但半径稍小,颜色为白色。
在那儿,我们画了一条彩虹。
为了使这个漂亮的创意居中,我们必须了解关于fillArc函数的一些知识。
参数为:
public abstract void fillArc(int x,
int y,
int width,
int height,
int startAngle,
int arcAngle)
int x和int y表示要绘制的弧的左上角的坐标。您的代码未居中的原因在于绘制弧线的方式。
g.fillArc(xCenter - largeRadius,yCenter - largeRadius,largeRadius,largeRadius,0,180);
g.fillArc(xCenter-(largeRadius+mediumRadius)/2,yCenter-(largeRadius+mediumRadius)/2,mediumRadius,mediumRadius,0,180);
g.fillArc(xCenter-(largeRadius+smallRadius)/2,yCenter-(largeRadius+smallRadius)/2,smallRadius,smallRadius,0,180);
我拿出一些多余的东西。您看到如何减去(largeRadius + smallRadius)/ 2和(largeRadius + mediumRadius)/ 2吗?这正在转移彩虹以使其偏离中心。您应该拥有的是:
g.fillArc(xCenter - largeRadius/2,yCenter - largeRadius,largeRadius,largeRadius,0,180);
g.fillArc(xCenter-(mediumRadius)/2,yCenter-(largeRadius+mediumRadius)/2,mediumRadius,mediumRadius,0,180);
g.fillArc(xCenter-(smallRadius)/2,yCenter-(largeRadius+smallRadius)/2,smallRadius,smallRadius,0,180);
这将使彩虹正确居中。这就是为什么。
这就是他们将从其开始绘制弧的点。如果要使整个彩虹居中,请将其移动整个宽度的一半。因此,如果您想将红色弧线居中,则可以
xCenter - (largeRadius/2)
因为这将x设置为开始左移一半。您不会在其他弧线中包含largeRadius,因为要将它们围绕此点居中。因此,您需要将它们移动一半单独宽度,这就是为什么它们的x位置是
xCenter-(mediumRadius)/2
xCenter-(smallRadius)/2
以Y轴为中心的工作原理有所不同。您必须考虑整个彩虹的高度是largeRadius的1/4。您的代码使用yCenter = 3/4 * height,因此会对其进行一些更改。
这是我的解决方案
g.fillArc(xCenter - largeRadius/2,yCenter - largeRadius/2 + largeRadius/4 -height/4,largeRadius,largeRadius,0,180);
g.fillArc(xCenter-(mediumRadius)/2,yCenter-(mediumRadius)/2 + largeRadius/4 -height/4,mediumRadius,mediumRadius,0,180);
g.fillArc(xCenter-(smallRadius)/2,yCenter-(smallRadius)/2 + largeRadius/4 -height/4,smallRadius,smallRadius,0,180);
让我们来看看。我减去了与x中相同原理的largeRadius / 2(以及相应的半径)。但是后来我添加了largeRadius / 4,因为我们必须向下移动整个彩虹。这是因为减去相应的半径/ 2只会使彩虹居中,就好像它是整个圆一样,而不是半圆。
添加largeRadius / 4可使彩虹向下移动其总高度的一半,将其正确居中半圈。最后,减去height / 4将yCenter更改为height / 2,因为3/4 * height是分配中的要求。
很抱歉评论中的所有问题,希望此问题可以解决。