use*_*075 2 java swing draw background-color
我会坦诚相待; 这是一项家庭作业,但有人可以指导我正确的方向,并向我解释代码的某些部分应该如何工作?方向低于代码和问题.
到目前为止这是我的代码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题:fillArc究竟是如何工作的; 我理解参数中的内容,但是每个弧必须做什么才能彼此不同?如何为每个弧设置一种颜色?我试过这样做,最后我最终列出的颜色最接近显示并覆盖其他颜色.我可能还有更多,因为我继续编码.
这些是方向:
![在此处输入图像说明] [1]
"彩虹"由四个重叠的半圆组成.外圈为红色(Color.RED),中间为绿色(Color.GREEN),内圈为品红色(Color.MAGENTA).最里面的半圆与背景颜色相同.
按照以下说明填写Rainbow.java中的空白.
启动Rainbow项目.
在文件顶部的类声明之前添加一个带有您姓名的完整注释标题.
向Rainbow类添加一个Color类型的私有final字段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,degrees)(带有所有整数参数)绘制圆的扇区.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);
}
}
Run Code Online (Sandbox Code Playgroud)fillArc()根据您给出的参数填充圆的一部分.例如你的第一个弧.
您正在绘制填充弧,在这种情况下是半圆形,颜色为红色.
//Set the arc color
g.setColor(Color.RED);
// Draw the large semicircle:
g.fillArc(xCenter,yCenter,largeRadius,height,0,180);
Run Code Online (Sandbox Code Playgroud)

这是我们的fillArc.这看起来不像彩虹.为了获得彩虹形状,我们必须在其内部绘制一个较小的弧.在你的情况下,下一个是绿色.因此我们在将颜色设置为绿色后再次执行fillArc.但是我们缩小了半径,所以绿色不会覆盖整个红色部分.
请记住,当我们画画时,我们画在上面,所以如果你先画绿色,它将被红色覆盖.
然后我们再次在这里画出另一个弧,但是让它成为天空的颜色(在这种情况下为白色).这创造了最终的彩虹形状.所以我们再次尝试fillArc,但半径略小,颜色为白色.

在那里,我们画了一条彩虹.
为了集中这个美丽的创作,我们必须了解fillArc函数的一些内容.
参数是:
public abstract void fillArc(int x,
int y,
int width,
int height,
int startAngle,
int arcAngle)
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
我拿出了一些多余的东西.你看到你如何减去(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);
Run Code Online (Sandbox Code Playgroud)
这将使彩虹正确居中.这就是原因.

这就是他们开始绘制弧线的点.如果你想让整个彩虹居中,你可以将它的整个宽度移动一半.所以,如果你想让红色圆弧居中,那你就做了
xCenter - (largeRadius/2)
Run Code Online (Sandbox Code Playgroud)
因为这是将x开始向左设置一半.你不会在其他弧中包含largeRadius,因为你将它们集中在这一点上.因此,您希望将它们移动一半的宽度,这就是它们x位置的原因
xCenter-(mediumRadius)/2
xCenter-(smallRadius)/2
Run Code Online (Sandbox Code Playgroud)
以Y轴为中心的工作方式不同.你必须考虑到整体彩虹的高度是largeRadius的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);
Run Code Online (Sandbox Code Playgroud)
让我们来看看.我减去了largeRadius/2(以及相应的半径),原理与x相同.但后来我添加了largeRadius/4,因为我们必须将整个彩虹向下移动.这是因为减去相应的半径/ 2仅使彩虹居中,好像它是整个圆而不是半圆.
添加largeRadius/4会将彩虹向下移动整个高度的一半,使其正确居中以获得半圆形.最后,减去高度/ 4会使yCenter更改为高度/ 2,因为3/4*高度是您的任务中的要求.
对于评论中的所有问题感到抱歉,希望这一点得到澄清.