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 12 of 12

Thread: Sierpinski's Triangle in Graphics

  1. #1
    Junior Member Europa's Avatar
    Join Date
    Dec 2011
    Location
    Texas
    Posts
    9
    My Mood
    Grumpy
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question Sierpinski's Triangle in Graphics

    I've been tinkering with this for a while but I can't seem to get rid of those last two errors. Some help would be wonderful, thank you. ^_^

    This link has what it's supposed to look like, click on the Sierpinski button to see.
    Sierpinski Triangle

    Errors:

    Sierpinski.java:53: cannot find symbol
    symbol : variable dx
    location: class Sierpinski
    int x4 = x - dx / 2;
    ^
    Sierpinski.java:54: cannot find symbol
    symbol : variable dy
    location: class Sierpinski
    int y4 = y - dy / 2;
    ^
    2 errors

    import java.awt.*;
    import java.awt.Color;
    import javax.swing.JFrame;
    import java.awt.Polygon;
    import java.util.Random;
     
    public class Sierpinski extends Canvas
    {
        public void paint( Graphics z )
        {
        	Random flippin_tables = new Random();
     
    		for( int n = 1; n <= 50000; n = n+1 )
    		{
    			int x = 512;
    			int y = 382;
    			int x1 = 512;
    			int y1 = 109;
    			int x2 = 146;
    			int y2 = 654;
    			int x3 = 876;
    			int y3 = 654;
     
       			int r = flippin_tables.nextInt(255);
       			int g = flippin_tables.nextInt(255);
       			int b = flippin_tables.nextInt(255);
     
    			Color COOL = new Color(r,g,b);
    			z.setColor(COOL);
    			z.drawLine(x,y,x,y);
     
    			for( int q = 1; q <= 100000; q = q+1 )
    			{
    			}
    			int oh_MY = 1 + flippin_tables.nextInt(3);
     
    			if ( oh_MY == 1 )
    			{
    				int dx = x-x1;
    				int dy = y-y1;
    			}
    			if ( oh_MY == 2 )
    			{
    				int dx = x-x2;
    				int dy = y-y2;
    			}
    			if ( oh_MY == 3 )
    			{
    				int dx = (x-x3);
    				int dy = y-y3;
    			}
     
    			int x4 = x - dx / 2;
    			int y4 = y - dy / 2;
     
    			z.drawLine(x4,y4,x4,y4);
    			z.drawString("Sierpinski Triangle", 462,484);
    		}
     
    	}
     
        public static void main(String[] args)
        {
            JFrame win = new JFrame("Sierpinski");
            win.setSize(1024,768);
            win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            win.add( new Sierpinski() );
            win.setVisible(true);
        }
     
    }
    Last edited by Europa; February 17th, 2012 at 08:01 PM.
    I don't know what your problem is, but I bet it's hard to pronounce.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Sierpinski's Triangle in Graphics

    those last two errors.
    Please explain.
    Post the full text of the error messages here.

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting

  3. #3
    Junior Member Europa's Avatar
    Join Date
    Dec 2011
    Location
    Texas
    Posts
    9
    My Mood
    Grumpy
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Sierpinski's Triangle in Graphics

    I highlighted it and posted the errors. ^^'
    Last edited by Europa; February 17th, 2012 at 08:03 PM.
    I don't know what your problem is, but I bet it's hard to pronounce.

  4. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Sierpinski's Triangle in Graphics

    			if ( oh_MY == 1 )
    			{
    				int dx = x-x1;
    				int dy = y-y1;
    			}
    			if ( oh_MY == 2 )
    			{
    				int dx = x-x2;
    				int dy = y-y2;
    			}
    			if ( oh_MY == 3 )
    			{
    				int dx = (x-x3);
    				int dy = y-y3;
    			}
    First of all, I recommend using if-else-if here, not just a list of if statements (saves time).

    Now, here's your problem: the scope of a variable is limited to the block of code in which you define it. In other words, you define dx and dy inside the if statements' code, so they can't be used outside of those code blocks. The solution is simple: define dx and dy outside of the if-statements.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  5. The Following User Says Thank You to snowguy13 For This Useful Post:

    Europa (February 20th, 2012)

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Sierpinski's Triangle in Graphics

    Where are the variables: dx and dy defined? The compiler can not find them in scope where they are used in the code.

    You have many locally defined variables with those names. Their definitions go away when the code exits the surrounding {}s.
    Define the variables at the same level (within the same pair of {}s) as where you are trying to use them.

  7. The Following User Says Thank You to Norm For This Useful Post:

    Europa (February 20th, 2012)

  8. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Sierpinski's Triangle in Graphics

    I'm guessing lines 53/4 are these:

    int x4 = x - dx / 2;
    int y4 = y - dy / 2;

    The compiler message is saying that it has no clue what the variables dx and dy. Possible reasons are that you made a typo and the spelling is wrong (difficult with such small names - but still possible since you might have declared them as DX and DY), or that you didn't declare them at all.

    So go back and see how (and whether) you declared them.

    -----

    In most programming languages variables don't last for ever. And that's a good thing because it means we can reuse variables. But it does mean we have to worry about scope. The scope of a variable is just which parts of the code you can use the variable. Outside this region the variable is said to "go out of scope" and you will get the compiler's "Cannot find symbol" if you use that variable.

    In Java the scope is easy to determine: start from wherever the variable is declared, the scope will extend until the closing } of that block. Eg

    while(true) {
        int foo = 42;
     
        if(foo == 666) {
            System.out.println("???");
        } // <-- this } doesn't count because it matches the { above
     
        System.out.println(foo); // ok, foo is "in scope"
    } // <-- this } is the closing } of the block where foo is declared
     
    System.out.println(foo); // bad!: foo is "out of scope" aka "Cannot find symbol"

  9. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Europa (February 20th, 2012)

  10. #7
    Junior Member Europa's Avatar
    Join Date
    Dec 2011
    Location
    Texas
    Posts
    9
    My Mood
    Grumpy
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Sierpinski's Triangle in Graphics

    There are no errors but it's still not working! All that pops up are the words "Sierpinski's Triangle". Help again por favor?

    import java.awt.*;
    import java.awt.Color;
    import javax.swing.JFrame;
    import java.awt.Polygon;
    import java.util.Random;
     
    public class Sierpinski extends Canvas
    {
        public void paint( Graphics z )
        {
        	Random flippin_tables = new Random();
     
    		for( int n = 1; n <= 50000; n = n+1 )
    		{
    			int x = 512;
    			int y = 382;
    			int x1 = 512;
    			int y1 = 109;
    			int x2 = 146;
    			int y2 = 654;
    			int x3 = 876;
    			int y3 = 654;
     
       			int r = flippin_tables.nextInt(255);
       			int g = flippin_tables.nextInt(255);
       			int b = flippin_tables.nextInt(255);
     
    			Color COOL = new Color(r,g,b);
    			z.setColor(COOL);
    			z.drawLine(x,y,x,y);
     
    			for( int q = 1; q <= 100000; q = q+1 )
    			{
    			}
    			int oh_MY = 1 + flippin_tables.nextInt(3);
     
    			if ( oh_MY == 1 )
    			{
    				int dx = x-x1;
    				int dy = y-y1;
     
    				int x4 = x - dx / 2;
    				int y4 = y - dy / 2;
     
    				z.drawLine(x4,y4,x4,y4);
    				z.drawString("Sierpinski Triangle", 462,484);
    			}
    			else if ( oh_MY == 2 )
    			{
    				int dx = x-x2;
    				int dy = y-y2;
     
    				int x4 = x - dx / 2;
    				int y4 = y - dy / 2;
     
    				z.drawLine(x4,y4,x4,y4);
    				z.drawString("Sierpinski Triangle", 462,484);
    			}
    			else if ( oh_MY == 3 )
    			{
    				int dx = (x-x3);
    				int dy = y-y3;
     
    				int x4 = x - dx / 2;
    				int y4 = y - dy / 2;
     
    				z.drawLine(x4,y4,x4,y4);
    				z.drawString("Sierpinski Triangle", 462,484);
    			}
    		}
     
    	}
     
        public static void main(String[] args)
        {
            JFrame win = new JFrame("Sierpinski");
            win.setSize(1024,768);
            win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            win.add( new Sierpinski() );
            win.setVisible(true);
        }
     
    }

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Sierpinski's Triangle in Graphics

    it's still not working!
    What do you expect the program you have written to do?
    The only thing I see it doing is that it draws a String and a line in three different places.
    What is not working about that String and that line that is drawn?

  12. #9
    Junior Member Europa's Avatar
    Join Date
    Dec 2011
    Location
    Texas
    Posts
    9
    My Mood
    Grumpy
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Sierpinski's Triangle in Graphics

    I ashamed to say that although directions were given I've still managed to fail at this -->still very much a beginner. -_-'

    Here's the web page, there's a button there that you can click to see an example of the triangle. It looks like dozens of triforces put together into one giant triforce.

    Sierpinski Triangle
    I don't know what your problem is, but I bet it's hard to pronounce.

  13. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Sierpinski's Triangle in Graphics

    Do you have any specific questions about your assignment?

    One change you should make to your code is move move the drawing to after the the values of x4, and y4 are computed and only do it once and not three times, like you had in post #1.

  14. #11
    Junior Member Europa's Avatar
    Join Date
    Dec 2011
    Location
    Texas
    Posts
    9
    My Mood
    Grumpy
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Sierpinski's Triangle in Graphics

    I can't move the drawing because then it gives me an error because it can not find the variables dx and dy. The question I have is why it is not forming triangles like it's supposed to?
    I don't know what your problem is, but I bet it's hard to pronounce.

  15. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Sierpinski's Triangle in Graphics

    error because it can not find the variables dx and dy.
    Define the variables dx and dy in the same place you have defined all the other variables.

    why it is not forming triangles like it's supposed
    Where does your code form a triangle? I only see where it draws a line.

Similar Threads

  1. Graphics class NullPointerException Initialize Graphics Class??
    By bglueck in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 13th, 2011, 11:13 PM
  2. help me draw a triangle....
    By beandip408 in forum Object Oriented Programming
    Replies: 10
    Last Post: October 28th, 2010, 05:49 PM
  3. [HELP] TRIANGLE!
    By kramista in forum Loops & Control Statements
    Replies: 10
    Last Post: July 29th, 2010, 12:58 PM
  4. ASCII Triangle
    By physics in forum Loops & Control Statements
    Replies: 1
    Last Post: March 27th, 2010, 06:39 AM
  5. Triangle Question
    By Leeds_Champion in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 29th, 2009, 11:33 PM

Tags for this Thread