我会坦率的这是一项家庭作业,但是有人可以指导我正确的方向并向我解释代码的某些部分应该如何工作吗?说明在代码和问题下方。

到目前为止,这是我的代码:

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中填写空白。
  • 启动Rainbow项目。
  • 在文件顶部的类声明之前,添加一个带有您的姓名的完整注释标头。
  • 向Rainbow类添加一个声明为Color类型的私有最终字段skyColor的声明,该声明初始化为Color.CYAN(天空的颜色)。在Rainbow的构造函数中,将窗口的背景设置为skyColor而不是Color.WHITE。
  • 在paint方法中,声明表示环中心坐标的局部整数变量xCenter和yCenter。将它们分别初始化为内容窗格的1/2宽度和3/4高度(向下)。 (请记住,Java中图形坐标的原点位于内容窗格的左上角,y轴指向下方。)请勿插入窗口尺寸中的固定数字。
  • 声明一个局部变量largeRadius,该变量表示最大(红色)半圆的半径,并将其初始化为宽度的1/4。
  • 方法调用g.fillArc(x,y,size,size,from,degree)(带有所有整数参数)绘制一个圆的扇区。 x和y是(逻辑上)内接椭圆的矩形(在本例中为正方形)的左上角的坐标;大小是正方形的一面(以及圆的直径); from是弧的起点,以度为单位(水平直径的最东点为0),度(正数)是弧的量度,逆时针旋转。在paint方法中添加一条语句以绘制最大的(红色)半圆。测试您的程序。
  • 添加语句以显示中(绿色)和小(品红色)半圆。品红色半圆的半径应为高度的1/4。绿色圆的半径应为红色半圆的半径和品红色半圆的半径的几何平均值(乘积的平方根),四舍五入到最接近的整数。 (对Math.sqrt(x)的调用返回x的平方根的值,即double的值。)重新测试程序。
  • 添加语句以显示背景(“天空”)颜色的最里面的半圆以完成彩虹。使用skyColor常数表示此半圆的颜色。选择天空半圆的半径,以使中间(绿色)环的宽度是红色和品红色环的宽度的算术平均值。
  • 测试您的程序。
  • 提交完成的程序并运行输出。可以通过捕获屏幕输出(Alt-PrintScrn),将其粘贴到图形程序(例如MS Paint)中,然后将图像保存到Eclipse项目目录中,来包含运行输出(彩虹图片)。
    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是分配中的要求。

    很抱歉评论中的所有问题,希望此问题可以解决。

    09-28 12:45