Help wanted, regarding JavaFX Tablet pressure!

Is there any method to get the tablet pressure on windows & Linux using any library using the FX2D (javafx) renderer in processing?

  • Mabye using the commandline it would be possible to get the pressure of the pen?
  • Using an extra library?
  • The main processing app & another java app that could just cast the pressure to the processing app using a server?

If you have any idea, please!
You might ask “Why javafx, use P2D?!” Simple, because openGl has weird artifacts regarding image rendering & rectangle drawing.

:slight_smile:
Thanks for reading!

1 Like

I would be interested in knowing how you are using FX2D to create graphics. A minimal reproducible example demonstrating the problem would help the rest of us have some idea of what you are trying to accomplish.

1 Like

I got pressure working in Kotlin by tapping into /dev/input/ on Linux. I can’t currently help porting this solution to Processing but maybe someone else could? Here’s my code:

1 Like

I don’t know what you mean by that, you don’t need any code from me to answer that question.

size(960,540,P2D);
rect(10,10,100,25,5);
//This will look bad in P2D
size(960,540,FX2D);
rect(10,10,100,25,5);
//This will look corrent in FX2D

That’s not what I’m talking about. The real problem as I see it is getting PenManager() to accept any parameter when FX2D is the renderer. It’s looking for an awt component and that’s just not available with JavaFX. Supposedly it does work with Swing components.

Addendum:
Hook your tablet up to this demo and see if it will measure pen pressure on the window (it does on my Mac). I’ll leave it to you to figure out how to draw on it. Note that PenManager() did accept a JPanel parameter.

import javafx.embed.swing.SwingNode;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import jpen.PenManager;
import jpen.PenEvent;
import jpen.event.PenAdapter;
import jpen.PLevel;
import jpen.event.*;
import jpen.*;

float pressure;

JFrame frame;
JPanel panel;

void setup() {
  println(FX2D);
  size(1, 1, FX2D);
  SwingNode swingNode = new SwingNode();
  SwingUtilities.invokeLater(() -> {
    frame = new JFrame();
    frame.setTitle("Pen Pressure JavaFX");
    frame.setBounds(100,100,600,600);
    panel = new JPanel();
    panel.setBounds(10, 10, 600, 600);
    println("panel = ", panel);
    PenManager pm = new PenManager(panel);
    println(pm);
    pm.pen.addListener(new ProcessingPen());
    swingNode.setContent(panel);
    frame.add(panel);
    frame.setVisible(true);
  }
  );

}

class ProcessingPen extends PenAdapter {
  public void penLevelEvent(PLevelEvent evt) {
    pressure = evt.pen.getLevelValue(PLevel.Type.PRESSURE);
    println("pressure = ", pressure);
  }
}

For me it doesnt want to start.
It says “cannot find anything named swing node”
What extra libraries have you installed?

Modules:

Try the following procedure:

  1. Create a folder inside of your sketch folder and name it ‘code’
  2. In your Processing folder find the ‘libraries’ folder
  3. There should be a folder called ‘javafx’ in the ‘libraries’ folder
  4. Inside the ‘javafx’ folder is another folder called ‘library’
  5. Inside of ‘library’ there should be a folder for your operating system, eg ‘linux-amd64’ or ‘linux-arm’
  6. Open the one that is for your system and look for another folder called ‘modules’
  7. When you open the ‘modules’ folder you should see the seven .jar files shown above.
  8. Copy/paste all seven of those .jar files into the ‘code’ folder inside of your sketch folder.
  9. Run the demo again.

The only other way to automatically get all of those modules (that I know of) is to edit the Processing editor source code and recompile it.

1 Like

Thank you! this really helped me! My only question is why is processing not automatically using all the libraries?

Because the folks who write our editor saw fit not to add all of them, for reasons I do not understand.

ah. And one last questio, Is it possible to get the pressure in JavaFx using a processing window (papplet). For example:

//import necessary libarys and create objects

void setup() {
   size(960,540,FX2D);
}

void draw() {
   //code that would get the pen pressure, without creating a new Panel
   //So i can draw objects like rectangles (rect) and also use the pressure
   //without the need of another window!
}

With my current knowledge I think getting pen pressure hinges on the successful implementation of PenManager, ie

PenManager pm = new PenManager(awt component)

If you can come up with a parameter to put inside the parentheses that allows the creation of ‘pm’ without throwing an exception then you can do what you’re trying to do. I’ve worked on this for over 24 hr. and have yet to find anything that will work for the setup that you want to use (default Processing window for JavaFX, ignoring stage, scene and all of that). There are a finite number of elements to a PSurfaceFX window and so far I can’t find one that will yield a clean ‘pm’. I was able to get the canvas for the window but that is a java.lang.object and not an awt component so it failed. I never was able to get the frame for PSurfaceFX so I don’t know if that will work or not.

The other option is to give up on using the JPen-2 library (which is where PenManager is defined) and figure out some other way to get the pressure. I use a Wacom tablet and they do have driver code for Windows and Mac, but no Linux code that I can find. With a Mac you can draw on several objects without any extra code as long as you trap mouse events, but I’ve never worried about line thickness and never bothered with pen pressure.

1 Like

I don’t think this looks too bad, and there is no bother with pen pressure:

import processing.javafx.*;

void setup() {
  size(960, 540, FX2D);
}

void draw() {
  if (mousePressed) {
    strokeWeight(3.0);
    line(pmouseX, pmouseY, mouseX, mouseY);
  }
}

Output:

Code for drawing into a JavaFX canvas:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.effect.BoxBlur;
import javafx.scene.shape.StrokeLineCap;
import javafx.scene.shape.StrokeLineJoin;
import javafx.stage.Stage;

class DrawCanvas extends Application {

  GraphicsContext gc;

  public void start(Stage stage) {
    Canvas canvas = new Canvas(1000, 500);
    canvas.setOnMouseDragged(e -> {
      gc.lineTo(e.getX(), e.getY());
      gc.stroke();
    }
    );
    canvas.setOnMousePressed(e -> gc.moveTo(e.getX(), e.getY()));
    gc = canvas.getGraphicsContext2D();
    gc.setLineCap(StrokeLineCap.ROUND);
    gc.setLineJoin(StrokeLineJoin.ROUND);
    gc.setLineWidth(1);

    BoxBlur blur = new BoxBlur();
    blur.setWidth(10);
    blur.setHeight(1);
    blur.setIterations(1);
    gc.setEffect(blur);

    Group root = new Group(canvas);
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
  }
}

DrawCanvas drawCanvas;

void setup() {
  size(1, 1, FX2D);
  surface.setVisible(false);
  drawCanvas = new DrawCanvas();
  Stage stage = new Stage();
  drawCanvas.start(stage);
}

Output looks a little better as far as line variation:

Thank you,
but do you know a way of transfering the pressure to a processing sketch? I need it to be processing, sorry for the inconvienience!

do you know a way of transfering the pressure to a processing sketch

No, not for your favorite FX2D renderer. There is no way to do that with JPen that I’ve ever been able to find (and I spent a lot of time looking). Hamoid says you can get the pressure data from /dev/input/ on Linux, but I never tried it; couldn’t find anything similar on Mac. Have you seen the app Krita?

Krita is in another programming language, right? Thanks for the help tho

Krita is in another programming language, right

No, Krita is a drawing program for digital artists: https://krita.org/en/

1 Like

yes i meant that, but the language isnt java, right?

No, it’s not for programming; it’s for drawing with your tablet.

And no! It is also not coded in java. It’s based on c++.

Yea, i know that. But then you can’t compare C++ and Processing / Java.