[Original] Building a desktop application prototype (Part III)

xiaoxiao2021-03-06  86

[Original] Building a desktop application prototype (Part III)

Technical decision

During the development process, I have to solve some technical problems and do some technical decisions. The following code snippet is just simple, but they will be described in detail in the article later. It is important to understand the role acting as a prototype. Use your prototype to find technical problems to test uncommon APIs and ensure your application performance.

Multi-storey PANELS

Build a graphical application of a discipline in Windows is not a very complex task. You must handle mouse events, draw lines, painting rectangles, and painting. It is also necessary to deal with the deformation function, such as from a basic application to a professional graphics editor with more work such as mobile, scaling, reordering, deleting, copying, cutting, and pasting. You can also want to include a text box that can be edited, rescan the size, and text packages. Build your own style text editor is unnecessary, because Swing has provided some text components. How do you integrate Swing text editors and your own drawing components? I consider 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 put text components within JinternalFrames. With the second solution, Swing has provided changing the size and mobile function, but the following problem is how you draw an image in a built-in Frame containing text comments? And how do you draw other simple graphics on JDESKTOPPANE, such as a straight line, rectangle, and ellipse? Fortunately, there is a simple solution because JDesktoppane is a real multi-storey PANEL. The prototype MAINPANE class expands JDESKTOPPANE, there are two floors. One of them contains a PaintView custom component, allowing you to draw simple graphics. The other layer contains text comments. Of course, if a comment image cannot be obtained by the program, then this 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 ();

Drawing outside of Paint ()

The drawing of Swing components is usually done in the inside of Paint () or in the PAINT () internal calling method. When you use a mouse to draw an object on the screen, you don't want to redraw other components because it will cause the application to run low efficiency. For example, the user is drawn with a pencil, and each mouse event is to draw a small line segment. There are hundreds of mouse_dragged events between mouse_pressed and mouse_released. When the user only draws some graphics on the screen, it is unacceptable to re-painting the PaintView component hundreds of such things. Note that the PaintView handles most of the drawing operations and a repaint requires all comments including text annotations to redraw. The correct solution is to obtain the graphic context 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 ();} PaintView component utilizes a mouse to listen to the mouse event. The above method is called by each event, and the CURRENTTOOL object is delegated. When the mouse is released, repaint () is called to request the refresh of the entire component. Therefore, the user completes the graphic object to draw Paint () is only called once. This is the code to register the mouse to listen:

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 full code of the PaintView class will be described in later articles. The above code snippet only shows how to use prototype to do technical decisions.

to sum up

The prototype has an important role in the application development process, allowing you to test your ideas and get user feedback as early as possible. I didn't think of the prototype as the code snippet that can be discarded when "truly" development begins. Instead, the prototype should be the basis of your product or application. This means you should be careful to encode it, although your class or method is to be rewritten later. In future articles, I will continue to describe Jimaging prototype code.

转载请注明原文地址:https://www.9cbs.com/read-121399.html

New Post(0)