Solved

Kindergarten Java question

Posted on 2002-05-12
7
260 Views
Last Modified: 2013-11-23
Please help me understand basic graphics / displays.

I feel kind of silly asking this.
But, any of the books I've got on Java do not explicitly tell me what it is I am asking myself about this Graphics stuff.

And this is something very basic.

All the books I've got just show me the line of code, and then USE the line of code, but since I've begun using java a little... NONE of my texts explain what this line means.  So, I feel like I do NOT understand it.
Yes, I can make stuff print out... and even manipulate output.
But, I don't understand that line.

Please interpret the following code(in english):
<yes, use pretty colored chalk if necessary>

 Graphics2D screen2D = (Graphics2D) screen;

 !!!

I understand that,
   public void paint(Graphics screen) {

is a paint method <that returns no value> in an applet.
And I understand that the paint method is creating a "Graphics object" named "screen".

Then:
I understand you are creating a "Graphics2D object" named "screen2D"...

But:
HOW do you interpret (in english please) what the right side of the equals sign means???

Please spell out the meaning of that line to me, as if you were a Java/Kindergarten teacher and HAD to draw a map... (And then had to LEAD the blind guy anyway).

If you can somehow make me understand these objects.
What I am creating (when I do this).
And what I am DOING (when I do this).
And maybe where values, graphics, printable stuff flows through these objects... I'll be very thankful.

<thank you>

Sincerely




0
Comment
Question by:stvrich
  • 4
  • 2
7 Comments
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 7004444
First, you need to know that class Graphics2D is a subclass of Graphics class.
The right side of the equal sign shows that you are explicitly casting a Graphics object called "screen" to a Graphics2D object. Why do we need this? This is because we want to access some member functions of Grahpics2D class which is not defined in the Graphics object. If we directly call the functions using the Graphics object "screen", then there is compilation error.

Since you are creating a new Graphics2D object by casting an existing Graphics object "screen", so the default configuration of the Graphics2D object will be the same as the object "screen".

Hope you understand. IF not, please feel free to ask.
0
 
LVL 6

Accepted Solution

by:
Holger101497 earned 100 total points
ID: 7004859
> I understand that,
> public void paint(Graphics screen) {
> is a paint method <that returns no value> in an applet.

Correct.
(mostly - see below)

> And I understand that the paint method is creating a "Graphics object" named "screen".

NOT correct!
That's one important factor: "Just creating" a Graphics-object wouldn't help you much (and that's not even very easy (go ahead and try ;-))
The "Graphics object called 'screen'" already exists and is PASSED to this function as a parameter. This graphics object represents the surface of your applet. I don't care about the tiny little details, but I'd say it's created by the browser and passed to the Virtual machine running your applet (possibly the other way around - doesn't make any difference as long as the effect is the same :-)

> Then:
> I understand you are creating a "Graphics2D object" named "screen2D"...

NOT correct.
Well... partly correct? You're not creating a new object at all in that line! You're just creating a new variable which points to the same object you alreday have. It's telling the compiler that you want to use the "screen" parameter as a variable of type "Graphics2D" from now on.
Now you wonder why the parameter does not have the correct ("real") type in the first place? The reason is compatibility. The "paint(...)" method has been a port of applets (API) forever - even before Graphics2D existed. Note that your code will not run in older browsers (virtual machines)

Now what are you doing and how does it work? As i said, this object was created "somewhere else" and represents the part of the screen that is used by your applet. If you call this object's methods to draw lines (or shapes/text or whatever), it translates your commands to little colored dots *g* (sorry I don't have colored chalk). So: your method does not reaturn a "value" as a new variable, but it returns a LOT of information in the object that it received and manipulated!

The "creator" of this object gives it to you(r method) to do stuff with it. When your method is done, the "owner" checks back what you did  (checks the contents/variables of the Graphics object) and displays these little colored dots on the screen :-)

HTH,
        Holger
0
 
LVL 1

Author Comment

by:stvrich
ID: 7008762
Hi Guys,

I've been busy lately.
But, I'm reading over your suggestions now.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:stvrich
ID: 7008788
Yes... !

This is precisely the kind of feedback I need to understand this.

(Obviously I'm still absorbing fundamental concepts, and... )

I'm thankful for your help.

Give me a Day or two to find some time to absorb what you've said... But, I think, Holger, that What you've described to me is exactly the kind of explanation I need.
0
 
LVL 1

Author Comment

by:stvrich
ID: 7008818
Yes... !

This is precisely the kind of feedback I need to understand this.

(Obviously I'm still absorbing fundamental concepts, and... )

I'm thankful for your help.

Give me a Day or two to find some time to absorb what you've said... But, I think, Holger, that What you've described to me is exactly the kind of explanation I need.
0
 
LVL 1

Author Comment

by:stvrich
ID: 7018072
A have Several <big name> Java books available to me to consult (Sun's -core Java-, Java Tutorial, Java/24 hours, and a couple of certification guides... etc).  And NONE of them have explicitly explained that statment as well as it just was explained here.

It's enough info, that a ~new~ Newbie, like me can be pointed in the right direction, connect the dots, and see what the statement REALLY means, and continue Self Educating.

Thank you very much for your help.

Sincerely
0
 
LVL 6

Expert Comment

by:Holger101497
ID: 7019533
Glad I could help :-))

> A have Several <big name> Java books available to me to consult (...etc).
> And NONE of them have explicitly explained that statment as well as it just was explained here.

Possibly, you're looking at the wrong chapter :-)
If you really want to "learn" Java and not just see how you can copy and modify an applet in one hour, you should take a look at the chapters about variables, passing parameters, object-oriented programming (i.e. classes, subclassing and typecasting).
This will be a few day's reading before you really start programming.
Some books, especially the "Get started quickly"-thingies, tend to explain these important things last (or not at all...)

I didn't have that much trouble getting started in Java because I already knew so much about these things from programming Delphi. THERE, however, I remember reading a good 200 or 300 pages in the manuals. They had a really good introduction "from the very beginning". I guess that's the difference between an expensive development tool (luckily much less expensive for students :-) and an essentially "free" language like Java...

Back to the topic: I think programming applets is NOT such a good way of learning Java. Because of the interaction with the browser, much of what's "really going on" is hidden...
Much less entertaining, but much more helpful: write a few little standalone-applications. Try simple one that print messages to the console first and then try getting started with the AWT or even Swing. When you understand these, applets will suddenly be MUCH less confusing (you could call it "simple" then :-))

Good luck,
               Holger
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now