Random color to fill triangles in java

Andy J. :

I am creating an application that draws 5 random triangles of different random colors. The random drawing of triangles work but for some reason the triangles are all the same color, sometimes having different colors where the triangles intersect. Here is a picture:

image of what the application creates

Does anyone know how to fix this issue and make it so that each triangle will be a different color?

TrianglesJPanel.java:

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.util.Random;

public class TrianglesJPanel extends JPanel 
{
    public void paintComponent(Graphics g) {
        //call superclass's paintComponent
        super.paintComponent(g);
        Random random = new Random();

        Graphics2D g2 = (Graphics2D) g;
        GeneralPath triangle = new GeneralPath();  //create general path object

        //get the height and width
        int height = this.getHeight();
        int width = this.getWidth();

        int[] x = new int[3];
        int[] y = new int[3];

        //draw 5 random triangles in application frame that can be adjusted
        for (int i = 0; i < 5; i++) {
            //get random points on the panel
            for (int j = 0; j < 3; j++) {
                x[j] = random.nextInt(width + 1); //random number from 0 to width
                y[j] = random.nextInt(height + 1);  //random number from 0 to height
                //Ex. Frame 500x500: x could be from 0 to 500, y from 0 to 500
            }

            triangle.moveTo(x[0], y[0]);  //start pos
            triangle.lineTo(x[1], y[1]);  //2nd point
            triangle.lineTo(x[2], y[2]);  //3rd point
            triangle.lineTo(x[0], y[0]);  //back to start pos
            triangle.closePath();         //close the triangle

            System.out.println(g2.getColor());
            //set random color for triangle
            g2.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));

            g2.fill(triangle);  //draw filled triangle
        }
    }
}

Triangle.java:

import java.awt.Color;
import javax.swing.JFrame;

public class Triangles
{
    public static void main(String args[]) {
        //choose title
        JFrame frame = new JFrame("Drawing random triangles");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //drawing done on JPanel, embedded in a JFrame
        TrianglesJPanel triangle = new TrianglesJPanel();
        frame.add(triangle);               //add the triangles to frame
        frame.setBackground(Color.WHITE);   //set background of frame to white color
        frame.setSize(500,500);    //set frame size to 500x500
        frame.setVisible(true);    //display frame
    }
}
Hovercraft Full Of Eels :

All Triangles share the same GeneralPath object, and it is being filled with the last color used. To solve this you need a new GeneralPath object for each triangle

so:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    // GeneralPath triangle = new GeneralPath();  //create general path object
    int height = this.getHeight();
    int width = this.getWidth();
    int[] x = new int[3];
    int[] y = new int[3];
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 3; j++) {
            x[j] = random.nextInt(width + 1); //random number from 0 to width
            y[j] = random.nextInt(height + 1);  //random number from 0 to height
        }
        GeneralPath triangle = new GeneralPath();
        triangle.moveTo(x[0], y[0]);  //start pos
        triangle.lineTo(x[1], y[1]);  //2nd point
        triangle.lineTo(x[2], y[2]);  //3rd point
        triangle.lineTo(x[0], y[0]);  //back to start pos
        triangle.closePath();         //close the triangle
        System.out.println(g2.getColor());
        g2.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
        g2.fill(triangle);  //draw filled triangle
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=334691&siteId=1