Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7507101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:25:11+00:00 2026-05-29T22:25:11+00:00

The generatePoints method is supposed to create ‘num’ number of random shapes (limited to

  • 0

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:25:12+00:00Added an answer on May 29, 2026 at 10:25 pm

    Do the Android libraries include java.util package?
    If yes, you could use the java.util.Random class (see this link for details).

    First solution:

    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)
    
    
    // ...
    }
    

    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.

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

    Third solution:
    You could use different pseudo-random generators and test them with different values of the seed.

    Random pointsGenerator = new Random();
    Random shapeGenerator = new Random(389453294);
    Random colorGenerator = new Random(84568);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day to all. I need to create a graphical countdown in js. The
I wish to randomly and uniformly generate points on a cylinder and a cone
I am relatively new to ggplot, so please forgive me if some of my
I need assistance in simulating movement between 2 points in a plane. Consider two
I'm trying to compute Pi, but what I really want to achieve is efficiency
How do we generate data points following a Gaussian (normal) distribution in R? Suppose
I have one list with points. I am applying: var result = neighbors.SelectMany(element =>
I am doing a project on face recognition.To test a face we need to
I am making a lunar lander game that randomizes the terrain for each new
It's weird! I was working on my CSS and jquery just stopped working! checked

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.