• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

i want to draw an equilateral sierpinski triangle.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want my triangle to be equivalent and not isoceles. i need help with this code.



[code]
import java.awt.*;
import javax.swing.*;


public class Triangle

{

/**make a new Triangle*/

public Triangle()

{
Frame f = new Frame(" MCS GroupX CHAOS GAME ");
f.setBackground(Color.pink);

//add STPane to frame

f.add(new STPane());


f.pack();


f.setVisible(true);

}



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){

x=nx;

y=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){
g.setColor(Color.black);



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

cp.x=(p.x+fp.x)/2;

cp.y=(p.y+fp.y)/2;



//draw the first point

g.drawRect(cp.x,cp.y,0,0);



}

}

}
[\code]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Legend Praise", please check your private messages for an important administrative matter.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, please Use Code Tags in the future. Although that won't make any difference if you don't indent your code at all...
 
Praise Onwuachu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
am doing that now.
thanks.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Praise Onwuachu wrote:am doing that now.
thanks.


Nope.
Still not good.
Please read Bear's message very carefully.
 
reply
    Bookmark Topic Watch Topic
  • New Topic