Embed processing 3 into swing - java

I'm trying to integrate Processing 3 into a swing application, but because PApplet doesn't extend Applet anymore I can't just add it as a component right away.
Is there anyway of embeding a Processing 3 sketch into Swing, it would be enough if I could just open the sketch in a seperate window without the PDE.

You can run a sketch from Java by extending PApplet and then using the runSketch() function to run that PApplet. It'll look something like this:
String[] args = {"MyPapplet "};
MyPapplet mp = new MyPapplet ();
PApplet.runSketch(args, mp);
public class MyPapplet extends PApplet {
public void settings() {
size(200, 100);
}
public void draw() {
background(255);
fill(0);
ellipse(100, 50, 10, 10);
}
}
Then if you want to get at the underlying component, you have to write code that depends on which renderer you're using. Here's how you'd do it with the standard renderer:
PSurfaceAWT awtSurface = (PSurfaceAWT)mp.surface;
PSurfaceAWT.SmoothCanvas smoothCanvas = (PSurfaceAWT.SmoothCanvas)awtSurface.getNative();
Once you have the SmoothCanvas, you can remove it from its frame and add it to yours.

Related

Processing 3 as a library for image processing

I'm currently working on image dynamic overlaying in java. My server will render images based on runtime parameters so I need a library to work with Images in a simple manner.
I've heard about Processing and curious about how to use it with my Spring boot server. Can I just use Processing as a Library without setup() draw() functions? Just run processing to load images, make operations on them and upload result in AWS S3 so end client will reach it?
I've tried to just use
import processing.core.PApplet;
import processing.core.PImage;
public class Application {
public static void main(String[] args) {
PApplet pApplet = new PApplet();
PImage pImage = pApplet.loadImage("/home/vadim/Pictures/lena.png");
PImage pImage2 = pApplet.loadImage("/home/vadim/Pictures/lena.png");
pImage.blend(pImage2, 0, 0, 50, 50, 0, 0, pImage2.width, pImage2.height, PImage.OVERLAY);
pImage.save("/home/vadim/Pictures/result.png");
}
}
Is it possible at all? or maybe I need to consider some another library for it?
As far as I know, you can't use Processing's functions as standalone without at least a setup() function. This is because Processing needs to do its own initialization before most of its functions work.
But note that you don't need to include a draw() function. You can do everything from setup(), something like this:
public class TestSketch extends PApplet {
public void setup() {
background(32);
ellipse(50, 50, 25, 25);
noLoop();
}
static public void main(String[] passedArgs) {
String[] appletArgs = new String[] { "TestSketch" };
PApplet.main(appletArgs);
}
}
That being said, you might be able to hack at Processing's source to isolate the functionality you want. This is probably more work than it's worth though.
Shameless self-promotion: here is a guide on using Processing as a Java library. Here is a guide on running Processing without a display.

Embed PApplet intp JApplet

How can I embed a PApplet into a JApplet ?
I wanted to add it to a JPanel inside the applet, but I couldn't.
If any of you know how I can do this. ??
As of Processing 3, you can no longer do this. PApplet no longer extends Applet, so it can't be treated as a component.
For 95% of users, this is okay. Applets are dead anyway, so you really shouldn't be using them. If at all possible, you should try deploying with Processing.js.
If you need to execute a Processing sketch from Java code, then you should use the PApplet.main("YourSketchNameHere"); function to launch it. Processing will take care of the window for you.
If you really need to treat a PApplet as a component, then you're going to have to go through its PSurface. The PSurface class contains a getNative() function that returns an object that can be treated as a component. But that's overkill for most Processing users.
Prior to Processing 3, this code should have worked for you as #Kevin has explained. So, if your question is directed towards understanding a legacy code here is what you will need to know:
import javax.swing.JFrame;
import javax.swing.JPanel;
class MyPApplet extends PApplet implements ActionListener{
#Override
public void setup() {
super.setup();
// setup
}
#Override
public void draw() {
// my draw code
}
}
public class PAppletDemo {
public static void main(String[] args) {
final JFrame frame = new JFrame("PApplet in Java Application");
JPanel panel = new JPanel();
//create an instance of your processing applet
final MyPApplet applet = new MyPApplet();
applet.init();
panel.add(applet); // From processing 3, this will give you error that applet is not a Component
frame.add(panel);
frame.setSize(applet.getSize().width, applet.getSize().height +200);
frame.setVisible(true);
}
}
To circumvent this, you will need to use PSurface getNative() function. Please refer to the example and discussion given on this link.

How to create more than one window of a single sketch in Processing?

