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
Code java:
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);
}
}
Re: Sierpinski's Triangle in Graphics
Quote:
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
Re: Sierpinski's Triangle in Graphics
I highlighted it and posted the errors. ^^'
Re: Sierpinski's Triangle in Graphics
Code Java:
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. :)
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.
Re: Sierpinski's Triangle in Graphics
I'm guessing lines 53/4 are these:
Code :
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
Code :
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"
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?
Code java:
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);
}
}
Re: Sierpinski's Triangle in Graphics
Quote:
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?
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
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.
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?
Re: Sierpinski's Triangle in Graphics
Quote:
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.
Quote:
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.