Make technical decisions
During the development process, I had to solve some technical problems and make some technical decisions. The following code snippet is simply explained, but they will be described in detail in my later articles. It is important here to understand the role that the archetype plays. Use your prototype to find solutions to technical problems, to test infrequently used APIs, and to ensure the performance of your application.
With multilayer panels
Building a graphics application, such as an artboard in Windows, is not a very complex task. You have to handle mouse events, draw lines, draw rectangles, and draw ellipses. Also deal with deformation features, such as from an underlying application to a professional graphics editor, to have more work on moving, scaling, reordering, deleting, copying, cutting, and pasting of pictures. You can also want to include a text box that allows you to edit, control the size and text wrapping, and so on. Building your own style text editor is not necessary because swing has already provided some text components.
How do you integrate a swing text editor with your own drawing components? I've considered two solutions. One is to implement a cell editor similar to JTable, but if you want to change the size of the text box or move it, you need a little skill. Another solution is to use JDesktopPane to place the text component inside the jinternalframe.
With the second solution, Swing has provided the ability to resize and move, but the question below is how do you draw an image in a built-in frame that contains text annotations? And how do you draw other simple graphs on the jdesktoppane, such as lines, rectangles, and ellipses? Luckily, there is a simple solution, because JDesktopPane is really a multilayer panel. The prototype Mainpane class extends the JDesktopPane, with two layers. One of them contains paintview custom components that allow you to draw simple graphics. Another layer contains text annotations. Of course, if an annotation picture cannot be obtained by the program, then the solution is meaningless. Mainpanel's Getannotatedimage () method uses the following code to do this:
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
printAll(g);
g.dispose();
Draw outside of paint ()
The drawing of swing components is usually done internally by paint () or by calling the drawing method inside the paint (). When you use the mouse to draw an object on the screen, you do not want to redraw other components, because this will cause the application to run inefficiently. For example, a user draws with a pencil, and each mouse event lets the application draw a small segment. There are hundreds of mouse_dragged events between Mouse_pressed and mouse_released.
Redrawing the Paintview component hundreds of times when a user draws only some graphics on the screen is not acceptable. Note that Paintview handles most of the drawing operations and a repaint requires all comments to be redrawn, including text annotations. The correct solution is to obtain the graphics context using getgraphics () outside the paint () when each mouse event is processed.
protected void toolAction(MouseEvent e) {
e.consume();
Graphics2D g2 = (Graphics2D) getGraphics();
float zoomFactor = model.getZoomFactor();
g2.scale(zoomFactor, zoomFactor);
float x = e.getX() / zoomFactor;
float y = e.getY() / zoomFactor;
currentTool.action(e.getID(), x, y, g2);
g2.dispose();
}
The Paintview component uses mouse monitoring to handle mouse events. The above method is called by each event, and the delegate draws the Currenttool object. When the mouse is released, repaint () is called to request a refresh of the entire component. As a result, paint () is called only once after the user finishes drawing the drawing object. This is the code for registering mouse listening:
protected void registerListeners() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
requestFocus();
currentTool = model.createTool(AbstractTool.DRAW_STYLE);
toolAction(e);
}
}
public void mouseReleased(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
toolAction(e);
model.setLastTool(currentTool);
currentTool = null;
repaint();
}
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e))
toolAction(e);
}
});
...
}
The complete code for the Paintview class will be described in a later article. The code snippet above shows just how to use the prototype to make technical decisions.
Summarize
Prototypes play an important role in the application development process, allowing you to test your ideas and get user feedback as early as possible. I don't think of prototypes as code snippets that can be discarded when "real" development starts. Instead, prototypes should be the basis of your product or application. This means you should be careful to encode it, although your class or method will be rewritten later.