画布网格随机生成不生成

时间:2017-12-27 20:52:36

标签: java android canvas android-canvas paint

大家好,我试图在android studio中为Floodit游戏生成一个网格作为我的任务的一部分。每次生成的网格应该是随机的,所以我使用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语句中的代码不起作用(为了演示),switch语句上方的canvas.drawRect(cx, cy, availableWidth, availableHeight, paint6);似乎工作正常,因此它在该块中的某些问题我和# 39;我不理解。任何帮助,将不胜感激。

亲切的问候

1 个答案:

答案 0 :(得分:0)

尝试设置所有颜料:

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