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.
IndexDiscussionExhibition › Drag and drop demo
Page Index Toggle Pages: 1
Drag and drop demo (Read 3849 times)
Drag and drop demo
Nov 3rd, 2009, 11:20am
 
I was recently reminded of a drag and drop project I'd worked on in Actionscript and wondered how I would implement it from scratch in Processing.  I also saw this as an opportunity to practice OOP techniques, in particular 'Inheritance'.  It's still got some bugs and things I'd like to improve but I thought I'd post it here and get some feedback from the OOP gurus Smiley

It's not the most exciting thing to look at...

http://www.blindfish.co.uk/code/processing/drag-drop/

I'm particularly interested in any ways I might optimise the code and how I would implement appropriate Interfaces.
Re: Drag and drop demo
Reply #1 - Nov 3rd, 2009, 12:17pm
 
Very nicely done. I like the shadow effect and the movement when dropped on target.
Although it can be argued, I think I prefer the shape not to auto-center on mouse when clicking on it. It is usually done by keeping the offset to origin at start of drag, and applying it when dragging.
I will try and take a look at code later (if I recall that...).
Re: Drag and drop demo
Reply #2 - Nov 4th, 2009, 9:00am
 
Hmm... the shadow is just a drop shadow added to the PNG in photoshop; so nothing special Wink

'moveToTarget' is a method in the CircleDragger class: simple but effective easing animation.  There are also methods to draw each state (default, mouse-over etc.); so if you subclass it you have the option to draw whatever you like by overriding the appropriate method.  If you don't override it simply uses the default.

This little project has certainly opened my eyes to the benefits of OOP, and I can already see some improvements, but I'm not sure what effect these might have on performance.  For instance if I were to add properties to my CircleDragger class (to allow changes to colours by setting appropriate properties) and someone decided to subclass this but not use those properties (e.g. using images instead, as in the crudely implemented 'MyCircleDragger') would the fact that the class has these, unused, properties mean it has a larger memory footprint; or is memory only allocated if the properties are actually populated?

I also see the benefits of Inheritance, but there are also benefits to having parent classes (e.g. the reverting to default display behaviour described above; referencing the parent class to store different sub-objects in a single array etc.).  As such would it make sense to extend one class but also implement an Interface?  Presumably that would ensure required methods were implemented whilst retaining some of the default, useful behaviour...
Re: Drag and drop demo
Reply #3 - Nov 4th, 2009, 9:34am
 
Unused properties do take memory... But it is a problem only if you have millions of these objects!

OOP isn't about performance: it probably worsen memory footprint and speed, in general. It is about convenience when programming and ease of maintenance.

Interfaces: they are, I think, mostly useful to define APIs. You can see them as a contract. Or a constraint. They are also used to typify an object: some interfaces are empty, like Serializable, they just mark the classes "implementing" them of being of some type.

Additional advantage: you can make more generic code. For example, you define an interface LiveBeing.
You can implement it in classes PlayerCharacter, NonPlayerCharacter, Ennemy, Monster, Plant, etc.
Then you can store LiveBeings in collections, you can be sure an object extracted from there can breathe(), eat(), die(), etc. And you can check the exact type and specialize it if needed.

A more concrete example can be seen in the Java API: you can make a class, eg. reading image headers (I just improved the well known public domain ImageInfo class to support Tiff images). All it has to do is to take an InputStream and read() or skip() bytes.
Now, you can feed this class with a FileInputStream, an InputStream got from an URL (getInputStream(), no need to know the exact nature), a BufferedInputStream, etc.

You have great flexibility this way.
Re: Drag and drop demo
Reply #4 - Nov 7th, 2009, 6:35am
 
I'd more or less understood the idea of an interface but was a little confused as to whether that was the best option in my case.  I've actually discovered that a better approach was to define my base classes as abstract - they're not meant to be instantiated anyway, and I'm able to declare both methods that will be inherited and abstract methods that must be implemented in the child classes.  I've now updated the drag and drop demo with these improvements.

I've still got a bug to fix and I still have one OOP-related question:

I want to give people the option to pass a single target or an array of targets to my dragger objects.  I have set up two constructors to handle this in my BaseDragger class, but it seems that I also need to create duplicate constructors in child methods to accept both a single target or array of targets as parameters.  Presumably there's no way of avoiding this

As it happens it's just occurred to me that I can write a private method to handle generic instantiation and call this from the separate constructors to make the process of writing all the constructors simpler; but if there's a workaround I'd love to hear about it.
Re: Drag and drop demo
Reply #5 - Nov 7th, 2009, 7:16am
 
Quote:
I can write a private method to handle generic instantiation and call this from the separate constructors to make the process of writing all the constructors simpler

Yes, it is a common pattern to have such common constructor, be it a real constructor (called by the others) or an independent method.
Re: Drag and drop demo
Reply #6 - Nov 10th, 2009, 9:03am
 
I've just uploaded my latest (and probably final) version.  I've now added a class to keep track of whether draggers have been places correctly and when all targets are covered.

By the standards of things posted here usually I know this isn't the most exciting sketch, but it's been a really useful way for me to get my head round some OOP concepts and hopefully it will prove useful to someone out there Smiley

Edit - Sorry: forgot to thank PhilHo for the tip on Observer and Listener patterns Wink
Page Index Toggle Pages: 1