create a callable java void

Hi there, Im wondering if anyone could help me out real quick. Im making a programming involving a lot of custom buttons (+40). Im using java and netbeans to write the program, Im totally new to processing in general so If you could help me out itd be greatly appriciated. Im trying to make a separate class file that i can call to create a object (like a button for example). heres a sample:

File: WordDoc

import static java.lang.System.out;

public class WordDoc {
    public static void RUN(String string) {

This is the Main file to go with WordDoc.

File: Printer

public class Printer {
    public static void main(String args[]) {
        WordDoc.RUN("whatever you want to say");             

That works just fine. whatever I put into the ( ) prints out when the program is run. What i need is to make something that would allow me to do this:

void draw()  {
    rect(4,4,4,4); //normal processing is in the file i want to call the button in.

    ButtonClass.NewButton(X,Y,Width,Height,Color etc);  // this is the line i need

How could I do this? I already tried this:

import processing.core.*;
public class Button extends InkWDStartwindow {  //I dont know if that works, but it fixes errors raised by rect().
    public static void Button(short X,short Y,short Wide,short Tall) {
        public void draw() {

Thanks Guys!


  • Sorry if the line formatting is weird... it was fine when i pasted it in

  • Fixed the line formatting.. could someone please help me with the posted question?

  • I don't understand your message at all. You show two classes, WordDoc and Printer, then you show ButtonClass.NewButton() which is unrelated to the first two, and remains unknown.

    Normally, you create a new instance with the new keyword.

    See also the tutorial Objects.

  • I used those two as a example... sorry about the confusion. I did get somewhere with this a second ago but its still having issues. Im no longer having errors but when I run the code it doesn't show the rectangle. it takes a distorted snapshot of my screen and fills the new processing window with it.

    package edu.mass.necc.processing;
    import processing.core.*;
    public class Button extends InkWDStartwindow {
        public void newButton( int X, int Y, int Wide, int Tall) {
    ************new file****************
    package edu.mass.necc.processing;
    import processing.core.*;
    public class InkWDStartwindow2 extends PApplet {
        public void draw() {
            Button button = new Button();
    ******************new file************
    package edu.mass.necc.processing;
     * @author digitalblueeye
    public class Main {
         * @param args the command line arguments
        public static void main(String[] args) {
  • Why does the button class extend InkWDStartwindow and what is this class anyway? Also the button should not be created in the draw () method rather it should be created in the setup() method. Read this to find out about these 2 methods

  • it raises a error if i dont extend InkWDStartwindow, also doesnt recognize rect() or draw()

  • if i run it in setup() i get:

    Exception in thread "Animation Thread" java.lang.NullPointerException
        at processing.core.PApplet.rectMode(
        at edu.mass.necc.processing.visual.newButton(
        at edu.mass.necc.processing.InkWDStartwindow.setup(
        at processing.core.PApplet.handleDraw(
        at processing.core.PGraphicsJava2D.requestDraw(

    yet there are no errors in IDE until i run the file

    • Consider using Processing's own IDE (PDE) while you're still learning about the framework!
    • PDE contains a pre-processor which doesn't exist when using other IDEs.
    • Processing relies on various tricks behind the scene in order to hide Java's many crufts.
    • When using other IDE's we gotta implement all of those expert tricks by ourselves!
  • edited March 2015
    • In order to use Processing as a library for an IDE, it's best to know how it works internally.
    • Basically Processing is a complex framework + API which instantiates a "canvas" (or PSurface as it is implemented in version 3 now).
    • Processing's drawing API functions expect that this canvas exist.
    • Main Processing's API resides in a class called PApplet.
    • In order to use Processing framework we implement a class which extends PApplet, preferably w/ methods setup() & draw() and other callbacks in it.
    • However inheriting from PApplet alone isn't enough! We still gotta ignite the framework!
    • For that we call PApplet.main() or PApplet.runSketch() method passing our class' name String.
    • That'll make the framework instantiate its "canvas" and callback setup(), draw() and other event functions automatically!
  • edited March 2015

    Some of the big issues from your posted code. Let's debug it:

    • Your InkWDStartwindow2 class extends PApplet. Correct!
    • Calls PApplet.main() passing "InkWDStartwindow2", igniting the framework. Checked!
    • Button class extends InkWDStartwindow2, thus inheriting from PApplet too. Makes no much sense!
    • Calls Processing's API like fill(), stroke(), rect(), etc. Totally wrong!

    So why InkWDStartwindow2 is OK and Button isn't if both are PApplet classes? :-/

    • As mentioned before, simply inheriting from PApplet isn't enough!
    • We gotta ignite the framework via PApplet.main("")!
    • That's why fill(), rect(), etc. won't work in Button b/c there's no canvas there!
    • That's why you end up w/ NullPointerException there after rectMode() due to lack of "canvas".
    • However, if we also ignite Button we'll end up w/ 2 canvas!
    • You probably want Button to access InkWDStartwindow2's canvas rather than having another 1.
    • For that there are 2 main approaches: Java's & Processing's styles.
  • edited March 2015

    Since you're coding in a Java-centric IDE (NetBeans), let's start w/ ordinary Java's style 1st:

    • In this approach, any class needing to interact w/ a PApplet class gotta request its reference.
    • Thus Button gotta request InkWDStartwindow2's instance reference in order to access its canvas.
    • Most common way is having a PApplet parameter in the constructor. And store it in a field.
    • Less common is passing it for every method which needs it. Which isn't very practical!
    1. Here's your Button class tweaked to request & store a PApplet reference.
    2. Notice rect() method needs that reference in order to draw in the InkWDStartwindow2's canvas.
    3. Also that Button doesn't need to be a PApplet per se. Merely import it:

    import processing.core.PApplet;
    public class Button {
      public static final int FILL = 0xff0080A0;
      final PApplet p;
      int x, y, w, h;
      public Button(PApplet sketch, int xx, int yy, int ww, int hh) {
        p = sketch;
        x = xx;
        y = yy;
        w = ww;
        h = hh;
      public void display() {
        p.rect(x, y, w, h);

    And inside your InkWDStartwindow2 class, notice that it can directly call Processing's API, like size(), smooth(), fill(), etc., w/o any explicit PApplet reference, b/c it got its own canvas:

    Button b;
    public void setup() {
      size(300, 200, JAVA2D);
      b = new Button(this, 50, 50, width - 50, height - 50);
    public void draw() {
    • As a side note, we don't need any PApplet ref., even within Button, when the API being called is static.
    • Like println(), sqr(), nf(), binary(), etc.
    • For example in Button, we can both use p.println() via the reference or directly PApplet.println()!
  • edited March 2015

    Now for the Processing's style: :bz

    • Rather than having our public classes as separate ".java" files, we can place them inside the PApplet class.
    • That is, rather than having top classes, we'll have nested classes!
    • More specifically non-static nested classes, also called inner classes.
    • As a side note, inner classes also include anonymous instantiation type.
    • An inner class have access to everything their enclosing classes got, up to the top class!
    • In JavaScript, those accessible top information would be called "closure".
    • That's the reason we don't need to pass any PApplet references to our own classes in the PDE.
    • If we wish, we can have that same "comfort" even when coding in other IDEs if we use inner classes too.
  • Thanks so so much for the help! its very much appriciated. one question tho... how could I get things such as fill() and rectMode() to be included in Button?

  • edited March 2015 Answer ✓

    The very same way Button can call rect() within its display() method.
    Relying on the dot . access operator:
    Via a PApplet object reference, so it knows which "canvas" it's sending orders to: :-B

    final PApplet p;
    public void display() {
      p.rect(x, y, w, h);

    Although I think if all your Button objects got the same fill() & rectMode(), it's better defining them within setup()!
    No need to waste CPU time telling the canvas over & over about those fixed configs! 8-X

  • Also... any ideas on how i could make a blank canvas inside a processing canvas. sorta like photoshop... a editing canvas inside the program canvas ( i hope that makes sense).

  • well i would do it in setup but this is just a test run. In the end i will have 40+ buttons with a different color and image for each.

  • like how would I, for instance, create a space inside my program that i could populate with images or buttons, that i can drag or transform, but the objects are only visible inside that space. sorta like a window It could be a rect().

  • Your new questions are way off the original problem. You should consider create another forum thread!
    BtW, perhaps PGraphics is what you're looking for:

Sign In or Register to comment.