• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Variable is not declared?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to create a Flag class which will draw a flag and use it in a FlagComponent class to declare the draw positions and then see them in FlagViewer class


On my Flag class I get an error message when I declare my variables:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Line2D;

public class Flag
{
public Flag(int x, int y, int aWidth)
{
xLeft = x;
yTop = y;
width = aWidth;
}
...


"Cannot find symbol - variable width"


Why does this happen? Also, is it not possible to compile the class if the variables are "double" instead of "int"?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marco,

Welcome to JavaRanch!

Before you can set the value of a member variable in a constructor like this, the variable has to be declared in the class body; that's where you specify the type of the variable, like this:



About the "double" thing: you can't assign a double value to an int variable without a castl perhaps that's the problem you're having. In other words, this is illegal:

double d = 2.0;
int i = 0
i = d; // Error

but this is OK:

double d = 2.0;
int i = 0
i = (int) d;
 
Marco Segura
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh I see, apparently I forgot to declare it, but I've done this type of coding while declaring those variables at the end of the code, for example:



But I guess both ways its ok.


Well, now I have another problem, as I try to draw a rectangle I get an error message saying "cannot find symbol - constructor Rectangle(int, int, double, double)", here is what I typed:


[ October 10, 2008: Message edited by: Marco Segura ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler has problems with the arguments you pass to the constructor call. Take a look at the API documentation for the Rectangle class. You'll notice that there are many overloaded constructors for this class, but none of them takes a combination of int, int, double and double as arguments.
 
Marco Segura
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I managed my way around that. One other question...

Like I mentioned on my first post, I have 3 classes, 1 is the FlagViewer, another is the FlagComponent and the last one is the Flag class which draws the flags.

I have the default flag code in Flag class with its respective colors. On my FlagComponent class I tell the program to draw 2 more flags on different coordinates... I want to know how to change the colors of the flags, where do i put the code? in the FlagComponent class or the Flag class?



This is my code edited of the Flag class (if you need it);


[ October 10, 2008: Message edited by: Marco Segura ]
reply
    Bookmark Topic Watch Topic
  • New Topic