We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello everyone !
First this is a little background on this project : I began using Processing to use with my arduino project to create a simple desktop monitoring "widget" that would display all kind of informations the arduino sensors would send. I'm quite new to programming and newer in Processing.
Here is my problem :
I wish to create an object ( called "bloc" ) that is simply a little rectangle with some attributes like title, colors and so on. Problem starts when I tried to implement a click&drag fonctionality, allowing for easy repositioning of the "bloc" inside the canvas.
I got it to work, but this imply I have to declare a bunch of extra stuff in the main code that kinda of erase the purpose of using an object and I feel I'm missing something that would allow me to get that code inside the "bloc" class file.
So anyway please feel free to help me on this, and idea/tips regarding the project would be greatly appreciated too, thanks !
Main file : objected.pde
PFont font;
Bloc a; // At this time the bloc a needs xa ya ca da IN the main file to run
int xa=0; // if I move those values and the "if mousepressed" in the bloc class file
int ya=0; // it will work until mouse isn't pressed and it will then come back to default values
int ca=100; // of the main program, because i'm unable to communicate variables between
int da=100; // main file and class file.
void setup()
{
size(400,1015);
font = loadFont("Impact-48.vlw");
textFont(font, 48);
a= new Bloc();
}
void draw()
{
background(255);
if ((mousePressed == true) && xa < mouseX && mouseX < xa+ca && ya < mouseY && mouseY < ya+da ) {
xa= mouseX -(ca/2) ;
ya= mouseY -(da/2) ;
}
a.display(xa,ya,ca,da,0,0,240);
a.title("Test");
}
Secondary file : bloc.pde
class Bloc {
int x;
int y;
color theme;
float xmiddle;
Bloc(){
}
void display(int a , int b , int c, int d, int Rtheme, int Gtheme, int Btheme){ // display a bloc on the screen ( xpos , ypox , lengh, height, Rtheme, Gtheme, Btheme )
x=a;
y=b;
xmiddle = x + (c/2) ;
theme = color (Rtheme,Gtheme,Btheme);
stroke(theme);
fill(255);
rect(x,y,c,d);
}
void title(String a) { // set the title of the bloc
PFont titlefont = loadFont("Corbel-30.vlw");
textFont(titlefont);
fill(theme);
textAlign(CENTER, TOP);
text(a,xmiddle,y-30);
}
void move() {
}
}
Answers
In this post there is a drag and drop example.
https://forum.processing.org/two/discussion/comment/54354/
Thanks vk for the quick answer !
What I saw in your code is that you set the class inside the main code. I did use a different file for the classe code ( using the "new tab" inside processing ) it helps me getting things nice and clean in my head (lol).
I guess my question can be shorten to : " is there a way to have a variable that can be defined in the child code and used in the main file. Can a variable have a "full scope" so to speak.
PS : Edited
This code shows two Bloc(s) which can be dragged independently.
Notice the class takes care of itself :)
BTW it is better that variable names represent the data they hold.
Tabs in PDE are a convenience, as long as they are NOT named
something.java
, they will all be wrapped in one class by the pre-processor. So there is no difference in declaring your class in a tab or not. :)Thanks you guys for all your answers, and thanks for the clarifiction on tabs vk !
I managed to get everything nice and neat like I wanted.
My problem was i needed to set initial parameters for the object ( x, y, height and lenght ) but then be able to interact with those without altering the values for other objects.
I managed to do that with some kind of triggers : as long as no trigger is triggered ( lol ) the object will use initial values. And if a trigger is trigerred ( re-lol ) it will then start to use custom and class local variables.
I put the code for reference, I tried and make it clean but that's far away from quark and his fancy blue functions haha, but still it allows me to keep it clean on the main file.
Thanks again and merry christmas =)
Main file :
Class file :
hmm...
room for improvement
Your thinking on the display() method is too complicated
you pass parameters to the constructor (see tutorials objects) and they are then used in the class. They can be changed.
I will try and improve on this, the constructor is this line ?
If so you're right I didn't quite get the grasp on it and left it empty. This is a learning process and I just started but I really wanted to work in "OOP" fashion to start on good bases.
So far I think Processing is really a fantastic tool, it allows a complete noob like me to code a nice looking interface quite simply and that's awesome =)
You coulda even removed the empty Block's constructor.
Java automatically creates an empty 1 when it's omitted! :ar!
Nice to know GoToLoop =)
I built up the class constructor and included the properties of the bloc inside ( position, size, color ) so it's really nice. I also got rid of the triggers things ( since the object are created in the setup, inital values won't be reset, that was my mistake ).
This is off of the original topic but I have another question :
When I use processing main fonctions ( like text(), background() etc ... ) they have this nice feature that allow for different forms of argument. For example background() can take one argument ( grey variants ) or three for RGB color. Is it possible for me to create a class/function with the same properties ? For example my block class now take 7 arguments ( 4 for placement and size and 3 for color ) it is possible for me to code so that if i only provide the placement argument it wil understand and use some kind of default value for color ? My answer would be to create a clone with a name variant but it would be nice to have it all inside the same function/class.
Thanks to anyone patient enough to answer me =)
Hi. I totally think going to OOP is a good idea. I't does makes things easier... I'm not really good in technical stuff, or even in advanced OOP concepts, but here a simple example that should answer your question. See the comments.
And as you can see //comments are awesome!
:)
That's called overloading, which is a type of polymorphism.
More than 1 method/constructor can use the same name as long as each 1 got its own signature.
Signature includes parameter's quantity, order & datatype. Returning datatype counts as well.
You guys are awesome ! Thanks a lot for the example vk. I will look about PVector as it seems a nice tool to organize parameters but for now, as I have no idea about it, I kept it within my range of understanding ^^. GoToLoop you man deserve a medal in pedagogy. Your answer was perfect =)
So I did implement this in my code :
Main :
Class File :
I tried and keep things nice and clean ( discovered the miraculous auto format tool xD ).
Once again thank you guys for your awesome help.
If you think 7 arguments are too many, why not take those 4 bounds' parameters and turn 'em into 1 Rectangle. Then those 3 last RGB attributes as 1 Color?
Here's a sketch example w/ 3 classes: BlocA, BlocB, & BlocC.
BlocB (Rectangle r, Color _c)
Then it extracts each property from those 2 and assign them to their respective fields:
int x, y, w, h;
&color c;
Hey =)
I didn't see your answer at the time GoToLoop but you're absolutly right, packing those paramaters into a rectangle and color object was the way to go for a cleaner code.
Thanks again for the help =)