test renderer ?

Hello I try to test the renderer, but it's not accessible. there is an other way to know the rendering mode ? void setup() { size(400,400, P3D) ; if (renderer.equals(P3D)) println("P3D") ; else println("2D" ) ; }

Answers

  • You already know what the renderer is because you use it in size, but this will work if you want to find out in other methods

    // Set the renderer to use
    String renderer = P3D;
    
    void setup() {
      size(400, 400, renderer) ;
      if (renderer.equals(P3D)) 
        println("P3D") ; 
      else if (renderer.equals(P2D)) 
        println("P2D" ) ;
      else if (renderer.equals(JAVA2D)) 
        println("JAVA2D" ) ;
    }
    
  • edited December 2015 Answer ✓

    https://forum.Processing.org/two/discussion/8045/how-to-format-code-and-text

    // forum.Processing.org/two/discussion/14093/test-renderer
    // GoToLoop 2015-Dec-21
    
    void setup() {
      size(400, 300, OPENGL);
      println(getRendererName());
      exit();
    }
    
    String getRendererName() {
      final PGraphics graph = getGraphics(); // can be replaced by g.
    
      if (graph instanceof processing.awt.PGraphicsJava2D)   return JAVA2D;
      if (graph instanceof processing.javafx.PGraphicsFX2D)  return FX2D;
      if (graph instanceof processing.opengl.PGraphics2D)    return P2D;
      if (graph instanceof processing.opengl.PGraphics3D)    return P3D;
    
      return PDF;
    }
    
  • edited December 2015 Answer ✓

    A better alternative based on forName() & isInstance() from Class: :-bd

    1. http://docs.Oracle.com/javase/8/docs/api/java/lang/Class.html#forName-java.lang.String-
    2. http://docs.Oracle.com/javase/8/docs/api/java/lang/Class.html#isInstance-java.lang.Object-

    // forum.Processing.org/two/discussion/14093/test-renderer
    // GoToLoop 2015-Dec-21
    
    void setup() {
      size(400, 300, OPENGL);
      println(getRendererName());
      exit();
    }
    
    String getRendererName() {
      final PGraphics graph = getGraphics(); // can be replaced by g.
    
      try {
        if (Class.forName(JAVA2D).isInstance(graph))  return JAVA2D;
        if (Class.forName(FX2D).isInstance(graph))    return FX2D;
        if (Class.forName(P2D).isInstance(graph))     return P2D;
        if (Class.forName(P3D).isInstance(graph))     return P3D;
        if (Class.forName(PDF).isInstance(graph))     return PDF;
        if (Class.forName(DXF).isInstance(graph))     return DXF;
      }
    
      catch (ClassNotFoundException ex) {
      }
    
      return "Unknown";
    }
    
  • edited December 2015

    A bad advise but a very curious Java case... =;
    In @quark's example, since field renderer is a known compile literal, we don't need equals() for comparison against another String constant.
    Exceptionally we may choose risking on going straight w/ == equality operator for such cases: :ar!

    String renderer = P3D;
    
    void setup() {
      size(400, 300, renderer);
    
      if      (renderer == P3D)     println("P3D") ; 
      else if (renderer == P2D)     println("P2D");
      else if (renderer == JAVA2D)  println("JAVA2D");
      else if (renderer == FX2D)    println("FX2D");
    
      exit();
    }
    
  • edited December 2015 Answer ✓

    Nonetheless, it's still too risky to use == to compare String constants.
    However, switch () {...} / case: ... is a gr8 replacement since it relies on equals() internally: :bz

    // forum.Processing.org/two/discussion/14093/test-renderer
    // GoToLoop 2015-Dec-21
    
    //static final String RENDER = OPENGL; // Known compile time constant.
    static final String RENDER = new String(OPENGL); // Forcing unique instance!
    
    void setup() {
      size(400, 300, RENDER);
      println(stringfyRenderer(RENDER));
      exit();
    }
    
    static final String stringfyRenderer(String render) {
      switch (render) {
        case JAVA2D: return "JAVA2D";
        case FX2D:   return "FX2D";
        case P2D:    return "P2D";
        case P3D:    return "P3D";
        case PDF:    return "PDF";
        case DXF:    return "DXF";
        default:     return "Unknown";
      }
    }
    
  • edited December 2015 Answer ✓

    An updated & more flexible version to deal w/ createGraphics() instances too: :D

    // forum.Processing.org/two/discussion/14093/test-renderer
    // GoToLoop 2015-Dec-21
    
    void setup() {
      size(400, 300, OPENGL);
      println(getRendererName(getGraphics())); // can be replaced by g.
      println(getRendererName(createGraphics(width>>1, height>>1, FX2D)));
      exit();
    }
    
    static final String getRendererName(final PGraphics graph) {
      try {
        if (Class.forName(JAVA2D).isInstance(graph))  return JAVA2D;
        if (Class.forName(FX2D).isInstance(graph))    return FX2D;
        if (Class.forName(P2D).isInstance(graph))     return P2D;
        if (Class.forName(P3D).isInstance(graph))     return P3D;
        if (Class.forName(PDF).isInstance(graph))     return PDF;
        if (Class.forName(DXF).isInstance(graph))     return DXF;
      }
    
      catch (ClassNotFoundException ex) {
      }
    
      return "Unknown";
    }
    
  • Whaouuuuu totaly answering :) Thx a lot Processing brothers !

  • edited December 2015 Answer ✓

    Glad you've liked it, @Stanlepunk. B-)
    However I've forgotten about SVG renderer. And b/c both PDF & SVG are subclasses of JAVA2D, PDF renderer was returning as if it was JAVA2D! b-(
    But for sake of correctness, here's a more correct behavior fix. Plus some speed tweak checks: :D

    // forum.Processing.org/two/discussion/14093/test-renderer
    // GoToLoop 2015-Dec-22
    
    import processing.pdf.PGraphicsPDF;
    import processing.svg.PGraphicsSVG;
    import processing.dxf.RawDXF;
    
    void setup() {
      size(400, 300, OPENGL);
      println(getRendererName(getGraphics())); // can be replaced by g.
      println(getRendererName(createGraphics(width, height, JAVA2D)));
      println(getRendererName(createGraphics(width, height, FX2D)));
      println(getRendererName(createGraphics(width, height, P2D)));
      println(getRendererName(createGraphics(width, height, PDF)));
      println(getRendererName(createGraphics(width, height, SVG)));
      println(getRendererName(createGraphics(width, height, DXF)));
      exit();
    }
    
    static final String getRendererName(final PGraphics graph) {
      if (graph.displayable()) {
        if (graph.isGL())  return graph.is2D()? P2D : P3D;
        return graph instanceof processing.awt.PGraphicsJava2D? JAVA2D : FX2D;
      } else if (graph.is3D())  return DXF;
    
      else try {
        if (Class.forName(PDF).isInstance(graph))  return PDF;
        if (Class.forName(SVG).isInstance(graph))  return SVG;
      } catch (final ClassNotFoundException cause) {
        System.err.println(cause);
      }
    
      return "Unknown";
    }
    
  • thanks for the improvement !!!! very nice code

Sign In or Register to comment.