Ok, recently i have written a “paint application” in Java. Small program similar to MS Windows Paint and apparently (according to some people that evaluated my source code) im not using classes properly in my application. Unfortunatelly i have no idea what could be wrong, and yet noone is in the mood to tell me what is incorrect.
I have “proper” classes that use the idea of inheritance:
PaintObject
-ClosedObject
--RectangleShape
--OvalShape
-OpenObject
--PointShape
--LineShape
But still i have no idea what could be wrong in code, what could lead to thinking that classes are not used properly…
The only thing what (in my opinion) could be wrong is the fact that im drawing by creating the new objects like: new RectagleObject(...) and new OvalShape(...) instead of creating one field objectToBeDrawn variable (maybe a PaintObject type) and then assign appropriate shape to it when the method executeOperation is called (obviously along with drawing of that particular shape)
Here is the code sample:
public void executeOperation(Graphics2D gr, int oper, boolean drawingMode){
if(oper==1){
new RectangleShape(startLocation, endLocation, borderColor, fillColor).draw(gr);
}
else if(oper==2){
new OvalShape(startLocation, endLocation, borderColor, fillColor).draw(gr);
}
....//more operations
}
Anyone has any idea what could be wrong in terms of general programming habits (unless my ‘guess’ is right? – if it is, please confirm my doubts). Or maybe there is something else?
(Skip the part of oper==1 since i know that such values should be defined constant)
Any help greatly appreciated, im learning by myself and it is really hard to guess how professional programs are designed (and follow their pattern) since my experience is “limited” (actually none).
If you’re drawing raster images, do you need to keep track of what the user is drawing? Perhaps you mean objects such as
PenTool,RectangleTool, etc?Right now, it seems like you create objects that persist and are redrawn to draw specific shapes. You can simply have the tool objects draw on an
Imageonce when appropriate (just don’t ever clear the graphics context). Just assign a particular tool to someactiveToolfield when the tool is selected.So instead of
you would write
or something similar. Now when you want to draw something, just invoke the draw method. As I stated before, you can do this simply by passing in an
Imagethat you never clear: