Creating a paint program
I am making a paint program and have a few questions regarding this.
I need the user to have the choice of drawing rectangle, oval and line. I suppose I need to make the superclass that they all come from. Should I make it an interface or an abstract class? And how would I establish that all shapes have default values when they are created, such as color, etc.
The user will use the mouse to click on the screen, and the program should make the shape centered where the user clicks, and use the previously entered height and length to draw it. What is the simplest way to store the points collected in a frame, and I even need to store this information?
I would also like the user to be able to scale the shape he just drew by dragging the sides. How can I most easily customize this?
Edit: Hi. I'll try with somewhat light MVC. I have a DrawingModel and a DrawingPanel class that I create in a view and then pass the DrawingModel object to the DrawingPanel using the setModel method. However, one question arises that I cannot omit; what part of the program will have to take care of the listeners? I am guessing that the View should be implemented, but where can I place the ActionListeners?
a source to share
Take a look at the MVC design pattern. In your case, you have to create a model object that is a collection of forms.
Your drawing area (like canvas or panel or whatever) must implement MouseListener and MouseMotionListener to capture mouse events. You can find out where the user clicked using the MouseEvent getX () and getY () methods. Then you can save this shape in the model and recolor () the area.
For scaling, you will need to use the mouseDragged () and mouseReleased () methods, which will give you the x, y coordinates. You need to determine the shape the user clicked on and then change its width and height depending on how far the user dragged (this can be determined by subtracting the original x, y from the new x, y coordinates).
a source to share
If you want all shapes to have some common required properties (and possibly methods), an abstract class is probably the best choice. You can set them in the abstract class definition:
abstract class AbstractShape
{
private static final Color DEFAULT_COLOR = Color.BLUE;
private static final float DEFAULT_LINE_WIDTH = .5f;
protected Color color = DEFAULT_COLOR;
protected float lineWidth = DEFAULT_LINE_WIDTH;
}
class Circle extends AbstractShape
{
}
EDIT: just for clarification, an abstract class can have a constructor, for example:
protected AbstractShape()
{
color = DEFAULT_COLOR;
lineWidth = DEFAULT_LINE_WIDTH
}
a source to share
To respond to your edit: This view will have an ActionListener that is bound to the components. Then you can get a view notifying the controller (via a listener) if the model needs to be updated. When the model is updated, it usually notifies the controller through a property change listener so that the controller can update the view.
a source to share
You may want to google DerekBanas, it has a couple of videos explaining how to do this. He also uploaded a great UML sample, design and other object related videos. I have been tinkering with some code over the past couple of days, which is a little beyond the scope of his tutorial videos, using other shapes and drawing better lines with a paint tool, etc. If you want to test the code, just send me a message and I'll send it to you, but it's too much to post a comment. Good luck.
a source to share