随机颜色填充Java中的三角形

问题描述 投票:0回答:1

我正在创建一个应用程序,该应用程序绘制5个具有不同随机颜色的随机三角形。随机绘制三角形是可行的,但是由于某些原因,三角形都是相同的颜色,有时在三角形相交处具有不同的颜色。这是一张图片:

image of what the application creates

有人知道如何解决此问题并使它成为每个三角形都具有不同的颜色吗?

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

}

}

java jframe jpanel
1个回答
0
投票

所有三角形共享相同的GeneralPath对象,并使用最后使用的颜色填充它。为了解决这个问题,您需要为每个三角形

创建一个新的GeneralPath对象

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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.