JRose
YaBB Newbies
Offline
Posts: 8
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; } }