Java创建颜色数组并在形状中使用它们
我需要一份课堂作业的指导。目前,我有一个由红色和蓝色矩形组成的金字塔,我的任务是通过创建一个不同颜色的数组(确切地说是9)来随机化这些颜色。我在创建数组时遇到了一些问题,因为它总是让我出错,我希望有人能给我指出正确的方向,让我开始。下面是我的代码:我当前的数组是否正确?我根据课本上的一个例子建立了这个数组,但它似乎不起作用。如蒙协助,将不胜感激Java创建颜色数组并在形状中使用它们,java,arrays,colors,Java,Arrays,Colors,我需要一份课堂作业的指导。目前,我有一个由红色和蓝色矩形组成的金字塔,我的任务是通过创建一个不同颜色的数组(确切地说是9)来随机化这些颜色。我在创建数组时遇到了一些问题,因为它总是让我出错,我希望有人能给我指出正确的方向,让我开始。下面是我的代码:我当前的数组是否正确?我根据课本上的一个例子建立了这个数组,但它似乎不起作用。如蒙协助,将不胜感激 @SuppressWarnings("serial") public class Legos2 extends JFrame { private
@SuppressWarnings("serial")
public class Legos2 extends JFrame {
private int startX;
private int startY;
private int legoWidth;
private int legoHeight;
private int baseLength;
private int arcWidth;
private int arcHeight;
//Declare and Array of Colors
Color[] colors;
//Allocate the size of the array
colors = new Color[4];
//Initialize the values of the array
colors[0] = new Color(Color.red);
colors[1] = new Color(Color.blue);
colors[2] = new Color(Color.yellow);
colors[3] = new Color(Color.green);
// Constructor
public Legos2() {
super("Jimmy's LEGOs");
startX = 20;
startY = 300;
legoWidth = 50;
legoHeight = 20;
baseLength = 10;
arcWidth = 2;
arcHeight = 2;
}
// The drawings in the graphics context
public void paint(Graphics g)
{
// Call the paint method of the JFrame
super.paint(g);
int currentX = startX;
int currentY = startY;
//row = 0 is the bottom row
for (int row = 1; row <= baseLength; row++)
{
currentX = startX;
System.out.println("row = " + row);
for (int col = 0; col <= baseLength - row; col++)
{
if (col % 2 == 0)
g.setColor(Color.red);
else
g.setColor(Color.blue);
System.out.println("col = " + col);
g.fillRoundRect(currentX, currentY, legoWidth, legoHeight, arcWidth, arcHeight);
currentX = currentX + legoWidth;
}
currentY -= legoHeight;
startX += legoWidth /2;
}
}
// The main method
public static void main(String[] args) {
Legos2 app = new Legos2();
// Set the size and the visibility
app.setSize(550, 325);
app.setVisible(true);
// Exit on close is clicked
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
@SuppressWarnings(“串行”)
公共类Legos2扩展了JFrame{
私人int startX;
私人int startY;
列高维兹私人酒店;
私人乐高;
专用整数基长;
私有宽度;
私家车;
//声明和数组的颜色
颜色[]颜色;
//分配数组的大小
颜色=新颜色[4];
//初始化数组的值
颜色[0]=新颜色(颜色为红色);
颜色[1]=新颜色(颜色为蓝色);
颜色[2]=新颜色(颜色为黄色);
颜色[3]=新颜色(颜色为绿色);
//建造师
公共立法2(){
超级(“吉米的乐高”);
startX=20;
startY=300;
legoWidth=50;
乐高=20;
基长=10;
弧宽=2;
arcHeight=2;
}
//图形上下文中的图形
公共空间涂料(图g)
{
//调用JFrame的绘制方法
超级油漆(g);
int currentX=startX;
int currentY=startY;
//row=0是最下面一行
对于(int row=1;row,应该在构造函数中初始化数组。
不能在方法外部初始化它
public Legos2() {
//Allocate the size of the array
colors = new Color[4];
//Initialize the values of the array
colors[0] = new Color(Color.red);
colors[1] = new Color(Color.blue);
colors[2] = new Color(Color.yellow);
colors[3] = new Color(Color.green);
...
}
除了Eran答案外:
您必须导入颜色、图形和JFrame类
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
要指定颜色,请在构造函数中执行此操作:
public Legos2() {
colors = new Color[4];
//Initialize the values of the array
colors[0] = Color.red;
colors[1] = Color.blue;
colors[2] = Color.yellow;
colors[3] = Color.green;
“因为它不断给我一个错误”最好显示错误消息和它发生的位置错误发生在我将它们分配给不同颜色的数组中。例如“colors[0]=new Color(Color.red);“错误在“red”上,消息是“red无法解析或不是字段”.成功了。谢谢!我会继续努力并跟进。