mercredi 21 avril 2021

My chaos of a code. Random color and size circles

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

public class keyExample extends JPanel implements ActionListener, KeyListener {
    Timer t = new Timer(5, this);
    double x = 0, y = 0;

    double changeX = 0, changeY = 0;

    public keyExample() {
        t.start();
        addKeyListener(this);

        setFocusable(true);

        setFocusTraversalKeysEnabled(false);

    }

    private static final int N_CIRCLES = 10;

    private static final double MIN_RADIUS = 5;

    private static final double MAX_RADIUS = 50;

    public void run() {
        Random gen = new Random();

        for (int i = 0; i < N_CIRCLES; i++) {

            double r = gen.nextDouble(MIN_RADIUS, MAX_RADIUS);
            double x = gen.nextDouble(0, getWidth() - 2 * r);
            double y = gen.nextDouble(0, getHeight() - 2 * r);

            Circle circle = new Circle(x, y, 2 * r, 2 * r);
            circle.setFilled(true);


            int red = gen.nextInt(256);
            int green = gen.nextInt(256);
            int blue = gen.nextInt(256);

            Color backColor = new Color(red, green, blue);

            pane.add(panel);

        }
        add(circle);
    }
}

I am supposed to create a code that produces circles with random colors and sizes. While tweaking with some parts and another. I have created this mess that I cannot comprehend. Please help me fixing the codes so that I will produce my intended outcome.




Aucun commentaire:

Enregistrer un commentaire