How to combine remixes in Swing?

I call the correspondence several times from the listeners, but the way I created my drawing function is only one redraw. I am creating a bunch of reviewers as it is connected to my mouse movement listener.

Is there a way to undo all incomplete copies for a specific component? I can't just start ignoring repaints, as some of them are valid, for example, when you resize a frame or restore it from a minimum.

Why does it bother me? Because my paint code is very heavy and I cannot do full redraws at very high FPS.

+3
source share
5 answers

Swing will combine remixes for you: see "Painting in AWT and Swing" on the Sun website. If you plan multiple repeats in quick succession, they are combined into one call to draw immediately ().

+2
source

Frequent redraw requests are automatically collapsed into one. The best way to optimize this is not to repaint it all, but to redirect the call with the coordinates of a specific area. This means that you are redrawing only the area that has actually changed.

+2
source

, repaint() , . , . , . , , .. . . JComponent.repaint RepaintManager.addDirtyregion.

+1

- , , JFreechart , .

, :

  • ScheduledExecutorService
  • submit , EDT, , 50 ,
  • - , (fut.isDone()), , ; .

, 20 .

fireDataTableChanged, .

0

, . . " " - Sun , , , , .

...

public void mouseDragged(MouseEvent e) {
            updateSize(e);
        }

        public void mouseReleased(MouseEvent e) {
            updateSize(e);
        }

        /* 
         * Update the size of the current rectangle
         * and call repaint.  Because currentRect
         * always has the same origin, translate it
         * if the width or height is negative.
         * 
         * For efficiency (though
         * that isn't an issue for this program),
         * specify the painting region using arguments
         * to the repaint() call.
         * 
         */
        void updateSize(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            currentRect.setSize(x - currentRect.x,
                                y - currentRect.y);
            updateDrawableRect(getWidth(), getHeight());
            Rectangle totalRepaint = rectToDraw.union(previousRectDrawn);
            repaint(totalRepaint.x, totalRepaint.y,
                    totalRepaint.width, totalRepaint.height);
        }

(. )

.

In truth, I have a similar problem with FPS, but that might be due to my bad code at the moment! I've learned so much over the past few months that I can now make my code more efficient. I hope I can overcome the FPS problem when more than 2 "people" slow down my schedule! Hummmm ... I just implemented the above code for the same section in my code and not in others, but by all means try!

0
source

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


All Articles