相关文章推荐
安静的便当  ·  ThingsBoard CE v2.4.0 ...·  3 周前    · 
近视的遥控器  ·  docker-compose ...·  1 年前    · 
慷慨大方的柚子  ·  node.js - Issue ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

You'll want to do a static import, see this question, and docs.oracle.com/javase/1.5.0/docs/guide/language/…. – Patrick McLaren Mar 19, 2015 at 3:30 When I do "import static StdDraw;" IntelliJ tells me: "java: '.' expected" on the line that I import StdDraw – Morgantuan Mar 19, 2015 at 5:13 Move StdDraw to a different package, you can't do a static import from the default package, see this bug report. – Patrick McLaren Mar 19, 2015 at 5:15

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

  • First you need to download the stdlib.jar file
  • Then you create a folder in your java project (name it "lib")
  • Copy & Paste the stdlib.jar inside the lib folder
  • Open your java project with IntelliJ.
  • Click File -> Project Structure -> Modules -> Dependencies
  • Click on the + sign and choose Library -> Java
  • 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.

    This isn't enough information to fully answer the question. The OP needs to know: What is <package>? – Barett Dec 2, 2015 at 18:28

    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.

    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

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.