I want to create two windows by using just one single sketch in Processing.
What I'm trying to do is that if I click a button in one window, then some image appear in another window.
I've searched Google and found some examples. Actually, I found the same question in this 'stack overflow web'. Here are the links.
Create more than one window of a single sketch in Processing
http://forum.processing.org/one/topic/multiple-windows-2-4-2011.html
Here is the codes of second links.
import java.awt.Frame;
PFrame f;
secondApplet s;
//f = new PFrame();
void setup() {
size(320, 240);
f = new PFrame();
}
void draw() {
background(255,0,0);
fill(255);
rect(10,10,frameCount%0,10);
s.background(0, 0, 255);
s.fill(100);
s.rect(10,20,frameCount%0,10);
s.redraw();
}
public class PFrame extends Frame{
public PFrame() {
setBounds(100,100,400,300);
s = new secondApplet();
add(s);
s.init();
show();
}
}
public class secondApplet extends PApplet {
public void setup() {
size(400, 300);
noLoop();
}
public void draw() {
}
}
But when I run this codes, I get the following error message at add(s);.
The method add(Component) in the type Container is not applicable for the arguments (multi_window_test.secondApplet)
Code of first comment of first link is similar, but when I run this code, I get the same error message.
Other example codes that I found are all similar. They all create PFrame class and secondApplet which extends PApplet. They said these codes works well but I can't run these codes.
I couldn't find the reason of my error message. Other people seems to have no problem when running this example code except me.
If someone knows the solution, please help me.
Also, if there is a other simple way to create multi-windows in one sketch, please let me know.
The reason for the error message is pretty self-explanatory: the add() function is expecting a Component, and PApplet is not a Component. This is because PApplet no longer extends Applet as of Processing 3, so old code that uses it as a Component will no longer work.
Instead, consider my answer to this question. Basically, just create a class that extends PApplet for your second window, and then call PApplet.runSketch() using that second PApplet as a parameter:
void setup() {
size(100, 100);
String[] args = {"TwoFrameTest"};
SecondApplet sa = new SecondApplet();
PApplet.runSketch(args, sa);
}
void draw() {
background(0);
ellipse(50, 50, 10, 10);
}
public class SecondApplet extends PApplet {
public void settings() {
size(200, 100);
}
public void draw() {
background(255);
fill(0);
ellipse(100, 50, 10, 10);
}
}

Create a video using processing opengl

I am creating a REST API to generate video dynamically based on user input and provided animation types. So, I am using processing 2.2.1 for this.
I want to generate 3D animation using OPENGL. But OPENGL requires Window object. Since I am using processing in background to just generate frame, how can I use processing with OPENGL to generate animated frames without interactive means without displaying the window.
My sample code
import com.hamoid.VideoExport;
import processing.core.*;
public class CircleSketch extends PApplet {
private VideoExport videoExport;
public void setup() {
size(400, 400,OPENGL);
videoExport = new VideoExport(this, "F:/work/tmp.mp4");
background(0);
}
public void draw() {
background(0);
fill(200);
rotateX(radians(50));
rectMode(CENTER);
rect(width/2,height/2, 100, 100);
videoExport.saveFrame();
}
}
DisplayFrame Class
public class DisplayFrame extends javax.swing.JFrame {
public DisplayFrame(){
this.setSize(600, 600); //The window Dimensions
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.JPanel panel = new javax.swing.JPanel();
panel.setBounds(20, 20, 600, 600);
processing.core.PApplet sketch = new CircleSketch();
panel.add(sketch);
this.add(panel);
this.setVisible(false);
//this is the function used to start the execution of the sketch
sketch.init();
}
public static void main(String[] args) {
new DisplayFrame().setVisible(false);
}
}
Any suggestion please???
how can I use processing with OPENGL to generate animated frames without interactive means without displaying the window.
In short terms: Given the current OpenGL driver models you can't (as long as you want to use a GPU for rendering). Period, that's how it is.
If you can live with software rendering (slow), then there's OSMesa. Also in the forseeable future there should be changes in the driver models, which allow to use OpenGL in a headless environment. Until then, you need a window of some sort on a graphics environment that actually actively feeds a display output (so it doesn't suffice to start a X11 server and background it).

Access dynamic component (i.e. new Label) in another class function?

I am currently working on a Swing Applet, and am having issues referencing my custom AWT Canvas component (very simple extended class) in other class functions, such as with any other component (i.e. button) normally created with Netbean (7.0)'s designer.
My custom canvas element I add here, I was sure this would be the appropriate place (especially after all other generated components were just created in the same area)
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
CustomCanvas myCan = new CustomCanvas();
myCan.setBounds(100, 100, 200, 200);
getContentPane().add(myCan);
...
However, unlike the generated components, I cannot access them by name and cannot seem to access them through other means (this.myCan) either. I have set up a sample function that will handle a (generated) button on the Swing form to manipulate the previously instantiated myCan component:
private void btnManipCanvasActionPerformed(java.awt.event.ActionEvent evt) {
//Essentially Was wanting to call something here such as myCan.getGraphics().setRect...
}
Do you know of a way to access myCan there? Am I supposed to place custom initializations of components in a different area so they can be publicly accessed?
Just make the canvas an instance variable, as (I guess) all the other components created by the Netbeans designer:
private CustomCanvas myCan;
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
myCan = new CustomCanvas();
myCan.setBounds(100, 100, 200, 200);
getContentPane().add(myCan);
// ...
there are possible issues or painting lacks because you probably mixing ATW Components with Swing JComponets,
if there nothing special that came from OpenGL, then look for JPanel instead of AWT Canvas and for all panting in Swing redirect everythigns to JLabel
please read how LayoutManagers works to avoids setBounds(int, int, int, int);

Categories