mercredi 14 octobre 2020

My Computer is Being Bitter. Can Someone Test This For Me?

I'm working on an assignment and I just need to know if I did this right. The computer I'm using is extremely temperamental and won't run any of the compilers I've installed. The online compiler I normally use won't run this. If anyone could test this code, it would be extremely helpful.

These are the instructions:

Your submission should be only JAVA source files (no output screen pictures, DOC files, JPG, etc.).

Write a program that displays 500 colored triangles on the screen. You must choose the color of each triangle randomly by selecting random values for the amount of red, green, and blue. Then select three random points for the vertices of the triangle. Each vertex will require two random numbers: one random x-value and one random y-value. This means that each triangle requires nine random numbers.

Finally, put all this stuff inside a loop that repeats 500 times.

The code looks right? I just wish I could see the results. Thank you.

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class newFlag extends Frame {
static int width=600;
static int height=500;
public static void main(String args[]) {
new newFlag();
}

public newFlag() {
//Title our frame.
super("Java 2D newFlag");

//Set the size for the frame.

setSize(width,height);

//We need to turn on the visibility of our frame
//by setting the Visible parameter to true.
setVisible(true);

addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{dispose(); System.exit(0);}
}
);
}

public void paint(Graphics g) {

int R;
int G ;
int B;

int x1,x2,x3,y1,y2,y3;
int nPoints=3;
for(int i=0;i<500;i++)
{
R = (int) (Math.random( )*256);
G = (int)(Math.random( )*256);
B = (int)(Math.random( )*256);
Color rcolor = new Color(R, G, B);
g.setColor(rcolor);
x1=(int)(Math.random()*width);
x2=(int)(Math.random()*width);
x3=(int)(Math.random()*width);
y1=(int)(Math.random()*height);
y2=(int)(Math.random()*height);
y3=(int)(Math.random()*height);
int xPoints[]={x1,x2,x3};
int yPoints[]={y1,y2,y3};

g.drawPolygon(xPoints,yPoints,nPoints);
}
}
}



Aucun commentaire:

Enregistrer un commentaire