Question: Below is the java program where I was able to print 500 random triangles with colors but as per my requirements, this code is not
Below is the java program where I was able to print 500 random triangles with colors but as per my requirements, this code is not correct.
I would need a Java program that displays 500 colored triangles but I must choose the color of each triangle randomly by selecting random values for the amount of red, green, and blue. Then I need to select three random points for theverticesof the triangle and each vertex will require two random numbers: one random x-value and one random y-value. It means that each triangle requiresninerandom numbers which is kept inside a loop of 500.
import java.awt.*;
import javax.swing.*;
import java.util.Random;
public class RandomTriangles extends Canvas {
public void paint(Graphics g){
Random nmb = new Random();
for(int z = 1; z<=500; z++) {
int x1 = nmb.nextInt(200) + 1;
int x2 = nmb.nextInt(200) + 1;
int x3 = nmb.nextInt(200) + 1;
int x4 = nmb.nextInt(500) + 1;
int x5 = nmb.nextInt(500) + 1;
int x6 = nmb.nextInt(500) + 1;
int x7 = nmb.nextInt(500) + 1;
int x8 = nmb.nextInt(500) + 1;
int x9 = nmb.nextInt(500) + 1;
g.setColor(new Color(x1, x2, x3));
g.fillPolygon(new int[]{x4, x5, x6}, new int[]{x7, x8, x9}, 3);
}
}
public static void main( String[] args )
{
JFrame win = new JFrame("Random Traingles");
win.setSize(800,600);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RandomTriangles canvas = new RandomTriangles();
win.add( canvas );
win.setVisible(true);
}
}
Can you please modify my code in such a way that all the requirements are met correctly.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
