Processing 3 as a library for image processing - java

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.

Related

How do intelligent Agents work with data base?

I am working with JADE framework and I want to know is there any way for intelligent agents to work with some kind of data base, where they can read from it and write some information?..
I tried to make a connection between excel (using jxl) and my project but there is a problem: below is the code for writing in excel file:
public static void write(String[] args) throws Exception {
// TODO code application logic here
File f = new File("C:\\Users\\Mastisa\\Desktop\\Master.xls");
WritableWorkbook Master = Workbook.createWorkbook(f);
WritableSheet History_Table = Master.createSheet("History_Table", 0);
Label L00 = new Label (0,0,"RUN#");
History_Table.addCell(L00);
Master.write();
System.out.println("finished...");
Master.close();
}
}
but I want agents to do something like this:
Database D;
D.add(myAgent.getLocalName);
but it is not possible as jxl doesn't provide functions for working with agents. and it looks like that everything must be written in that excel file manually.... but it is not what I want.. I want agents comfortably read and write...
Is there any other way?
Yes basically when you create a JADE agent, you can add behaviors to those Agents,
There are several types of behaviors, you should be choosing them based on your requirement. You can find the list of behaviors here
For an Example,
public class MyAgent extends Agent
{
#Override
protected void setup()
{
addBehaviour( new InformBehaviour() );
}
private class InformBehaviour extends CyclicBehaviour
{
//dostuff
}
}
So basic idea is you need to do all these inside a behavior of a agent.
Make sure you choose right behaviour which suites your requirement.

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.

Embed processing 3 into swing

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.

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).

Creating Annotations in Java like Mockito's before

In processing I have several functions that change the properties of applet to draw stuff, for instance:
public void resetBackground(PApplet pApplet){
pApplet.fill(175);
pApplet.noStroke();
pApplet.rect(0,0,100,100);
}
But I want these functions to preserve the state of the pApplet after the function call, for that I have something like:
public void resetBackground(PApplet pApplet){
SaveAndRestoreDefaults saveAndRestoreDefaults = new SaveAndRestoreDefaults(pApplet);
// Code that changes state.
saveAndRestoreDefaults.restoreOriginals();
}
Now this works for me but I would like this not to clutter my code here but rather be annotation driven, something like:
#PreserveState
public void resetBackground(){
// code that changes state.
}
I have done a little research on it but it seems to be not an easy task. The googling took me to AOP and I don't want to spend time to learn that. Is there an easier way to achieve the same?
Thanks :)
I'd strongly recommend staying in Processing, instead of reaching into the underlying virtual machine API (just because you run it in java, doesn't mean every implementation of Processing has a JVM. Processing.js comes to mind).
Just make a state class and keep track that way:
class SketchState {
color background_color, stroke_color, fill_color;
SketchState(color bg, color st, color fl) {
sketch = s; background_color = bg; stroke_color = st; fill_color = fl;
}
}
ArrayList<SketchState> stateCache = new ArrayList<SketchState>();
void cacheState() {
stateCache.add(new SketchState(...));
}
void restoreState() {
SketchState rest = stateCache.remove(stateCache.size()-1);
background(rest.background_color);
stroke(rest.stroke_color);
fill(rest.fill_color);
}
and add whatever other state aspects you want saved to that class.

Categories