How to draw a cancelable buffer?

While I do not do animation or draw very complex graphics.
I need (sort of) double buffering to draw multiple primitives. In the application, the user enters the name Shape, followed by the associated arguments, and this shape is drawn on the buffer and then on the screen. For example, the command RECT 100, 50, 200, 120 .

For perseverance, I can keep all the commands in the list and in
public void paintComponent(Graphics g) from JPanel I draw them one by one.
But this is very inefficient due to repeating the list and using a hash map each time to call (or send) the corresponding shape-drawing interface.

How and on what type of buffer can I draw? How can different methods use this buffer?

Also, is there a convenient way to cancel previous draws using a buffer? Or do I need to redraw the buffer every time I cancel?

Thanks, I do not need the full code, but the corresponding class names and a small pseudo-code are appreciated.

+4
source share
1 answer

JPanel is buffered by default by default, so selecting and dragging and dropping is usually pretty smooth. GraphPanel is a simple object drawing program that illustrates such operations on a List<Node> . A similar approach has been successfully used with thousands of nodes.

The details of implementing the cancellation strategy are highly dependent on the desired behavior, but remove() and repaint() effective.

Appendix: One general optimization uses flies to render a large number of objects . JTable , JFreeChart and JGraph are examples. This simplified example illustrates an essential mechanism.

+6
source

Source: https://habr.com/ru/post/1397261/


All Articles