How to import stddraw correctly?

Morgantuan picture Morgantuan · Mar 19, 2015 · Viewed 32.9k times · Source

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

Answer

Jozott picture Jozott · Nov 19, 2019

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.