随机生成形状和颜色并绘制到画布

时间:2012-02-17 12:14:14

标签: java android drawing

generatePoints方法应该创建具有随机颜色(限于红色,绿色,蓝色)的'num'个随机形状(限于正方形,三角形,圆形)。到目前为止,所有程序都绘制了一个或两个对象,从来没有两个相同的形状,总是相同的颜色,从来没有三角形。过去我一直在撞墙,希望有人能够指出我的错误!

提前致谢!任何建议将不胜感激

scatterPlotActivity.java:

package scatter.plot;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.widget.FrameLayout;

public class ScatterPlotActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        scatterPoint[] points = generatePoints();
        for(int i = 0; i<points.length; i++)
            drawPoint(points[i]);

    }

    public void drawPoint(scatterPoint point) {
        FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
        main.addView(point);
    }


    public scatterPoint[] generatePoints(){
        Point point = new Point(0,0);
        int shape=0;
        int c=0;
        Paint colour = new Paint(Color.RED);

        int num = 20; //number of points to generate, maybe when I grow a brain I'll know how to prompt the user for this

        scatterPoint[] points = new scatterPoint[num];

        for(int i = 0; i < num; i++) {
            point.x = (int) (Math.random()*screenMetrics().x);
            point.y = (int) (Math.random()*screenMetrics().y);

            shape = (int) Math.round((Math.random()*2));

            c = (int) Math.round((Math.random()*2));
            switch(c){
                case 0:
                    colour.setColor(Color.RED);
                    break;
                case 1:
                    colour.setColor(Color.GREEN);
                    break;
                case 2:
                    colour.setColor(Color.BLUE);
                    break;
            }
            System.out.println("Point "+i+": ("+point.x+", "+point.y+") "+shape+" "+colour);
            points[i] = new scatterPoint(this, point, shape, colour);
        }

        return points;
    }

    public Point screenMetrics(){
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        return size;
    }
}

scatterPlot.java:

package scatter.plot;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.view.View;

public class scatterPoint extends View { //scatterPlot point has a position, shape, and colour
    private final Point coordinates;
    private final int itemShape;
    private Paint itemColour = new Paint(Paint.ANTI_ALIAS_FLAG);

    public scatterPoint(Context context, Point p, int shape, Paint colour) { // Constructor
        super(context);
        coordinates = p;
        itemShape = shape;
        itemColour = colour;
    }

     @Override
     protected void onDraw(Canvas canvas) {
         super.onDraw(canvas);

         int radius = 5; //hardcoded item size

         switch(itemShape){
            case 0:
                canvas.drawRect(coordinates.x - radius, coordinates.y - radius, coordinates.x + radius, coordinates.y + radius, itemColour);
                break;
            case 1:
                Path path = new Path();
                path.setFillType(Path.FillType.EVEN_ODD);
                path.moveTo(coordinates.x - radius, coordinates.y - radius);
                path.lineTo(coordinates.x, coordinates.y + radius);
                path.lineTo(coordinates.x + radius, coordinates.y - radius);
                path.lineTo(coordinates.x - radius, coordinates.y - radius);
                path.close();

                Paint fill = itemColour;
                fill.setStyle(Paint.Style.FILL);

                canvas.drawPath(path, fill);
                break;
            case 2:
                canvas.drawCircle(coordinates.x, coordinates.x, radius, itemColour);
                break;

         }
     }

    public Point getCoordinates(){
        return coordinates;
    }

    public int getShape(){
        return itemShape;
    }

    public Paint getColour(){
        return itemColour;
    }
}

2 个答案:

答案 0 :(得分:0)

我认为问题在于随机数的生成。 您是否检查了 Math.random()返回的数字,它们可能不会像预期的那样随机。

如果我没记错的话,尝试使用随机数生成器,就行了:

 Random generator = new Random(); 
 n = generator.nextInt((int) max);

答案 1 :(得分:0)

Android库是否包含java.util package? 如果是,您可以使用java.util.Random类(有关详细信息,请参阅this link。)

第一个解决方案:

Random rand = new Random(438976);   // initialize pseudo-random generator with an arbitrary seed

Point size = screenMetrics();   // so screenMetrics() is called only once
int xSize = size.x; 
int ySize = size.y;

for(int i = 0; i < num; i++) {
    point.x = rand.nextInt(xSize);   // from 0 (inclusive) to xSize (exclusive) 
    point.y = rand.nextInt(ySize);   // from 0 (inclusive) to ySize (exclusive)

    shape = rand.nextInt(3);   // from 0 (inclusive) to 3 (exclusive)


// ...
}

第二个解决方案: 为了生成不同的形状,您应该检查先前是否生成了新生成的形状。您可以使用Vector<Integer>来保存生成的形状,然后您可以生成新形状,直到它与先前生成的形状不同。

Random rand = new Random(438976);   // initialize pseudo-random generator with an arbitrary seed

Point size = screenMetrics();   // so screenMetrics() is called only once
int xSize = size.x; 
int ySize = size.y;

Vector<Integer> generated = new Vector<Integer>(0);    
for(int i = 0; i < num; i++) {
    point.x = rand.nextInt(xSize);   // from 0 (inclusive) to xSize (exclusive) 
    point.y = rand.nextInt(ySize);   // from 0 (inclusive) to ySize (exclusive)

    while (true) {
        shape = rand.nextInt(3);   // from 0 (inclusive) to 3 (exclusive)
        if (!generated.Contains(shape)){
            generated.add(shape);
            break;
        }
        else if (generated.size() == 3) {
            generated.clear();
            break;
        }
    }

// ...
}

第三种解决方案: 您可以使用不同的伪随机生成器,并使用不同的种子值测试它们。

Random pointsGenerator = new Random();
Random shapeGenerator = new Random(389453294);
Random colorGenerator = new Random(84568);
相关问题