Implementation of Java Sketchpad

I believe many people have used the drawing board of windows. This time, let's talk about the implementation of the simple drawing board of java version. The basic idea is as follows: the implementation of the drawing board is roughly divided into three parts: the first is the implementation of the drawing board interface, the second is the monitoring and drawing of the drawing board, and the third is the redrawing of the drawing board. (the article is long, but the code is progressive step by step. You can look at it separately according to three parts. After realizing the current part, go to the next part.) the first is the implementation of the interface of the sketchpad, because I didn't find the specific icon. All components on the interface are swing's own components, so the interface is a little rough, but if you want to optimize it, it's also simple to replace all components on the interface Change to a custom icon. After the implementation of the interface, you can consider adding monitoring to the components of the interface. Different graphics can add different monitoring methods according to the specific situation. Then write the event processing class and write the specific algorithm of drawing according to different graphics. A simple version of the drawing board can almost be realized. Redrawing will not be mentioned here until later.

Let's take a look at the implementation of the drawing interface:

The API classes required to implement the drawing interface mainly include flowlayout, GridLayout, color, dimension, JButton, JFrame and JPanel.

Define the draw class and let the draw class inherit the JFrame. Set its size, title, visibility, etc. It should be noted that if more buttons are added here, it is recommended to use an array to complete the addition of buttons, because adding buttons directly one by one will not only increase the amount of code, but also be detrimental to search, addition and code maintenance. In order to make the interface not so simple, several jpanels and different layout managers are used here. The main form uses the flow layout manager, and then uses three panels to load graphic buttons, color buttons and canvas respectively. The panel containing graphic buttons and color buttons uses table layout. Then the implementation of the interface is basically completed.

Here is another point to note. The order of adding JPanel panels should not be changed. This is the panel size calculated according to the streaming layout. Readers can change the debugging by themselves. Another is that the SetSize method of the form is only valid for the form itself. If you want to change the size of other components, use the setpreferredsize method. In this way, the basic interface of the drawing board is realized. The buttons and panels of the interface can be changed according to their own needs. This is the general appearance of the interface:

Implementation of monitoring:

Of course, it's no use having an empty interface. What we need is to click different buttons to realize different functions. Here you need to use the event monitoring mechanism. The main API classes for listening are: actionlistener, mouselistener, mousemotionlistener. The previous steps to add the method root of event listening are the same: determine the event source object, write the event processing class, and add the listening method. There are two kinds of event source objects in the drawing board, one is the button, and the other is the canvas panel. The button uses actionlistener, and the canvas panel uses mouselistener and mousemotionlistener because it is responsible for drawing. In order to realize event listening, we need to define an event processing class drawlistener, which implements the above three event interfaces. Then, different graphics are drawn in the rewritten method. The drawing of graphics can be realized by the method of graphics object. But there is a problem here. How to tell which button is pressed in the draw class? When defining the draw class, we use graphic buttons and color buttons. When instantiating the object, the buttons of the graphic class use the construction method with parameters, that is, the graphic buttons have text, while the color buttons do not. Based on this, it is easy to distinguish different buttons.

Drawlistener class:

The draw class also needs to be modified to add listening for buttons and panels:

Drawdrawlistener only describes the methods of drawing straight lines and curves. Readers can add them according to their own needs. The ideas and methods are the same. There are some points to note in the draw class. One is that the brush g must be obtained after the form is visible, otherwise the return value of the obtained brush object will be null. Second, to add listening for graphic buttons, the drawlistener needs to be instantiated before the setvisible method, so it is not recommended to use the construction method to directly pass in the G brush parameter. I use the set method. Finally, notice which addition method to use. The button uses the addactionlistener method, and the palette panel uses the addMouseListener and addMouseMotionListener methods. The purpose of using the sketchpad panel to obtain the brush and add monitoring to the picture panel is to prevent the graphics from running out of the panel when drawing. It is also possible to obtain the brush and monitoring from the main window, but there will be a problem of drawing the panel with lines during drawing.

Redrawing of Sketchpad:

Here, the production of drawing board has been basically realized, and we can draw all kinds of graphics on it. But careful people may find a problem, that is, if the form is minimized and opened again, all the things already painted on the drawing board will disappear. This is certainly not the case. How can the painstaking "masterpiece" be said to be gone without it. So why is there such a problem? To answer this question, we need to understand the drawing mechanism of Java. For drawing boards, we use swing components, which are developed based on the original AWT components. When drawing, we will call the drawing function of the system, which is why we can obtain the brush object from the panel or form. This means that all the forms, buttons or other components you can see in Java are actually drawn. Therefore, when we click the form to minimize it or change its size, the original painted form can not meet the needs. At this time, the system will call the paint method of JFrame to redraw the form, that is, draw a new form again, and the paint method of JFrame is only effective for the added components of the form, What we draw ourselves is not written in the paint method, so after the form is redrawn, the original drawing will disappear. To solve this problem, we need to override the paint method of the parent class. But then the problem comes again. Drawing is implemented in the drawlistener class. How do you get them into the paint method?

Of course, there may be many methods. Here I only introduce what I know: if you want to draw the drawn graphics again in the paint method, you need something to save the drawn graphics. Save can use arrays or collections. It is recommended to use collections here, which can be easily added without considering the size. The implementation of array is also similar, so I won't introduce it here. If the set is used, what type of data is stored in the set? There is no doubt that graph data should be saved, but if the collection uses generics, it can only save the same type of data, but we have so many graphs? Here you can use interfaces or abstract classes. We just need to create different graphic classes to inherit abstract classes or implement interfaces. Then every time you draw a graph, you instantiate a graph object and store it in the collection. Finally, you can traverse the collection in the paint method and redraw the graph.

The final code is pasted directly below (still only lines and curves are written), but a few lines of code are added. Pay attention to the comparison with the previous code.

Here, when using the collection to add graphics to realize the redrawing of the drawing board, I will create a new class to save the information of graphics every time I implement a graphics, so there will be many graphics classes. If you don't want to create so many graphic classes, you can put them in one class, set a type parameter and assign the name of the button. Then, in the draw method, judge what graphics are based on this value to realize different drawing methods. In this way, all the functions of the sketchpad are basically realized, and the project of the Sketchpad is here.

Summary:

The making of drawing board mainly uses swing components, event monitoring mechanism, graphics drawing, redrawing of drawing board and the use of collection, and the role of abstract classes or interfaces as standard graphics classes.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>