2012-10-04 34 views
2

我会对此坦诚相待;这是一项家庭作业,但是有人能指导我正确的方向,并向我解释代码的某些部分应该如何工作?方向在代码和问题之下。在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,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中的空白。

  1. 开始彩虹项目。

  2. 在文件顶部的类声明之前添加一个包含您的名字的完整注释标题。

  3. 添加到彩虹类Color类型的私人最终场skyColor,初始化为Color.CYAN(天空的颜色)的声明。在Rainbow的构造函数中,将窗口的背景设置为skyColor而不是Color.WHITE。

  4. 在涂料方法中,声明局部整数变量x中心和y中心表示所述环的中心的坐标。将它们分别初始化为内容窗格的1/2宽度和3/4高度(向下)。 (回想一下,Java中图形坐标的原点位于内容窗格的左上角,y轴向下。)不要插入窗口尺寸的固定数字。

  5. 声明局部变量largeRadius表示最大(红色)半圆的半径并将其初始化宽度的1/4。

  6. 一个方法调用g.fillArc(X,Y,尺寸,大小,从,度)(与所有整数参数)绘制一个圆的扇区。 x和y是矩形(在这种情况下是一个正方形)的左上角的坐标,椭圆在其中(逻辑上)刻入其中;大小是正方形的边(以及圆的直径); from是以度数表示的弧的起始点(在水平直径的最东侧点处为0),度数(正数)是逆时针方向的弧的度量。向paint方法添加语句以绘制最大(红色)半圆。测试你的程序。

  7. 添加报表显示介质(绿色)和小(品红)半圆。洋红色半圆的半径应为高度的1/4。绿色半径的半径应该是红色半圆半径和品红色半圆半径的几何平均值(产品的平方根),四舍五入为最接近的整数。 (调用Math.sqrt(x)返回x的平方根值,double。)重新测试您的程序。

  8. 添加报表显示背景的最里面半圆(“天空”)的颜色来完成的彩虹。对这个半圆的颜色使用skyColor常量。选择天空半圆的半径,使中间(绿色)环的宽度是红色和洋红色环的宽度的算术平均值。

  9. 测试你的程序。

  10. 提交完成的程序并运行输出。通过捕获屏幕输出(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); 
    } 
    } 
    
+0

的颜色在你的屏幕截图的对比让我的眼睛受伤... –

+0

你对某件你不明白的事情有疑问吗? –

回答

7

fillArc()填写基于你给它的参数圆的部分。例如你的第一个弧。

您正在绘制填充的弧线,在这种情况下是一个半圆红色的。

//Set the arc color 
g.setColor(Color.RED); 

// Draw the large semicircle: 
g.fillArc(xCenter,yCenter,largeRadius,height,0,180); 

circle

有我们fillArc。那看起来不像彩虹。为了得到彩虹的形状,我们必须在它内部画一个更小的弧。在你的情况下,下一个是绿色的。所以我们在将颜色设置为绿色后再次填充颜色。但是我们缩小了半径,所以绿色并没有覆盖整个红色部分。 enter image description here

请记住,当我们绘制,我们正在顶部,所以如果你先画绿色,它会被红色覆盖。

然后我们再次在里面画另一条弧线,但是让它成为天空的颜色(在这种情况下为白色)。这创建了最终的彩虹形状。所以我们再次填充弧度,但半径稍小,颜色为白色。

rainbow!

在那里,我们画了一道彩虹。

要居中这个美丽的创造,我们要了解有关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); 

这将正确地居中彩虹。这是为什么。

enter image description here

这就是他们将开始从画弧的点。如果你想把整个彩虹居中,你会把它移过整个宽度的一半。所以,如果你想中心,红色的弧线,你会做

xCenter - (largeRadius/2) 

由于这是设置x开始向左一半。您不会在其他弧中包含largeRadius,因为您正在围绕这一点对它们进行集中。因此,你会希望他们在通过其各自的宽度的一半转移,这就是为什么它们的x位置

xCenter-(mediumRadius)/2 
xCenter-(smallRadius)/2 

紧紧围绕Y轴的工作方式不同。你必须考虑彩虹的整体高度是大半径的1/4。你的代码使用yCenter = 3/4 *高度,所以改变一下。

这是我的解决方案

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将彩虹向下移动整个一半的高度,将其正确对准半圆。最后,减去高度/ 4会使yCenter更改为高度/ 2,因为3/4 *高度是您作业中的要求。

对不起,在评论中的所有问题,希望这清除了它。

+1

你一定是色盲! –

+1

哦,上帝,我认为你是对的D: – Clark

+0

xCenter和yCenter的目的是为了找到绘图开始的屏幕上的点吗? – user1629075

2

我修改了一部分代码,以便您可以获得一个想法。请记住,您的xCenter和yCenter代表您的圈子的中心,而不是您在fillArc方法中需要使用的坐标。您提供的说明很好地解释了它。你可以从我在这里做的事情中得到一个想法,并由你自己决定。

// First declare and initialize all radiuses 
    int largeRadius = width/4; 
    int smallRadius = height/4; 
    int mediumRadius = (int) Math.sqrt(smallRadius * largeRadius); 

//Then draw each arc in descending order from the largest one 

g.setColor(Color.RED); 

g.fillArc(xCenter-largeRadius,yCenter-largeRadius,largeRadius,largeRadius,0,180); 

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 

为您skyRadius考虑:

  1. 红色宽度=大半径 - 中等半径
  2. 绿色宽度=中 - 小
  3. 品红宽度=小半径 - skyradius

如果我没有数学rightyou得到:skyRadius = smallRadius - 2 *(mediumRadius-smallRadius)+ largeRadius-mediumRadius

int skRadius=smallRadius-2*(mediumRadius-smallRadius)+largeRadius-mediumRadius; 
g.setColor(skyColor); 
g.fillArc(xCenter-(largeRadius+skRadius)/2,yCenter-(largeRadius+skRadius)/2,skRadius,skRadius,0,180); 
+0

感谢您的帮助。 – user1629075

-1

skyRadius一个更简单的公式:

int skyRadius = largeRadius - 3 * mediumRadius + 3 * smallRadius;