• Check out world news on HeadLinesList.com
  • Forum registration is now open for public.
Hello There, Guest! Login

Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need help with java?
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
if(layer%2 == 0){//even iterations will be white

public static void main(String[] args) {

JFrame frame = new JFrame("Fractal");
frame.add(new fractal());
frame.setSize(500, 500);

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.


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.



Forum Jump:

1 Guest(s)