Skip to content

Instantly share code, notes, and snippets.

Created May 25, 2017 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7c2f94d9f279f2295f44e59f1709f571 to your computer and use it in GitHub Desktop.
Save anonymous/7c2f94d9f279f2295f44e59f1709f571 to your computer and use it in GitHub Desktop.
Drawing Random Triangles
import javax.swing.JFrame;
import java.awt.Polygon;
import java.util.Random;
public class RandomTriangles extends Canvas {
public void paint(Graphics g) {
Random r = new Random();
Polygon tri = new Polygon();
for (int X = 0; X <= 500; X++) { //randomizer loop 9 numbers, 500 times
//Triangle point randomizer
int tPoint1X = 1 + r.nextInt(798);
int tPoint1Y = 1 + r.nextInt(598);
int tPoint2X = 1 + r.nextInt(798);
int tPoint2Y = 1 + r.nextInt(598);
int tPoint3X = 1 + r.nextInt(798);
int tPoint3Y = 1 + r.nextInt(598);
tri.addPoint(tPoint1X, tPoint1Y);
tri.addPoint(tPoint2X, tPoint2Y);
tri.addPoint(tPoint3X, tPoint3Y);
//random color generator
int randR = r.nextInt(255);
int randG = r.nextInt(255);
int randB = r.nextInt(255);
Color randColor = new Color(randR,randG,randB);
g.setColor(randColor);
g.fillPolygon(tri);
}
}
public static void main(String[] args) {
JFrame win = new JFrame("Random Triangles");
win.setSize(800,600);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.add(new BoringTriangle());
win.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment