3

Create a 2D ball with a random color when created

 2 years ago
source link: https://www.codesd.com/item/create-a-2d-ball-with-a-random-color-when-created.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Create a 2D ball with a random color when created

advertisements

So I have a ball class. The ball gets created, and starts to drop. When the ball is created I'd like it to be any color between Red, Yellow, Green and Purple. How would one go about it. I created a ball already but the only color is red.

Ball.java:

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;
import android.view.ViewTreeObserver;

public class RedBall extends View{

    // Ball attributes
    private float ballRadius = 30;
    private float ballX = 50;
    private float ballY = 50;
    private float ballSpeed = 10.0f;
    private RectF ballBounds;
    private Paint ballColor;
    boolean doBallAnimation = false;
    // For game elements
    private int score = 0;
    ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
    long startTime, prevTime; // Used to track elapsed time for animations and fps

    public RedBall(Context context){
        super(context);
        // Initialize game elements
        ballBounds = new RectF();
        ballColor = new Paint();
        this.setFocusableInTouchMode(true);

        getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener(){
                    @Override
                    public void onGlobalLayout(){
                        getViewTreeObserver().removeOnGlobalLayoutListener(this);

                        ballY = 0;
                        ballX = (getWidth()- ballRadius) / 2.0f;

                        doBallAnimation = true;
                        animator.start();
                    }
                }
        );

        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){
            @Override
            public void onAnimationUpdate(ValueAnimator arg0){
                long nowTime = System.currentTimeMillis();
                float secs = (float)(nowTime - prevTime) / 1000f;
                prevTime = nowTime;

                if((ballY + ballSpeed) > (getHeight() - (ballRadius)))
                {
                    animator.cancel();
                    return;
                }

                ballY += ballSpeed;

                // Force a redraw to see the ball in its new position
                invalidate();
            }
        });
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setDuration(3000);
    }

    public void onDraw(Canvas canvas){
        // Draw ball
        if(doBallAnimation)
        {
            ballBounds.set(ballX - ballRadius, ballY - ballRadius, ballX + ballRadius, ballY + ballRadius);
            ballColor.setColor(Color.RED);
            //private Color[] colors = {Color.RED, Color.GREEN, Color.YELLOW, Color.rgb(255, 0, 255)};

            canvas.drawOval(ballBounds, ballColor);
        }
    }
}


You can use Random Class to generate random colors by using Color.rgb(int,int,int) function where int can be between 0 to 255

    Random rand=new Random();
    Paint paint=new Paint();
    paint.setColor(Color.rgb(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)));//Will generate random color


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK