The generatePoints method is supposed to create ‘num’ number of random shapes (limited to square, triangle, circle) with random colour (limited to red, green, blue). So far all the program does is draw one or two objects, never two of the same shape, always the same colour, and never a triangle. I’ve been banging my head against a wall for the past while, hopefully someone will be able to point out my blunder(s)!
Thanks in advance! Any suggestions would be appreciated
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;
}
}
Do the Android libraries include java.util package?
If yes, you could use the
java.util.Randomclass (see this link for details).First solution:
Second solution:
In order to generate different shapes, you should check if the new generated shape has been previously generated. You could use a
Vector<Integer>to hold the generated shapes, then you could generate a new shape until it is different from a previously generated shape.Third solution:
You could use different pseudo-random generators and test them with different values of the seed.