Nedd help with a class


#1

Hello!

I’m getting back into coding after 20 some years… I’ve been toying around with processing for a year now, doing simple things… but this time, I want to learn classes

I have followed this tutorial Objects by Daniel Shiffman on Processing site worked well. until now…
I ran into a a problem that I just can’t figure out.

What I want to do

Control 3 different VLC player on different computer. Works flawless with “regular” code.

I decided to create a class to “clean” the code instead of having 3 time the same code for each instance different VLC player.

I hope it make sense, thanks for taking the time to help out…

  • Eric

error at compile

The constructor Client (VLC_Class.VLC, String, int) is undefined

my code

import processing.net.*;

VLC vlc1;

public void setup() {
  size(600, 600, JAVA2D);

  noStroke();
  //client = new Client(this, "127.0.0.1", 4212);
  //client.write("1234"+"\r\n");


  vlc1 = new VLC("127.0.0.1", 4212, "1234");
}

void StartVLC() {

    vlc1.play();
}


void draw() {

 // client.write("play"+"\r\n");
  
  
  StartVLC();
  
  exit();
}

my class

class VLC {

  String serverIP;
  int portNumber;
  String password;
  String enter = "\r\n";
  Client client;

  VLC( String tempIP, int tempPort, String tempPass) {

    serverIP = tempIP;
    portNumber = tempPort;
    password  = tempPass;
  }

  void connect() {
    println("connect");
    client = new Client(this, serverIP, portNumber);
    client.write(password+enter);
  }

  void play() {
    println("play");
    client.write("play" +enter);
  }

  void next() {
     client.write("next" +enter);
  }

  void pause() {
     client.write("pause" +enter);
  }
}

#2

The problem is that when you were setting up the client inside setup(), the keyword this used in the constructor refered to the PApplet that setup() belongs to. Once you moved this constructor call for Client() inside your class, the this keyword now refers to that class, and not the PApplet that it was referencing before.

The solution is to pass the reference to your PApplet into your own constructor:

// inside setup():
  vlc1 = new VLC(this, "127.0.0.1", 4212, "1234");
// Fix your class to deal with the new parameter:
  Client client;
  PApplet parent_app;

  VLC( PApplet ipa, String tempIP, int tempPort, String tempPass) {
    parent_app = ipa;
    serverIP = tempIP;
    portNumber = tempPort;
    password  = tempPass;
  }

  void connect() {
    println("connect");
    client = new Client(parent_app, serverIP, portNumber); // The this that was here was the problem - now changed.
    client.write(password+enter);
  }

You could also have added a global variable to store this reference to the parent PApplet in setup and use it inside your class, should you not desire to pass it as a parameter.

Untested code - YMMV


#3

#4

@EricSaumier – Using a global variable certainly an option, but the first pattern that @TfGuy44 recommends – passing in a PApplet with this – is very standard in Processing. Many of the major libraries follow this pattern, and require you to initialize a major library object with this in order to work their magic (see ControlP5, G4P, PeasyCam, etc etc). For example, you fire up ControlP5 with:

cp5 = new ControlP5(this);

#5

I took the time to digest everything… I’m starting to understand things a bit more, and it starts make sense. I need to really undestand PApplet… with GoToLoop link, I think i’m on the right path…

Thanks alot for the help and the time you guys invest helping others. One day, I’ll give back!

@TfGuy44 … I tested your "untested’ solution… worked on first try.
@jeremydouglass That will be the way I’ll go… seems to make more sense