We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › refferencing main from with in a class
Page Index Toggle Pages: 1
refferencing main from with in a class (Read 658 times)
refferencing main from with in a class
Dec 8th, 2009, 12:51pm
 
hello,

sorry if this is basic, i am a newbie

I am using the SDrop library with PGraphics to make an image tile that can have an image be scaled and panned while still being in an enclosed (image dropable) tile. I have everything working but I want to turn it into a class so i can make the tiling be scale able etc. I am having problems though with the SDrop class constuctor. The examples use SDrop(this) but since I am putting it in a class it is refferencing my class not the main app. Is there a way to use something like parent instead of this?

thanks for any help and here is the class code


// a custom DropListener class.
class MyDropListener extends DropListener {
 
 int myColor;
 int x,y,w,h;
 
 MyDropListener(int tx, int ty, int tw, int th) {
   myColor = color(255);
   // set a target rect for drop event.
   setTargetRect(x,y,w,h);
   x = tx;
   y = ty;
   w = tw;
   h = th;
 }
 
 void draw() {
   fill(myColor);
   rect(x,y,w,h);
 }
 
 // if a dragged object enters the target area.
 // dropEnter is called.
 void dropEnter() {
   myColor = color(255,0,0);
 }
 
 // if a dragged object leaves the target area.
 // dropLeave is called.
 void dropLeave() {
   myColor = color(255);
 }
 
 void dropEvent(DropEvent theEvent) {
   println("Dropped on MyDropListener");
 }
}





class ImageTile{
 PGraphics pg;
 PImage b;
 import sojamo.drop.*;

 //Sdrop features
 SDrop drop;
 MyDropListener m;
 
 int pX;
 int pY;
 int pushX = 0;
 int pushY = 0;
 int scaleX, scaleY;
 int scaleStartX, scaleStartY;
 float scaleFactor = 1.0;

 ImageTile(int tx, int ty) {
   pg = createGraphics(tx, ty, P3D);
   drop = new SDrop(this);
   m = new MyDropListener();
   drop.addDropListener(m);
 }

 void display() {
   pg.beginDraw();
   pg.background(0);
   pg.pushMatrix();
   pg.translate(pushX,pushY);
   pg.scale(scaleFactor);
   if(b != null){
     pg.image(b,0,0,50,50);
   }
   else {
   }
   pg.stroke(255);
   pg.popMatrix();
   pg.endDraw();
   image(pg, 0, 0,50,50);
 }


 void dropEvent(DropEvent theDropEvent) {
   // if the dropped object is an image, then
   // load the image into our PImage.
   if(theDropEvent.isImage()) {
     println("### loading image ...");
     pg.beginDraw();
     b = theDropEvent.loadImage();
     pg.image(b,0,0);
     pg.endDraw();
   }
 }

 void mousePressed() {
   if(mouseButton == RIGHT) {
     scaleStartX = mouseX;
     scaleStartY = mouseY;
   }
 }


 void mouseDragged() {
   if(mouseButton == LEFT){
     if(mouseX > pX) {
       pushX++;
     }
     else if(mouseX < pX) {
       pushX--;
     }
     if(mouseY > pY) {
       pushY++;
     }
     else if(mouseY < pY) {
       pushY--;
     }
   }
   else if(mouseButton == RIGHT){
     if(mouseY > pY) {
       scaleFactor += 0.1;
     }
     else if(mouseY < pY) {
       scaleFactor -= 0.1;
     }
   }
   pX = mouseX;
   pY = mouseY;
 }




}


Re: refferencing main from with in a class
Reply #1 - Dec 8th, 2009, 1:13pm
 
Just pass the PApplet instance to your custom class, so you can pass it into the SDrop() constructor :

Code:
class ImageTile {
//PApplet parent;
 SDrop drop;
 ImageTile(PApplet parent) {
//this.parent = parent;
   drop = new SDrop(parent);
 }
}

ImageTile imgTile = new ImageTile(this);


Uncomment the commented lines if you want to store the PApplet reference for later use.
Re: refferencing main from with in a class
Reply #2 - Dec 8th, 2009, 2:25pm
 
awesome dude,

thank you very much. i tried messing with PApplet but obviously didn't have it. thanks again

Page Index Toggle Pages: 1