wanted to change from triangles or square maybe use the circles to make squares
ID: 3547417 • Letter: W
Question
wanted to change from triangles or square maybe use the circles to make squares and for some reason i got it flip upside down.
heres what i have
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
class SierpinskiPicture extends Canvas {
protected int gasket_depth = 5; // the depth to which the triangle is drawn
protected Dimension preferred_size = new Dimension(300, 300);
protected Dimension minimum_size = new Dimension(50, 50);
public Dimension getPreferredSize() {
return preferred_size;
}
public Dimension getMinimumSize() {
return minimum_size;
}
public void paint(Graphics g) {
double[] x = new double[2];
double[] y = new double[2];
double[] z = new double[2];
x[0] = 0.0; x[1] = 0.0;
y[0] = 1.0; y[1] = 0.0;
z[0] = 0.5; z[1] = 1.0;
drawTriangle(x, y, z);
drawGasket(gasket_depth, x, y, z);
}
public void drawGasket(int depth,
double[] a, double[] b, double[] c) {
double[] abmid = new double[2];
double[] bcmid = new double[2];
double[] acmid = new double[2];
int i;
if(depth == 0) {
return;
} else {
for(i = 0; i < 2; i++) {
abmid[i] = (a[i] + b[i]) / 2.0;
bcmid[i] = (b[i] + c[i]) / 2.0;
acmid[i] = (a[i] + c[i]) / 2.0;
}
drawTriangle(acmid, bcmid, abmid);
drawGasket(depth - 1, a, abmid, acmid);
drawGasket(depth - 1, abmid, b, bcmid);
drawGasket(depth - 1, acmid, bcmid, c);
}
}
public void drawTriangle(double[] x, double[] y, double[] z) {
int[][] coord = new int[2][4];
int i;
Dimension this_size = this.getSize();
coord[0][0] = (int) (this_size.width * x[0]);
coord[0][1] = (int) (this_size.width * y[0]);
coord[0][2] = (int) (this_size.width * z[0]);
coord[0][3] = (int) (this_size.width * x[0]);
coord[1][0] = (int) (this_size.height * x[1]);
coord[1][1] = (int) (this_size.height * y[1]);
coord[1][2] = (int) (this_size.height * z[1]);
coord[1][3] = (int) (this_size.height * x[1]);
getGraphics().drawPolygon(coord[0], coord[1], 4);
}
}
public class Sierpinski extends Frame implements WindowListener {
protected SierpinskiPicture canvas; // the picture itself
public Sierpinski() {
super.setTitle("Sierpinski gasket"); // set the window's title
this.addWindowListener(this);
// create the Sierpinski gasket
canvas = new SierpinskiPicture();
this.add(canvas); // add the canvas into the frame
this.pack(); // sets up the layout
}
public void windowOpened(WindowEvent e) { }
public void windowClosed(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }
public void windowClosing(WindowEvent e) {
this.dispose(); // destroy the window
}
public static void main(String[] args) {
Sierpinski f;
// create the dialog box and show it.
f = new Sierpinski();
f.show();
}
}
Explanation / Answer
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
//
// We define SierpinskiPicture as a window component. Its job is only to
// maintain a Sierpinski gasket in its contents.
//
// This is just a helper class. Note that it is not declared as
// `public'. This is because only one public class is allowed per file,
// and we want the Sierpinski application to be public here.
//
class SierpinskiPicture extends Canvas {
protected int gasket_depth = 5; // the depth to which the triangle is drawn
//
// getPreferredSize and getMinimumSize
//
// These methods tell whoever wants to know how big the component
// should be. We define both minimum and preferred sizes.
//
protected Dimension preferred_size = new Dimension(300, 300);
protected Dimension minimum_size = new Dimension(50, 50);
public Dimension getPreferredSize() {
return preferred_size;
}
public Dimension getMinimumSize() {
return minimum_size;
}
//
// paint
//
// This is called every time a redraw is called for, basically
// whenever the window is deiconified, uncovered, or resized.
// In this case we just redraw the entire gasket every time.
//
public void paint(Graphics g) {
double[] x = new double[2];
double[] y = new double[2];
double[] z = new double[2];
x[0] = 0.0; x[1] = 0.0;
y[0] = 1.0; y[1] = 0.0;
z[0] = 0.5; z[1] = 1.0;
drawTriangle(x, y, z);
drawGasket(gasket_depth, x, y, z);
}
//
// drawGasket
//
// Draws a Sierpinski gasket using multiple calls to drawTriangle.
// The gasket consists of <depth> sizes of triangles, bounded by
// the points represented by <a>, <b>, and <c>. Each point is
// an array of two real numbers, the x-coordinate followed by the
// y-coordinate. Both coordinates will be between 0 and 1.
//
public void drawGasket(int depth,
double[] a, double[] b, double[] c) {
double[] abmid = new double[2];
double[] bcmid = new double[2];
double[] acmid = new double[2];
int i;
if(depth == 0) {
return;
} else {
for(i = 0; i < 2; i++) {
abmid[i] = (a[i] + b[i]) / 2.0;
bcmid[i] = (b[i] + c[i]) / 2.0;
acmid[i] = (a[i] + c[i]) / 2.0;
}
drawTriangle(acmid, bcmid, abmid);
drawGasket(depth - 1, a, abmid, acmid);
drawGasket(depth - 1, abmid, b, bcmid);
drawGasket(depth - 1, acmid, bcmid, c);
}
}
//
// drawTriangle
//
// Draws a triangle on the canvas between the points <x>, <y>, and
// <z>.
//
public void drawTriangle(double[] x, double[] y, double[] z) {
int[][] coord = new int[2][4];
int i;
Dimension this_size = this.getSize();
coord[0][0] = (int) (this_size.width * x[0]);
coord[0][1] = (int) (this_size.width * y[0]);
coord[0][2] = (int) (this_size.width * z[0]);
coord[0][3] = (int) (this_size.width * x[0]);
coord[1][0] = (int) (this_size.height * x[1]);
coord[1][1] = (int) (this_size.height * y[1]);
coord[1][2] = (int) (this_size.height * z[1]);
coord[1][3] = (int) (this_size.height * x[1]);
getGraphics().drawPolygon(coord[0], coord[1], 4);
}
}
//
// The Sierpinski object is a very simple object which simply holds
// a SierpinskiPicture.
//
// All GUI Java applications should be subclasses of the Frame object,
// as this one is.
//
// Note that this is defined as ``implementing'' WindowListener.
// This means that this object will include several methods defining
// what should be done in the case of the window being opened, closed,
// resized, etc.
//
public class Sierpinski extends Frame implements WindowListener {
protected SierpinskiPicture canvas; // the picture itself
//
// Sierpinski
//
// Initializes the dialog box
//
public Sierpinski() {
super.setTitle("Sierpinski gasket"); // set the window's title
// set myself up for listening to WindowEvents (because
// I want to know when the window is closed)
this.addWindowListener(this);
// create the Sierpinski gasket
canvas = new SierpinskiPicture();
this.add(canvas); // add the canvas into the frame
this.pack(); // sets up the layout
}
//
// The following methods are for handling WindowEvents that I might
// receive (since I registered myself as a WindowListener). The
// only one I really care about is windowClosing, but all of
// them must be defined. In the case of windowClosing, I want
// the application to exit altogether.
//
public void windowOpened(WindowEvent e) { }
public void windowClosed(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }
public void windowClosing(WindowEvent e) {
this.dispose(); // destroy the window
}
//
// main
//
// All this does is to create a new Sierpinski object which will
// take everything over.
//
public static void main(String[] args) {
Sierpinski f;
// create the dialog box and show it.
f = new Sierpinski();
f.show();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.