• 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

Applet Frustration

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to write an applet that draws a face. Basically a filled circle, with drawn circles for eyes and nose. I haven't gotten to the mouth yet as I'm having some errors I can't seem to resolve.

In the line "Graphics2D g2 = (Graphics2D)g;" I'm getting "Cannot resolve symbol" and in the line "newFace.draw();" I'm getting "draw(java.awt.Graphics2D) in Face cannot be applied to ()" I've also got tons of other "Cannot resolve symbol" errors (I put comments where they appear as CRS, Symbol .

Here's the code:

FaceApplet.java


Face.java (class file)

[ February 24, 2005: Message edited by: Greg Roberts ]
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Cannot resolve symbol" means exactly what it says. The compiler cannot find the variable or method you are using. For example:

Where's the variable "g" declared? You have to declare it before you use it.
 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
g is not a variable. Its part of (Graphics2D)g. At least that's what I assumed. I'm not trying to use a variable called "g" in my program, I thought it was just part of using Graphics2D.
[ February 24, 2005: Message edited by: Greg Roberts ]
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Greg Roberts:
g is not a variable.



Trust me: Don't argue with the Java compiler. It's not going to change its mind.
You MUST declare variables before you use them. The statement:

is called a "type cast". It takes a variable of one type (g is supposed to be declared as type Graphics) and turns it into another type, in this case, Graphics2D.
What you are actually trying to do is override the method Applet.paint() which is inherited from Component. It's method signiture looks like this:

The "g" in that method signiture is what you are trying to manipulate.
What's more, since your method paint doesn't override Component's paint signiture (i.e. the signitures must be identical), your method would never get called by the JVM's display engine.
I strongly suggest that you work your way through the Java Tutorial before you strike out on your own. There's a lot to Java. Having a good foundation in the basics will save you a lot of pain.
 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trust me, I'm not striking out on my own here. This is a project for an introductory Java class. Here are the instructions for the project:


Project Requirements:
1. Write a program that draws a Face. There should be a Face.java file that contains the actual code for the Face Class. This Face class should have a method that looks like this:
public void draw(Graphics2D g)
{
... // much code here...
}
This will allow your Face class to draw itself. It should also have a constructor that looks like this:
public Face(double aFaceRadius, Color aFillColor)

This constructor will allow your Face to be drawn of any size and any color.

2. You should also have a FaceApplet.java file that extends the Applet class. This is the program that you will run and display an instance of your Face class
In this class you will have a:
a. paint method that will call the draw method of your face class.
b. constructor that will use the dialog boxes to get the radius and color of the face. (see ColorApplet.java pg 164 - 165 of Big Java).

3. You should also have a FaceApplet.html file that links to the Applet class using the <applet> tag. For example:
<applet code="FaceApplet.class" width="200" height="300"> </applet>

4. Your Face must have at least 4 components such as a set of eyes, a nose and a mouth with turn-up edges and a color. You can make it look however you wish, but try to make it interesting. Once you get the minimum requirements completed you may want to try adding ears or a hat etc.



Also, I updated my code and got it down to 1 error message. Here's the updated version:


 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a difference between a method declaration, for example:

and a method invocation, for example:

In the declaration we tell the compiler what the arguments of the method are, both the type and a variable name. In a method invocation we just pass the variable(s). Given that difference, there's something wrong with this method invocation (a constructor is a special type of method invocation):
 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, but I don't understand how that helps me. I don't fully understand the Graphics class, which I need to make this project work. Can you recommend a good tutorial on the Graphics class? I'll post my code again with the errors I'm getting.





[ February 26, 2005: Message edited by: Greg Roberts ]

[ February 26, 2005: Message edited by: Greg Roberts ]
[ February 26, 2005: Message edited by: Greg Roberts ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic