Java 创建一系列颜色并在形状中使用它们

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20170464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:41:49  来源:igfitidea点击:

Java creating an array of colors and utilizing them in shapes

javaarrayscolors

提问by jnguyen

I am in some need of guidance with an assignment for class. I currently have a pyramid made up of red and blue rectangles and my task is to randomize those colors by creating an array of various colors (9 to be exact). I am having some trouble creating the array as it keeps getting me an error and I was hoping someone could point me in the right direction to get me started. Here is my code: is my current array even correct? I based that array on an example from my textbook but it didn't seem to work. Any assistance would be greatly appreciated.

我需要一些课堂作业的指导。我目前有一个由红色和蓝色矩形组成的金字塔,我的任务是通过创建各种颜色的数组(准确地说是 9 个)来随机化这些颜色。我在创建数组时遇到了一些麻烦,因为它不断让我出错,我希望有人能指出我正确的方向,让我开始。这是我的代码:我当前的数组是否正确?我根据教科书中的一个示例创建了该数组,但它似乎不起作用。任何帮助将不胜感激。

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

   }
}

采纳答案by iShaalan

In addition to Eran answer :

除了 Eran 的回答:

you have to import color , Graphics and JFrame classes

您必须导入 color 、 Graphics 和 JFrame 类

import java.awt.Color;

import java.awt.Graphics;

import javax.swing.JFrame;

and to assign colors do it inside the constructor :

并在构造函数中分配颜色:

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;

回答by Eran

You should initialize your array in your constructor. You can't initialize it outside a method.

您应该在构造函数中初始化数组。你不能在方法之外初始化它。

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);
...
}