This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes i want to draw an equilateral sierpinski triangle. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "i want to draw an equilateral sierpinski triangle." Watch "i want to draw an equilateral sierpinski triangle." New topic

i want to draw an equilateral sierpinski triangle.

Praise Onwuachu

Joined: Feb 11, 2009
Posts: 2
i want my triangle to be equivalent and not isoceles. i need help with this code.

import java.awt.*;
import javax.swing.*;

public class Triangle


/**make a new Triangle*/

public Triangle()

Frame f = new Frame(" MCS GroupX CHAOS GAME ");

//add STPane to frame

f.add(new STPane());




public static void main(String[] args){

//make a new frame

new Triangle();



// Point class holds a 2 dimentional integer point.

class Point {

//two portions of the point

int x,y;

//default constructor does nothing

public Point(){}

//constructor that takes two arguments

public Point(int nx,int ny){





//class that computes and displays the Triangle

class STPane extends JComponent {

//holds the initial triangle

Point[] triangle = new Point[3];

//first point

Point fp;

//current point

Point cp=new Point();

//default constructor simply sets the original size

public STPane(){

this.setPreferredSize(new Dimension(500,500));


//simple method to ge a 0-2 range random number to select a new point

public int rand0to2(){

return (int)(Math.random() * 3);


//paint the points of the chaos

public void paint(Graphics g){

//get the current size of the component

Dimension d = super.getSize();

//make triangle from the size of the

//component for the original points

triangle[0] = new Point((int)d.getWidth()/2,0);

triangle[1] = new Point(0,(int)d.getHeight());

triangle[2] = new Point((int)d.getWidth(),(int)d.getHeight());

//get an arbitrary point in the middle of the screen somewhere

fp=new Point((int)(Math.random()*d.getWidth()),(int)(Math.random()*d.getHeight()/2));

//get a random triangle point

Point p = triangle[rand0to2()];

//get the first current point by going half way from the first

//point to the chosen triangle point



//draw the first point





d legend
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63348

"Legend Praise", please check your private messages for an important administrative matter.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rob Spoor

Joined: Oct 27, 2005
Posts: 20131

Also, please Use Code Tags in the future. Although that won't make any difference if you don't indent your code at all...

How To Ask Questions How To Answer Questions
Praise Onwuachu

Joined: Feb 11, 2009
Posts: 2
am doing that now.
pete stein

Joined: Feb 23, 2007
Posts: 1561
1) You really don't want to place a JComponent on a Frame, trust me. Put it in a JFrame.
2) To set the triangle as equilateral, calculate the height vs base of an equilateral triangle (any basic geometry text will tell you, something about sqrt(3)/2), and use these numbers to set your preferred size of the jcomponent.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10747

Praise Onwuachu wrote:am doing that now.

Still not good.
Please read Bear's message very carefully.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
I agree. Here's the link:
subject: i want to draw an equilateral sierpinski triangle.
It's not a secret anymore!