Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to write a program where I start drawing a Sierpinski Triangle in a

ID: 3630982 • Letter: I

Question

I'm trying to write a program where I start drawing a Sierpinski Triangle in a Java applet using a user mouse input as the starting point. From the starting point, one of the three vertices of the triangle is picked and a new point is plotted halfway between the starting point and that vertex. After thousands of iterations, a Sierpinski triangle is supposed to form. Right now, I'm stuck with this program. I've drawn the equilateral triangle that I would like to work with and have coded in the mouse inputs and I've started trying to put in the code to start plotting those points but I can't get it to work. Help would be nice. Thanks!

Here's what I have so far.

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class Sierpinski extends Applet implements MouseListener, MouseMotionListener{

int width, height;

int mx,my;

boolean isButtonPressed = false;

public void init(){

    width=getSize().width;

    height=getSize().height;

    setBackground(Color.white);

    mx=width/2;

    my=height/2;

    addMouseListener(this);

    addMouseMotionListener(this);}

public void paint (Graphics g){

    int[] xPoints={205,65,335};

    int[] yPoints={30,255,255};

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

public void mouseEntered(MouseEvent e){}

public void mouseExited(MouseEvent e){}

public void mouseClicked(MouseEvent e){

    double[]cx={205,65,335};

    double[]cy={30,255,255};

    double x=130, y=189; //midpoint of the equilateral triangle

    mx=e.getX();

    my=e.getY();

    for (int t=0;t<10000;t++)

    {x=(x+cx[mx])/2;

      y=(y+cy[my])/2;

    g.drawPoint(x,y);}

}

public void mousePressed(MouseEvent e){}

public void mouseReleased(MouseEvent e){}

public void mouseMoved(MouseEvent e){}

public void mouseDragged(MouseEvent e){}

}

Explanation / Answer

import javaawt*; import javaxswing*;   public class Sierpinski extends JFrame {      /**make a new Sierpinski*/      public Sierpinski()      {          //add STPane to frame          thisadd(new STPane());          //pack          thispack();          //set visible          thissetVisible(true);      }      /**main*/      public static void main(String[] args){          //make a new frame          new Sierpinski();      } } /** * Point class holds a 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 Sierpinski Triangle */ class STPane extends JComponent {      //holds the initial triangle for the sierpenski      Point[] triangle = new Point[];      //first point      Point fp;      //current point      Point cp=new Point();      //default constructor simply sets the original size      public STPane(){          thissetPreferredSize(new Dimension(,));      }      //simple method to ge a - range random number to select a new point      public int randto(){          return (int)(Mathrandom()*)%;      }      //paint where the magic of the chaos happens      public void paint(Graphics g){          //get the current size of the component          Dimension d = supergetSize();          //make an isocilese triangle from the size of the          //component for the original points          triangle[] = new Point((int)dgetWidth()/,);          triangle[] = new Point(,(int)dgetHeight());          triangle[] = new Point((int)dgetWidth(),(int)dgetHeight());          //get an arbitrary point in the middle of the screen somewhere          fp=new Point((int)(Mathrandom()*dgetWidth()),(int)(Mathrandom()*dgetHeight()/));          //get a random triangle point          Point p = triangle[randto()];          //get the first current point by going half way from the first          //point to the chosen triangle point          cpx=(px+fpx)/;          cpy=(py+fpy)/;          //draw the first point          gdrawRect(cpx,cpy,,);          //compute points in the same fashion but use the current          //point rather than the very original point          for(int i = ; i