Tech Guides‎ > ‎

Processing and SWT in Eclipse

I needed to do some basic user interface for a Processing sketch.
 
- But lets face it: Doing UI in Processing sucks big time

Why not just use SWT (Standard Widget Toolkit) and get combos buttons and other nice stuff? 
- I had a hard time finding anybody doing this, so this guide should help others.

Switch to Eclipse

Abandon the Processing IDE and switch to Eclipse. Your life will be easier. You will smile more. Women will ask you out.

When you download, you want the "Eclipse IDE for Java EE Developers"
You also need to install a JDK (Java Development Kit) from Oracle/Java.


Coding Processing in Eclipse

You need to import the basic Processing libraries to your Eclipse workspace.

Getting started with SWT in Eclipse

Setup Eclipse for working with SWT. 











SWT and Processing

There are several ways of combining the Processing PApplet and a SWT 
In this example, I have two windows. - A window for Processing and a window next to it with my UI controls.
Processing window - And SWT window

There are two classes in this example SimpleProcessingExample.java and SwtUI.java
We launch SimpleProcessingExample.java and then SimpleProcessingExample open SwtUI with the .. SWT UI. Tadaa!

SimpleProcessingExample.java


package dk.liljedahl.christian.apx;

import processing.core.*;

// Launch this, the Processing thingy. This will open the UI thingy.

public class SimpleProcessingExample extends PApplet{

    public void setup() {
        size(300, 300);
        background(255);
        fill(0);        
        
        // Instantiate the UI - Using "this" to make it possible for the UI to reference this
        SwtUI mySwtUI = new SwtUI(this);
        Thread mySwtUIThread = new Thread(mySwtUI);

        //Start the UI-threads
        mySwtUIThread.start();
        
    }
    // position is public, to make it accessible from SwtUI
    public int position = 30;
    public void draw() {
          background(255);

          ellipse(position, 150, 25, 25);
        }
}

(ColorCoding done with: http://tohtml.com/java/ )

SwtUI.java


package dk.liljedahl.christian.apx;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class SwtUI implements Runnable {

    SimpleProcessingExample myParent; 

    public SwtUI(SimpleProcessingExample myPapa) {
        // we need to establish a reference to the Processing Sketch, to make it possible to talk to it
        this.myParent = myPapa;
    }

    @Override
    public void run() {

        Display display = new Display ();
        Shell shell = new Shell(display);

        // Some properties - Size and placement. To keep nicely sided next to the Processing window
        shell.setSize(300,300);
        shell.setBounds(315, 0, 300, 300);
        shell.setText("Button Example");
        shell.setLayout(new RowLayout());

        // The two buttons
        Button button1 = new Button(shell, SWT.PUSH);
        button1.setText("Left");
        button1.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                myParent.position = 30;
            }
        });
        Button button2 = new Button(shell, SWT.PUSH);
        button2.setText("Right");
        button2.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                myParent.position = 270;
            }
        });
        
        shell.open();
        
        // Default Swt-stuff
        while (!shell.isDisposed ()) {
            if (!display.readAndDispatch ()) display.sleep ();
        }
        display.dispose ();
    
    }
}

(ColorCoding done with: http://tohtml.com/java/ )

More about java and threads

http://www.ibm.com/developerworks/library/j-thread.html
http://www.javabeginner.com/learn-java/java-threads-tutorial/all/1
Comments