2017-12-27 7 views
0

안녕 모두들 나는 내 과제의 일부로 floodit 게임을위한 안드로이드 스튜디오에서 그리드를 생성하려고합니다. Genereated grid는 매번 무작위로 만들어야하므로 아래와 같이 switch 문을 사용하고 페인트하여 색상을 생성합니다.캔버스 그리드 무작위 세대가 생성되지 않습니다

Paint paint1 = new Paint (getResources().getColor(R.color.color1)); 
     Paint paint2 = new Paint (getResources().getColor(R.color.color2)); 
     Paint paint3 = new Paint (getResources().getColor(R.color.color3)); 
     Paint paint4 = new Paint (getResources().getColor(R.color.color4)); 
     Paint paint5 = new Paint (getResources().getColor(R.color.color5)); 
     Paint paint6 = new Paint (getResources().getColor(R.color.color6)); 


     for (int col = 0; col < mGame.getHeight(); col++) { 
      for (int row = 0; row < mGame.getWidth(); row++) { 
       Random random = new Random(6); 
       int num = 1; 
       int randomResult = random.nextInt(); 

       float cx = separator + (diameterx + separator) * col + diameterx/2; 
       float cy = separator + (diametery + separator) * row + diametery/2; 
       canvas.drawRect(cx, cy, availableWidth, availableHeight, paint6); 
       switch (randomResult) { 
        case 0: 
         canvas.drawRect(cx, cy, availableWidth/2, availableHeight/2, paint1); 
         break; 
        case 1: 
         canvas.drawRect(cx, cy, availableWidth, availableHeight, paint2); 
         break; 
        case 2: 
         canvas.drawRect(cx, cy, availableWidth, availableHeight, paint3); 
         break; 
        case 3: 
         canvas.drawRect(cx, cy, availableWidth, availableHeight, paint4); 
         break; 
        case 4: 
         canvas.drawRect(cx, cy, availableWidth, availableHeight, paint5); 
         break; 
        case 5: 
         canvas.drawRect(cx, cy, availableWidth, availableHeight, paint6); 
         break; 

       } 


      } 
     } 

문제는 내가 이해하지 못하는거야 그 블록 내에서 몇 가지 문제, 그래서 스위치 문 내의 코드가 바로 switch 문 위의 canvas.drawRect(cx, cy, availableWidth, availableHeight, paint6);가 잘 작동하는 것 같다 동안 (데모 위해) 작동하지 않습니다이다 . 어떤 도움을 주시면 감사하겠습니다.

종류는 모든 페인트에 설정할

답변

0

시도에 관하여 : 시도에 대한

paint.setStrokeWidth(20); 
paint.setStyle(Paint.Style.STROKE); 
paint.setAntiAlias(true); 
+0

감사하지만 불행하게도 아무 짓도 안 했어요. – Deadraa

+0

'cx','cy','availableWidth'과'availableHeight'는 올바른 값을 가지고 있습니까? – BullyBoo

+0

또한 Random 클래스를 잘못 사용했습니다. 시도 : '임의 임의 = 새로 만들기 임의(); int randomResult = random.nextInt (6); ' – BullyBoo

관련 문제