我会对此坦诚相待;这是一项家庭作业,但是有人能指导我正确的方向,并向我解释代码的某些部分应该如何工作?方向在代码和问题之下。在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中的空白。
开始彩虹项目。
在文件顶部的类声明之前添加一个包含您的名字的完整注释标题。
添加到彩虹类Color类型的私人最终场skyColor,初始化为Color.CYAN(天空的颜色)的声明。在Rainbow的构造函数中,将窗口的背景设置为skyColor而不是Color.WHITE。
在涂料方法中,声明局部整数变量x中心和y中心表示所述环的中心的坐标。将它们分别初始化为内容窗格的1/2宽度和3/4高度(向下)。 (回想一下,Java中图形坐标的原点位于内容窗格的左上角,y轴向下。)不要插入窗口尺寸的固定数字。
声明局部变量largeRadius表示最大(红色)半圆的半径并将其初始化宽度的1/4。
一个方法调用g.fillArc(X,Y,尺寸,大小,从,度)(与所有整数参数)绘制一个圆的扇区。 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); } }
的颜色在你的屏幕截图的对比让我的眼睛受伤... –
你对某件你不明白的事情有疑问吗? –