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

Random color to fill triangles in java

我正在创建一个应用程序,它绘制 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
    }
}

所有三角形共享同一个 GeneralPath 对象,并且正在使用上次使用的颜色填充。要解决这个问题,您需要一个新的 GeneralPath 对象用于 each triangle

所以:

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
    }
}

答案已经提供,但这里还有一些其他建议。

  • 不要画回x[0], y[0]。那是 closePath 的工作。
  • 使用 renderingHints 平均化边缘,使它们看起来更平滑。
   g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
  • 如果你想让框架在屏幕上居中,你可以这样做。
        frame.setLocationRelativeTo(null);
  • 最后,将 x 和 y 声明为 floatsdoubles。它可能没有任何区别,但应该尽可能与预期类型一致。