How can you get reference to PConstants in sketch's .java class?

I have this problem, of which my cannot get reference to CENTER and GROUP. I have tried to implements PConstants but it does not make my sketch working.

What should I do?


  • edited October 2015

    Use import static for the class processing.core.PConstants: :ar!
    import static processing.core.PConstants.*;

    Also using implements processing.core.PConstants works as well!

  • Does not working. I am using Processing 2.2.1 by the way.

  • Try

    import processing.core.PConstants;

  • Hei quark, your solution was in my first screenshot. It says that The function shapeMode(int) does not exist.

  • edited October 2015

    You have PContants it should be PConstants, you are missing an 's' and you don't need the static keyword so again it should be

    import processing.core.PConstants;

  • edited October 2015 Answer ✓

    Now that I've taken more time to pay attention, your problem got nothing to do w/ PConstants! =P~

    You're trying to access sketch's "canvas" w/o its PApplet's reference! X_X
    Any classes accessing the "canvas" needs to request its owner's reference somehow.
    Usually right at their constructors and storing it in some field afterwards:

    "" tab file:

    import processing.core.PApplet;
    import static processing.core.PConstants.*;
    public class ForeignJavaClassNeedsCanvas {
      final PApplet p;
      public ForeignJavaClassNeedsCanvas(PApplet p5) {
        p = p5;
      public void iCanzP5() {
        p.background((int) p.random(0xff000000));
        p.rect(p.width>>1, p.height>>1, p.width>>2, p.height>>2);
        PApplet.print("ICanzP5!!!", '\t');

    Testing "some.pde" tab file:

    // how-can-you-get-reference-to-pconstants-in-sketch-s-java-class
    // GoToLoop (2015-Oct-14)
    final ForeignJavaClassNeedsCanvas hey = new ForeignJavaClassNeedsCanvas(this);
    void setup() {
      size(800, 600);
    void draw() {
    void keyPressed() {
    void mousePressed() {
  • If it still doesn't work then replace ALL 3 imports with

    import processing.core.*;

    which will make all classes and interfaces in processing.core available to you.

  • It is working! Thanks quark and GoToLoop for helping ^:)^ .

    I have been using Processing for a year and never experience this. Am I missing something? And why suddenly I need to put suffix f (example. 0.01f) in every float value in but not int the .pde file?

  • edited October 2015 Answer ✓
    • All ".pde" tab files are merged & pre-processed before becoming 1 valid ".java" file.
    • Those include suffixing all literals which happen to have a . or an e in them w/ an f.
    • For example: .5 becomes .5f, 3. becomes 3.f, 1e3 becomes 1e3f, etc.
    • Of course when some literal is already suffixed, the pre-processor won't do anything!
    • All fields & methods & constructors w/o an explicit access keyword are prefixed w/ public.
    • # becomes 0xff.
    • Keyword color becomes int.
    • Those are the 1s I remember right now though. ~O)
  • Thanks man really helpful.

    One last question, why P2D is slower than the default renderer? When I run the P2D sketch, it goes to grey blank screen before showing the sktech. Is there anyway I can make my P2D sketch run faster?

  • Answer ✓

    Java has 2 floating point number types float and double

    In Java the value 5.0 is treated as a double to make it a float we need to add the suffix i.e. 5.0f

    In .pde tabs the Processing pre-processor adds the 'f' suffix for you.

    So the statement
    float x = 5.0;
    is OK in a ,pde tab but not OK in a .java tab

  • Hei quark, your solution was in my first screenshot. It says that The function shapeMode(int) does not exist.

    Unfortunately the error message is not related to PConstants but everything else you write is about PConstants no wonder GoToLoop and I started down the wrong track.

  • edited October 2015

    Worth reading, somewhat related to your issue too, but on Eclipse IDE: O:-)

  • There is another thing why I think I never have this problem before.

    The class here is a .java file. If I want to make a Processing pre - compiled object, I can just make my class in .pde. Hence, I do not need to worry about importing Processing internal libraries, adding suffix f in float, and refering PApplet as class argument.

Sign In or Register to comment.