下面的代碼將生成不同顏色的隨機生成的圓。我需要能夠通過它的大小來指定圓的顏色,以便循環將在不同位置產生相同的圓形圖案。這需要用2d數組完成。我知道這可能不是那麼難,但我似乎無法理解這個概念。 下面是方向和我的代碼。構建一個2d數組以繪製不同的色環
謝謝!
設置一個2-D,int colors [] []數組,每行6個行,每個圓圈3個列,每個元素(紅,綠,藍)使用一個顏色。在上面的顯示中,顏色值是在程序開始時在0到255範圍內隨機生成的。然後對於每個直徑[i],顏色[i] [0],顏色[i] [1]和顏色[i] [2]用於RGB級別。
import java.io.*;
import java.util.*;
import java.awt.*;
public class Lab10 {
/**
* @param args
*/
public static void main(String[] args) {
Scanner console=new Scanner (System.in);
Random r = new Random();
int [] color= new int [3];
color[0]=r.nextInt(256);
color[1]=r.nextInt(256);
color[2]=r.nextInt(256);
System.out.println("Please enter 6 integer values. The values should be in descending order and in the range 100 to 1.");
int[] diameters=new int[6];
int colors[][] = new int [6][3];
for(int i=0; i<6; i++){
diameters[i]=console.nextInt();//values entered
}
for (int i=0; i<diameters.length; i++) {
for (int j = 0; j < color.length; j++) {
colors[i][j]=colors[i][j];
}
}
int panelX = 400, panelY = 400;
DrawingPanel panel = new DrawingPanel(panelX, panelY);
panel.setBackground(Color.WHITE);
Graphics g = panel.getGraphics();
for (int i=0; i<10; i++){
int centerX=r.nextInt(350);
int centerY=r.nextInt(350);
for(int value:diameters){
g.setColor(new Color(r.nextInt(256),r.nextInt(256), r.nextInt(256)));
g.fillOval(centerX - value , centerY - value, 2 * value, 2 * value);
}
}
}
}
家庭作業?究竟是什麼問題? –
「DrawingPanel」究竟是什麼?此外,你有'colors [i] [j] = colors [i] [j]',我想不出自我分配的理由。 – 2012-12-14 07:08:04