2013-11-24 38 views
-1

我需要一些指导,需要为类指定一些东西。我目前有一个由红色和蓝色矩形组成的金字塔,我的任务是通过创建各种颜色的数组(随机确定9)随机化这些颜色。我在创建阵列时遇到了一些麻烦,因为它一直在给我一个错误,我希望有人能指出我正确的方向来让我开始。这是我的代码:我的当前数组是否正确?我以我的教科书中的一个例子为基础,但它似乎没有用。任何援助将不胜感激。Java创建颜色数组并在形状中使用它们

@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); 

    } 
} 
+0

*“因为它一直让我出错”*最好显示错误信息并将其发生在何处 – iShaalan

+0

th在我为其分配各种颜色的数组中发生了错误。例如“颜色[0] =新颜色(Color.red);”错误是在“红色”,并且消息是“红色无法解析或不是字段”。 – jnguyen

回答

1

除了叶兰答案:

你必须输入色彩,图形和的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; 
+0

是的,我这样做,我仍然得到错误消息。 – jnguyen

+0

@jnguyen更新 – iShaalan

+0

工作。谢谢!我会继续努力并跟进。 – jnguyen

0

你应该在你的构造函数中初始化你的数组。 您无法在方法外初始化它。

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); 
... 
} 
相关问题