Displaying a PApplet inside a JFrame
This code example demonstrates the combination of PApplet with JFrame, JPanel, JButton and JFileChooser.
A simple Processing Applet, showing some bouncing balls, is displayed and controlled inside a JFrame of a Java Application.
Download the source files
Table Of Contents
Source - class Application
package ch.ethz.caad; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; /** * A simple demo application launching a Processing Applet * * Demonstrates the combination of JFrame, JButton, JFileChooser * and PApplet. * * @author georg munkel * */ public class Application { public static void main(String[] args) { //create a frame for the application final JFrame frame = new JFrame("PApplet in Java Application"); //make sure to shut down the application, when the frame is closed frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //create a panel for the applet and the button panel JPanel panel = new JPanel(); //create a panel for the buttons JPanel buttonPanel = new JPanel(); //create an instance of your processing applet final MyApplet applet = new MyApplet(); //start the applet applet.init(); //Buttons //create a button labled "create new ball" JButton buttonCreate = new JButton("create new ball"); //assing a tooltip buttonCreate.setToolTipText("creates a new ball "); //give a name for the command //if this is not assigned the actionCommand equals the button label buttonCreate.setActionCommand("create ball"); //create a button lable "load file" JButton buttonLoad = new JButton("load file"); buttonLoad.setToolTipText("loads a new background image"); //button actions //the create button is simply linked to the applet //the action is executed inside applet.actionPerformed() buttonCreate.addActionListener( applet ); //this action is implemented NOT in the PApplet on purpose //fileDialogues like to crash a PApplet // //if the JFileChooser returns a valid file //loadBgImage() in MyApplet is executed buttonLoad.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { JFileChooser chooser = new JFileChooser(); //example of an image fileFilter //no need to use, just switch it off chooser.setFileFilter(new MyImageFileFilter()); int returnVal = chooser.showOpenDialog(frame); if(returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName()); //sending the selectedFile to loadBgImage() in the PApplet applet.loadBgImage( chooser.getSelectedFile() ); } } }); //store the two buttons in the button panel buttonPanel.add(buttonCreate); buttonPanel.add(buttonLoad); //store the applet in panel panel.add(applet); //store the buttonPanel in panel panel.add(buttonPanel); //store the panel in the frame frame.add(panel); //assign a size for the frame //reading the size from the applet frame.setSize(applet.getSize().width, applet.getSize().height +200); //display the frame frame.setVisible(true); } }
Source - class MyApplet
package ch.ethz.caad; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.util.ArrayList; import processing.core.*; /** * A simple Processing Demo Applet * * Used to demonstrate the combination of JFrame, JButton, JFileChooser * and PApplet * * Moves and displays a list of balls on the applet's screen * A background image can be loaded * * This applet can be used as ActionListener for Java Applications. * @author georg munkel * */ public class MyApplet extends PApplet implements ActionListener{ //list of all balls ArrayList <Ball> ballList; //the background image PImage bgImg = null; @Override public void setup() { size(400, 300); ballList = new ArrayList<Ball>(); //creates a first ball createNewBall(); } @Override public void draw() { //check if the background image is already loaded //if not, the background is painted white if (bgImg == null) { background(255); } else { image(bgImg,0,0, width, height); } //move and display all balls for (int i=0; i<ballList.size(); i++) { Ball ball = ballList.get(i); ball.move(); ball.display(); } } /** * implementation from interface ActionListener * method is called from the Application * the String being compared is the ActionCommand from the button */ public void actionPerformed(ActionEvent evt) { if (evt.getActionCommand().equals("create ball")) { createNewBall(); } else { println("actionPerformed(): can't handle " +evt.getActionCommand()); } } /** * this method is called by the ActionListener asigned to * the JButton buttonLoad in Application */ public void loadBgImage(File selectedFile) { bgImg = loadImage(selectedFile.getAbsolutePath()); } /* * creates a new Ball instance and adds it to ballList */ private void createNewBall() { Ball nBall = new Ball(); ballList.add(nBall); } /* * simple inner class Ball * balls have a position, speed, size and color * the basic constructor assign random values to all properties * * balls can move * balls can display themselves */ private class Ball { float x; float y; float size; float speedX; float speedY; Color color; private Ball() { this.size = random(10, 40); this.x = random(this.size, width-this.size); this.y = random(this.size, height-this.size); this.speedX = random(-2, 2) *3; this.speedY = random(-2, 2) *3; this.color = new Color(random(1), random(1), random(1)); } private void move() { if (x+size/2f > width || x-size/2f < 0) speedX = -speedX; if (y+size/2f > height || y-size/2f < 0) speedY = -speedY; x += speedX; y += speedY; } private void display() { stroke(color.getRGB()); fill(color.getRGB(), 120); ellipse(x, y, size, size); } } }
Source - class MyImageFileFilter
package ch.ethz.caad; import java.io.File; import javax.swing.filechooser.FileFilter; /** * Example for a ImageFileFilter */ public class MyImageFileFilter extends FileFilter{ /** * define which file extension is accepted */ @Override public boolean accept(File f) { if (f.isDirectory()) { return true; } String extension = Utils.getExtension(f); if (extension != null) { if (extension.equals(Utils.tiff) || extension.equals(Utils.tif) || extension.equals(Utils.gif) || extension.equals(Utils.jpeg) || extension.equals(Utils.jpg) || extension.equals(Utils.png)) { return true; } else { return false; } } return false; } /** * define the description of this fileFilter * for the JFileChooser */ @Override public String getDescription() { return "ImageFiles"; } /** * Simple utility to compare file extensions */ private static class Utils { public final static String jpeg = "jpeg"; public final static String jpg = "jpg"; public final static String gif = "gif"; public final static String tiff = "tiff"; public final static String tif = "tif"; public final static String png = "png"; /* * Get the extension of a file. */ public static String getExtension(File f) { String ext = null; String s = f.getName(); int i = s.lastIndexOf('.'); if (i > 0 && i < s.length() - 1) { ext = s.substring(i+1).toLowerCase(); } return ext; } } }