gpt4 book ai didi

java - 随机生成形状和颜色并绘制到 Canvas 上

转载 作者:行者123 更新时间:2023-12-01 05:28:54 25 4
gpt4 key购买 nike

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;
}
}

最佳答案

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);

关于java - 随机生成形状和颜色并绘制到 Canvas 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9328011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com