1

I want to use the StdDraw package, and I've tried many different ways of importing it.

Of course I tried:

import StdDraw;

But, when I look at the documentation from Princeton here, it shows that StdDraw is part of Objects, so I try this:

import java.lang.Object.StdDraw;

However, this results in an error:

error: cannot find symbol in
import java.lang.Object.StdDraw;

I saw this question here but it does not answer this question either.

How do I import StdDraw? thank you.

Community
  • 1
  • 1
makansij
  • 9,303
  • 37
  • 105
  • 183
  • 1
    FYI, the documentation means that StdDraw extends Object. That is, it's a special kind of Object. All classes in Java extend Object implicitly! Little bit more info here ( http://stackoverflow.com/questions/19114997/why-does-every-object-in-java-implicitly-extend-java-lang-object-class ) if you're interested. – Luke Sep 04 '15 at 07:11

2 Answers2

1

if you want to use StdDraw you must have

  • either the sources
  • or the classes (best zipped up as jar)

as preferred way you use the sources (see http://introcs.cs.princeton.edu/java/15inout/). it says there "To use these libraries, download StdIn.java, StdOut.java, StdDraw.java, and StdAudio.java into the same directory as your program. "

once you did this the imports should be working.

NOTE: all four files are not in packages, so you should 'download' them into the 'standard' package. That means you have to download them to the root package of your project.

by the way: don't import import java.lang.Object.StdDraw; but do just import import StdDraw;

Martin Frank
  • 3,445
  • 1
  • 27
  • 47
  • How do I know what is the root package of my project? There is only one folder for all of the .java files, and I copied those files into that folder. It still doesn't recognize them. – makansij Sep 04 '15 at 05:57
  • 1
    you use the wrong import, adapt it - i've adjusted my answer! – Martin Frank Sep 04 '15 at 06:17
  • 1
    @Sother you can't import classes from the default package. If you want to use packages, you need to use a sub-directory which is the name of the package. – Peter Lawrey Sep 04 '15 at 07:09
  • @MartinFrank, either way, the import gets the same error - `cannot find symbol`. – makansij Sep 04 '15 at 16:59
  • @PeterLawrey are you saying that I should change the name of my sub-directory to reflect that of the package? Confused about what you mean... I tried using `package ;` in all of the files, and then compiling one level above using `javac /*.java`, but this doesn't help. Still results in the `cannot find symbol` errors. – makansij Sep 04 '15 at 17:03
  • I suggest you use an IDE to setup and edit your program. This will make it easier to layout your project correctly and run it with a push of a button. – Peter Lawrey Sep 04 '15 at 17:50
  • An IDE hides a lot of important details that I want to learn. Hence, the question. – makansij Sep 05 '15 at 23:08
0
  1. First of all check encoding of your IDE. It should be set to UTF-8. It is important if you are using MS Windows operating system.

  2. Then create StdDraw.java class in the same package as the package of your program you are writing. Remove class declaration, leave only package declaration.

  3. Then visit this page: https://introcs.cs.princeton.edu/java/stdlib/StdDraw.java .

  4. Copy all its contents (Ctr-A, Ctrl-C) and then paste it into StdDraw.java file you created previously.

  5. StdDraw.java has its own main method so try to run it in order to check that the library works correctly. You should see a window with four strange figures :) .

  6. Don't touch StdDraw.java anymore. Now you can easily import StdDraw library and refer to its methods with name of the class.

Enjoy