We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello
I want to convert this code into class in order to use wherever is possible,
PImage sample;
int x, y;
void setup() {
size(1000, 600);
noStroke();
sample=loadImage("G2.png");
x=0;
y=400;
}
void draw() {
background(255);
image(sample, x, y, 50, 30);
}
void mouseDragged() {
x=mouseX;
y=mouseY;
}
I rewrote this code this way, but it nor show mistakes neither works, and I don't konw what am I doing wrong...
Draggingpic Dragging = new Draggingpic();
void setup() {
size(600, 600);
noStroke();
Dragging.charge();
}
void draw() {
background(255);
Dragging.mouseDragged();
}
class Draggingpic
{
int x;
int y;
PImage sample;
void Draggingpic(int posx,int posy,PImage sample2)
{
x=posx;
y=posy;
sample = sample2;
}
void charge()
{
sample=loadImage("g2.png");
}
void mouseDragged()
{
x=mouseX;
y=mouseY;
}
}
Answers
It looks almost correct, except you forgot to call
image()
somewhere...Hi
I found that I haven't declare a new object for that class as variable and then in setup to call
Now it just appeared a mistake, but I don't get howto declare an instance of that PImage there
Thanks
https://forum.Processing.org/two/discussion/19271/coding-class-with-dragging
the constructor in the class withoud void
Draggingpic
Hello
I replace this in setup
but the image still doesn't appear. Without void it'd work, but, no possible yet. Right now there are no errors, anyway no possible to see image nor of course drag it.
??
the constructor inside the class must be without void.
you need a line image(sample);
inside the class
in mouseDragged eg
Hello
I delete that word, but as I say, it doesn't work yet. No image appearing on background, and obviously
Thanks
use image.....
loadImage is wrong!!!
the command is image(sample);
!!
Hello
I wrote this in dragged...is it right, anyway....nothing, no image showing...
void mouseDragged() { sample2=loadImage("g2.png"); x=mouseX; y=mouseY; }
Hi
Yhis is all my code now, where do I call the image?
I guess is only one code line missing, but I don't know where...
Thanks
please highlight your code and press ctrl-o, don't rely on the backticks for formatting blocks of code.
Hi
Add a method
void display()
to your Draggingpic class where you callimage(sample,x,y);
. Then call Dragging.display() somewhere in your draw method.Hello:
It works but not properly, I mean, at first appears the image but the point is that I need to drag while click pressed and droped when click button is released, not all time as it is shown now.
Thanks
You actually need to check when someone clicks on the image (use
mousePressed()
), then calculate the difference b/w original and new mouse coordinates when the mouse is released/dragged.Hi
I really am wondering why to add one more event mousePrresed if in the original code isn't included, and it works perfectly.
Thanks
Well then try this:
I also recommend that you check out the tutorials on the Processing page.
Hello,
The code above (from Nov 25) is not good code.
When you read the tutorial on objects (classes, OOP), you'll see you can improve the code.
Especially line 11:
image(sample2,50,50,50,50);
belongs into the class. In fact you don't usex,y
in the class at all. You even don't use the image in the class.As soon as you want to use multiple images you'll run into troubles.
Here is my sketch:
Chrisir
@Chrisir He wanted quite literally the original code thrown into a class, and that requires not having
mousePressed()
ormouseReleased()
(even if it does not work). And besides, if he followed @colouredmirrorball, he would have already removed thatimage(sample2,50,50,50,50)
and replaced it with a display function inside the Draggingpic class.P.S. @Iamperiestro You ought to learn how to name your classes and variables more properly and conventionally.
Hi
I don't feel comfortable with Chrisir answer (despite is proposed a new class to handle drag and drop, that's what I was asking for), because the fact is to write few lines instead to spread along, I originally had a code, then obtained twice the size of the code using class, now I have three times that size, that's why I say so, anyway I thank the effort for helping me with this staff. The point is to learn to write code using classes whenever is possible, because the code is reusable, looks more organized and is widely used if you need to draw on many objects with same properties
Thanks
@Iamperiestro Did you try my answer? That is just a little larger than yours and does the exact same thing. @Chrisir has given the correct answer as far as the problem of dragging images goes, but it was a bit different from the original code. So I don't see why you are uncomfortable with it.
It is not a bad thing that the code is longer though.
In fact, I often feel longer code is better readable and more explicit than shorter code.
And perhaps more importantly, it is very easy to reuse this longer code. It may seem longer now, but try with some 20-30 images. Then the hard coded version will be even more difficult to understand, and maybe even longer.
Hi
Thanks for explanations and suggestions
Hello
It's right Chrisir and Lord_of_the_Galaxy, I tried that code and it worked very good with and object array, the original code didn't permit to select-drag over a certain area, just wherever.
I put here some code which shows two animal (dam and hunter) whom would be dressed before starting a competition...and their new look is saved
Thanks