Enigmatic Wang
Enigmatic Wang

Reputation: 765

How to trace back methods to their classes then their packages? Java Syntax

I have been recently trying to utilize the Java API (http://docs.oracle.com/javase/7/docs/api/). I was wondering how I can locate the "page.drawRect" method. Is the "drawRect" method apart of the "page" class ? I'm not sure if that's how the syntax works, or if I'm making sense. All I want is an explanation of "page.drawRect" and maybe some tips on how to use the API documentation. Nothing is wrong with the actual code I posted. Thank you :)

import javax.swing.JApplet;
import java.awt.*;

public class HopeThisWorks extends JApplet
{

    public void paint(Graphics page)
    {
        page.drawRect(50, 50, 40, 40);
        page.drawRect(60, 80, 225, 30);
        page.drawOval(75, 65, 20, 20);
        page.drawLine(35, 60, 100, 120);

        page.drawString ("\"Don't try to be like Jackie. There is only one Jackie. " + 
        "Study computers instead.\"", 110, 70);
        page.drawString ("-Jackie Chan",130, 100);
    }
}

Upvotes: 0

Views: 266

Answers (4)

Reimeus
Reimeus

Reputation: 159874

drawRect a method in the Graphics class. Have a look at Graphics.drawRect().

page here is the variable instance name for the applet Graphics object.

For more see:

Painting in AWT and Swing

Upvotes: 1

mre
mre

Reputation: 44250

maybe some tips on how to use the API documentation

If you're using Eclipse,

  1. Right-click on your project and select Properties
  2. Select Javadoc Location
  3. Enter "http://docs.oracle.com/javase/7/docs/api/" in the Javadoc location path textfield
  4. Tap OK

Now you will be able to step into the source code, or simply hover over drawRect and read the associated documentation.

Upvotes: 1

Jayan
Jayan

Reputation: 18468

Other answers are good enough. Adding a small note.

Use an IDE like IntelliJ/Netbeans/Eclipse. Most of these tools has ability to take appropriate definition, show quick documentation etc.

Pragmatic Programmer Tip- Use one editor well

Upvotes: 1

Jack
Jack

Reputation: 133679

It's quite simple

  • packages are just named containers of class definitions and they don't contain code or methods, just .java files
  • methods can reside only in classes

In your example you are talking about the Graphic class, page is indeed a reference to a Graphic instance. So you should look for class java.awt.Graphic.

Whenever you import a package you don't need to specify the full qualified name of a class contained inside that package, that's why you can use plainly Graphic page instead that java.awt.Graphic page but this is just a shorthand to make everything less verbose (and sometimes more ambigue).

Upvotes: 3

Related Questions