Hot questions for Using Applets in processing

Question:

So here is really simple code that will not compile in Eclipse:

import processing.core.*;

public class MyPApplet extends PApplet {
}

I'm trying to run it as a Java Applet, but I get the error:

java.lang.ClassCastException: MyPApplet cannot be cast to java.applet.Applet

The problem is PApplet is a class from processing package, and it extends java.applet.Applet, and MyPApplet extends PApplet, but I still get this error. It makes no sense. Why can't MyPApplet be cast to java.applet.Applet?

Can someone please help?


Answer:

Like George said, PApplet no longer extends Applet as of Processing 3.

But instead of going back to an old version of Processing, I would recommend using the runSketch() function to run your sketch:

public class MyPapplet extends PApplet {

  public static void main(String... args){
    String[] pArgs = {"MyPapplet "};
    MyPapplet mp = new MyPapplet ();
    PApplet.runSketch(pArgs, mp);
  }

  public void settings() {
    size(200, 100);
  }
  public void draw() {
    background(255);
    fill(0);
    ellipse(100, 50, 10, 10);
  }
}

If you really need access to the underlying native component, you have to write code that depends on what renderer you're using. Here's how you'd do it with the default renderer:

PSurfaceAWT awtSurface = (PSurfaceAWT)mp.surface;
PSurfaceAWT.SmoothCanvas smoothCanvas = (PSurfaceAWT.SmoothCanvas)awtSurface.getNative();

But the first approach should be good enough for most people, so try that first.

Question:

I have downloaded and installed the processing IDE from their official website, then I followed the instructions on how to import the libraries into eclipse. I have core.jar, gluegen-rt.jar and jogl-all.jar added to path. I have the code below which I'm trying to run but when I go to the run menu there is no run options whatsoever. I only see 'run configurations'. Is there anything I'm missing?

package week2;
import processing.core.*;

public class ProcessingTest extends PApplet{

    private String URL = "https://c2.staticflickr.com/6/5254/5428199232_c3678ed2ac.jpg";
    private PImage backgroundImg;

    public void setup()
    {
        size(800,  800);
        backgroundImg = loadImage(URL, "jpg");
        backgroundImg.resize(800, 800);
        background(backgroundImg);
    }
    public void draw()
    {

    }
    //public static void main(String args[]){
    //  PApplet.main(new String[]{"--present", "week2.ProcessingTest"});
    //}
}

Answer:

As of Processing 3, PApplet no longer extends Applet (more info here). In other words, you can't run sketches as an applet anymore.

You'll have to put your main method back in and run it as an application instead.

If you really want to run as an applet, you'll have to create your own class that extends Applet or JApplet, and then add the Processing component to that. That can be pretty convoluted, plus applets are pretty much dead now anyway, so you're probably much better of deploying as an application- or even better, as JavaScript using Processing.js.

As a side note, you shouldn't have to rely on eclipse automatically detecting the run configuration. You should be able to go into the run configurations and create one yourself. That won't work for this specific case, but it's not a bad idea to get more comfortable with the "behind the scenes" stuff so cases like this are less confusing.

Question:

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.


Answer:

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);
  }
}

Question:

I have a java applet on a website (http://martinogames.x10.mx/) but it gets blocked by java security settings. It is a signed applet created using the AppletMaker tool in processing. I know how to allow access on my computer (java security settings to medium/whitelist the site), but I want to know how to get Java to accept it for other users. I am the developer and should have access to all information needed to do this, if it is possible.

Thanks in advance, -Peperononew


Answer:

You need to buy a real certificate and sign it with that. Otherwise it would require users to trust your certificate, which is not secure at all (you're not a trustworthy entity).

Question:

I'm using the code from this answer and I have an issue with changing the size of the first applet. Changing values of size(100, 100) does nothing.How can I fix this problem?

public 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(500, 500);
  }
  public void draw() {
    background(255);
    fill(0);
    ellipse(100, 50, 10, 10);
  }
}

Answer:

An easy fix is to just move the first size() into the settings() function:

void settings() {  
  size(500, 100);
}

void setup() {
  String[] args = {"TwoFrameTest"};
  SecondApplet sa = new SecondApplet();
  PApplet.runSketch(args, sa);
}

void draw() {
  background(0);
  ellipse(50, 50, 10, 10);
}     

class SecondApplet extends PApplet {

  public void settings() {
    size(500, 500);
  }
  public void draw() {
    background(255);
    fill(0);
    ellipse(100, 50, 10, 10);
  }
}

I'm not exactly sure why this is happening. I would guess that it has something to do with the fact that you have to call size() from settings() when using eclipse. More info here.