All Available info
I need help with java? - Printable Version

+- All Available info (http://allavailable.info)
+-- Forum: Programming and Web Development (/forum-51.html)
+--- Forum: General programming forums (/forum-63.html)
+--- Thread: I need help with java? (/thread-338864.html)



I need help with java? - AVI - 04-11-2014

I am having serious problems with both affine transformations and timers. I have tried to figure both out on my own and utterly failed. I am only writing the code for myself, so there is no cheating here. I just want to see it done. It is a program that should generate a simple fractal comprised of black and white squares being placed on top of each other. Please take my code and do two things...
1. make each white square turn 45 degrees
2.Make a four second delay between the creation of each new square, except for the creation of the first square.

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.math.*;
import java.awt.geom.Rectangle2D;
import javax.swing.Timer;
import java.awt.geom.AffineTransform;
import javax.swing.Timer;
public class fractal extends JPanel {
Timer timer;
public void paint(Graphics g) {
double lineLength = 400; //sidelength of the original square, will be used for each square
double layer; //iteration of for loop
Graphics2D g2d = (Graphics2D) g;
for(layer = 1; layer < 40; layer=layer+1){
lineLength = lineLength/Math.sqrt(2);//makes the sidelength of the next square smaller
Rectangle2D.Double rectangle = new Rectangle2D.Double(250-(lineLength/2),250-(lineLength/2),lineLength,lineLength);//creates a smaller square that if turned 45 degrees will touch the midpoints of the previous square
if(layer%2 == 1){//odd iterations will be black
g2d.setColor(Color.black);
}
if(layer%2 == 0){//even iterations will be white
g2d.setColor(Color.white);
}
g2d.fill(rectangle);
}
}

public static void main(String[] args) {

JFrame frame = new JFrame("Fractal");
frame.add(new fractal());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

Please don't make suggestions on what I should do, as I am a beginner and it never helps. Most of the vocabulary that people use goes way over my head and I can almost never use suggestion unless they directly work when copied into my code(which never happens).
Please put in comments though.

Thankyou very much.
In case its not completely and utterly clear, I am asking you to actually edit the code and make it work. Do not give an answer asking me to clarify if you will not edit your answer after I have done so.


- Shawn - 04-11-2014

bellow is the link to your answer. if my current answer is not good enough update this question and i will update my answer.


good luck
don't forget to choose best answer, it shows you appreciate.