2012-12-14 38 views
0

下面的代碼將生成不同顏色的隨機生成的圓。我需要能夠通過它的大小來指定圓的顏色,以便循環將在不同位置產生相同的圓形圖案。這需要用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); 
      } 
     } 
    } 

    } 
+1

家庭作業?究竟是什麼問題? –

+0

「DrawingPanel」究竟是什麼?此外,你有'colors [i] [j] = colors [i] [j]',我想不出自我分配的理由。 – 2012-12-14 07:08:04

回答

0

提示

爲了打破這個概念你。

int color[6][3]是你的數組。

隨機生成的顏色值 創建類Random

Random random = new Random(); 

的目的信息在顏色列環

for(int i=0;i<6;i++) 
{ 
for(int j=0;j<3;j++) 
color[i][j]= random.nextInt(255); 
} 

現在畫像圓圈直徑[I],

參考color[i][0],color[i][1],color[i][2]其rgb值

0

所以,如果我得到這個權利,你想要隨機繪製圓圈,並且你想要顏色取決於直徑。

這裏是我的解決這一問題:pastebin

而且,我試圖展示它如何使用和不使用的Java 8來完成,所以不要混淆了。

這是應該的樣子:

Screenhot

相關問題