We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, i´m trying to use this
surface.setSize(img.width, img.height);
but from antother surface, how i can resize the main surface from a class created surface.
this is my code
ControlWindow CWindow;
Button OpenImage;
Button SaveFile;
PImage img;
void settings() {
size(320, 240);
smooth();
noLoop();
}
void setup() { //This is the one y wanna to resize from the Control Window
surface.setTitle("Output");
surface.setResizable(true);
CWindow = new ControlWindow();
}
void draw() {
background(250);
if(img!=null){
image(img, 0, 0);
}
}
class ControlWindow extends PApplet {
public ControlWindow() {
super();
PApplet.runSketch(new String[]{this.getClass().getName()}, this);
}
public void settings() {
size(400, 400);
OpenImage = new Button(this,150, 70,50,30,"Nuevo", GREEN, RED );
SaveFile = new Button(this,150, 140,65,30,"Guardar", YELLOW, RED );
//smooth();
noLoop();
}
public void setup() {
surface.setTitle("Control Window");
surface.setResizable(true);
}
public void draw() {
background(250);
OpenImage.SetButton();
SaveFile.SetButton();
}
int bana=0;
public void mousePressed() {
if(OpenImage.ButtonPressed()){
String Path = FileChooser("Imagenes", "Open File", new String[] {
"jpg", "bmp", "png"});
println(Path);
img= loadImage(Path);
//here is where i need to set the new size SOMETHING.setSize(img.width, img.height);
}
if(SaveFile.ButtonPressed()){
}
}
public void mouseDragged() {
}
}
Answers
Do you want to resize the main sketch window or
CWindow
?To get the enclosing applet and its surface create 2 global variables at line 5
then the first 2 lines in setup (after line 12)
Then in the inner class you can use this variable and
SOMETHING.setSize(img.width, img.height);
becomes
appSurface.setSize(img.width, img.height);
An alternative approach to using reflection :)
Hi quark, your answer was so useful, this is just i need, thanks!, GoToLoop, thanks for the answer but, i can´t understand this, i´m a begginer jeje, did you know about a book that i can follow to learn?
@FertheBana, getEnclosingPApplet() is a utility method which returns the PApplet's reference of the top class enclosing your inner class ControlWindow. That is, the main sketch itself! :-B
Therefore, there's no need to request it from the sketch. The inner class finds that out alone! \m/
Here's a variant of your sketch that stores getEnclosingPApplet() in the field p.
Then relies on that to invoke getSurface() and setSize() + setTitle() within mousePressed():
@GoToLoop, it´s very very interesting way, i'll try to undestand it and use it in my code :)
Any further doubts about it, just ask it! ;)
@GoToLoop can you explain me this?
int grow = mouseButton == LEFT? GROWTH : -GROWTH;
how can i read this textually? (if mouse button is left then...bla bla bla...) i can´t understan this kind of code easily, i´m so noob :)https://Processing.org/reference/conditional.html
oookkaayyy jajaja that was easiest than appear XD soorry if i´m being anoying with my english sikills :(
@GoToLoop, can you teachme how to close individual applets? i´m opening a new nested applet when i press a button, but, every time i did, another applet apears :(, i need to close the nested applet if there is already open.
It's much simpler to merely "hide" them.
Use
p.getSurface().setVisible(false);
+p.noLoop();
in order to "hibernate" a PApplet.And
p.getSurface().setVisible(true);
+p.loop();
to "wake" it up back. :ar!@GoToLoop, where can i find getSurface() methods, or more information to play with ppaplets and surfaces? :)
Function getSurface(), and many others btW, is undocumented! @-)
We need to look straight to Processing's source code in order to even know it exists: [-(
https://GitHub.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L814
:'( by the hardway