Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: g.drawLine doesn't draw line in for loop

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default g.drawLine doesn't draw line in for loop

    Hi!
    I am really new to Java. As an exercise I'm trying to make an applet that draws "Cantor Dust" (L-system - Wikipedia, the free encyclopedia - third example. I filled an array with integers named A and B
    and used g.drawLine to try to draw a line at a spot proportional to the position in the array for every element that was "A".

    It compiles, but in Firefox I just get a blank screen

    Any help or pointers greatly appreciated!

    Code:
    import java.awt.*;
    import java.lang.*;
    import java.util.*;
     
    public class CantorDust extends java.applet.Applet {
       static int A; //A and B are what the arrays are filled with. A = draw forward. B= move forward
       static int B;
       static int i; //i,j,k,l are "counters" in the for-loops.
       static int j;
       static int k;
       static int l;
       static int m; //m is just l+1. 
     
        static double iterations = 5.0;                           //#iterations
        static double sizeArray = Math.pow(3.0, iterations);      //Size of array is 3^#iter.      
        static int NsizeArray = (int)sizeArray;                   //convert double to int
        static int[] VDust = new int[NsizeArray];                 
        static int[] NDust = new int[NsizeArray];                //Declare two identical arrays
        static double lineSize;                                  //Size of line to draw
        static int NlineSize;                                    //same for int
     
        public void paint(Graphics g) {
     
            double lineSize = 600/NsizeArray;              //Line size - (for 5 iter should be 2.4)
    	int NlineSize = (int)lineSize;                       //convert to int.
    	for(i=0; i<iterations; i++)
    		for (j=1; j<NsizeArray;j++)      
    	     if (VDust[j]==A)
    		NDust[1+3*(j-1)]=A;                        //Fill NDust from VDust               
            	NDust[2+3*(j-1)]=B;                      //according to the rule A -> ABA
                    NDust[3+3*(j-1)]=A;
     
    	    if (VDust[j]==B)
    		NDust[1+3*(j-1)]=B;
    	        NDust[2+3*(j-1)]=B;                      //Fill NDust from VDust according
    	        NDust[3+3*(j-1)]=B;                   //to the rule B -> BBB
     
    	for (k=0;k<sizeArray;k++)                     //Form a new NDust by copying VDust
    	    VDust[k] = NDust[k];
    	for(l=0; l<sizeArray; l++)
    	  m = l++;
    	  if(NDust[l]==A)
    	      g.drawLine(l*NlineSize,20,(m)*NlineSize,20);   //For every A element in NDust 
    	  // if(NDust[l]==B) do nothing.("move forward")     //draw a line of length Nlinesize    
     
        }
    }
    Last edited by copeg; October 12th, 2010 at 06:09 PM. Reason: Please use the code tags


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: g.drawLine doesn't draw line in for loop

    Did you test this outside of a browser, say in AppletViewer? This will tell you if your code has any exceptions, which there are:
    Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: 244
    	at CantorDust.paint(CantorDust.java:29)

Similar Threads

  1. Won't Draw??
    By The_Mexican in forum Java Applets
    Replies: 4
    Last Post: March 13th, 2010, 06:00 PM
  2. lucky draw.. (pls help)
    By amin in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 20th, 2009, 11:30 PM
  3. [SOLVED] Trouble with draw and fillRect in pyramid logic using nested loop
    By LiquidMetal in forum Loops & Control Statements
    Replies: 4
    Last Post: April 27th, 2009, 03:25 AM
  4. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM