4

Editor: IntelliJ CE

What I want: Be able to write

setCanvas(500,500);

Instead of

StdDraw.setcanvas(500,500);

Problem: I can't figure out how to correctly import the Stddraw library. If i simply do

import StdDraw;

IntelliJ tells me "StdDraw" symbol cannot be resolved. If I comment it out I can call methods from StdDraw but I have to write StdDraw.setcanvas(500,500);

StdDraw.java is in the same directory as Solver.java.

Code:

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;
//    import StdDraw;//StdDraw is in the same directory as Solver

public class Solver {

    public static void main(String[] args) {
        System.out.println("Solver main is running.");

        StdDraw.setCanvasSize(500, 500);
        StdDraw.setPenColor(StdDraw.RED);
        StdDraw.filledRectangle(0,0,10,10);
     }
}

I've already tried: - Making sure Stddraw.java is in the same directory as the file I'm compiling and running - Looking at http://introcs.cs.princeton.edu/java/stdlib/javadoc/StdDraw.html - Searching for COMPLETE code examples, ie. code that shows how to import the library - Searching YouTube tutorials - Reading https://www.jetbrains.com/idea/help/library.html - Fiddling around with adding stuff in front of StdDraw, eg. stblib.StdDraw

Morgantuan
  • 41
  • 1
  • 1
  • 3
  • You'll want to do a static import, see this [question](http://stackoverflow.com/q/162187/269300), and http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html. – Patrick McLaren Mar 19 '15 at 03:30
  • When I do "import static StdDraw;" IntelliJ tells me: "java: '.' expected" on the line that I import StdDraw – Morgantuan Mar 19 '15 at 05:13
  • Move StdDraw to a different package, you can't do a static import from the default package, see this [bug report](http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4989710). – Patrick McLaren Mar 19 '15 at 05:15

5 Answers5

3

You need to add Stdlib to your local libraries of your java project. StdDraw is part of this Stdlib library.

  1. First you need to download the stdlib.jar file
  2. Then you create a folder in your java project (name it "lib")
  3. Copy & Paste the stdlib.jar inside the lib folder
  4. Open your java project with IntelliJ.
  5. Click File -> Project Structure -> Modules -> Dependencies
  6. Click on the + sign and choose Library -> Java
  7. Then you need to select your stdlib.jar inside your lib folder

Now you can use the StdDraw class. You don't need to import the class at the top of the file.

Community
  • 1
  • 1
Jozott
  • 2,367
  • 2
  • 14
  • 28
1

I use StdDraw all the time

Under your package declaration, type:

import stddraw.StdDraw;

then all the stuff you need to do should work, also make sure the actual class is inside of your file correctly

Barett
  • 5,826
  • 6
  • 51
  • 55
Ron
  • 11
  • 1
1

Add this import to your class.

import static StdDraw.*;

What it means is that all static methods of the StdDraw class can be used without prefixing them with StdDraw. It also assumes that StdDraw class is in the default package, which is generally frowned upon but appears to be what that library has done.

E-Riz
  • 31,431
  • 9
  • 97
  • 134
Amegar
  • 11
  • 1
  • This isn't enough information to fully answer the question. The OP needs to know: What is ``? – Barett Dec 02 '15 at 18:28
1

You said:


 What I want: Be able to write

     setCanvas(500,500);

 Instead of

     StdDraw.setcanvas(500,500);

Isn't that against the basic rules of Java?

You cannot write

    setCanvas(500,500);

unless you are inside the "StdDraw" class where other methods of the "StdDraw" class call the "setCanvas" method.

Otherwise, either you have to create an instance of the "StdDraw" class first:

    e.g. StdDraw stdDraw = new StdDraw();

and then use that instance to call the method:

    e.g. stdDraw.setCanvas(500,500); 

or you call the method this way:

    StdDraw.setcanvas(500,500);

This is the basic knowledge of Java, right?

By the way, if the "StdDraw" class is in the same directory as class "Solver", you don't have to import it to use it.

I use eclipse. I put class "StdDraw" in the same package with other classes. This way, I don't have to use the "import" key word to import "StdDraw". I just use the methods of "StdDraw" the static way. You import it only when it is not in the same package.

FYI: I'm reading Robert Sedgewick's "Algorithms", in which I've never seen any direct calls to methods like the way you want:

 uniform(N-i); or 
 printf("%.2f\n", x); or
 point(x0, y0); or
 line(x0, y0, x1, y1); or
 circle(x, y, r); or
 square(x, y, r); or
 polygon(x, y); etc. etc....

Instead, it's always:

 StdRandom.uniform(N-i); or 
 StdOut.printf("%.2f\n", x); or
 StdDraw.point(x0, y0); or
 StdDraw.line(x0, y0, x1, y1); or
 StdDraw.circle(x, y, r); or
 StdDraw.square(x, y, r); or
 StdDraw.polygon(x, y); etc. etc....

I hope this helps.

William Hou
  • 1,251
  • 14
  • 17
0

You can download the library stdlib.jar here: http://introcs.cs.princeton.edu/java/stdlib/

Then import it follow this tutorial:https://stackoverflow.com/a/32853178/2048865

Community
  • 1
  • 1