Multithreading for java graphics

I have a Java application that transfers the source data and accordingly draws real-time graphs. this is handled by calling methods from class i, which uses the Graphics object. I implemented the algorithms in the overridden paintComponent method to generate all the graphs from the most recent data. I have other methods in my class for updating the variables used in the paintComponent method to draw graphs.

in my main class, I periodically update my schedules in the timer event handler. in the event handler, I call methods from my graph class that update certain variables, do a few calculations, and then call redraw () (which, apparently, is the correct way to call the paintComponent method).

My problem is that the algorithms that I use in the paintComponent method can take (relatively) a long time depending on the number and resolution of my patches. (I have not encountered this problem yet, but now I am trying to address it). Of course, I would not want this whole chart to hang all the time my application was processing, so I was wondering if it is possible that paintComponent is executed in a separate thread.

What happens if I create a subclass in my main program to run in a separate thread and just call the graph methods described? Do all these methods (including paintComponent) automatically execute in a new thread? or do I have to change my graph class to make it work? Ideally, I would like to avoid modifying the graph class because I already designed it to work in the NetBeans GUI builder as JPanel, and I would like to avoid breaking this functionality.

+4
source share
4 answers

you need to redirect the drawing methods to SwingWorker or Runnable#Thread (all output in the GUI must be wrapped in invokeLater ), for example here or here

+4
source

There are several options.

One way is to use two BufferedImages, where you draw one in a separate stream and draw from the other, and switch at the end of the drawing (for what I assume, this is a snapshot so often.)

A much better solution is to have a model of directly visualized data (as in the data that it stores, it can be carried out without any further algorithmic work on it).

This means that you will execute your alogirthms in a separate thread, calculate the values ​​that will be used for drawing, call SwingUtilities.invokeLater to update the model. Then the model will be updated only in the Swing stream, and when you redraw, you have access to exactly the data that you need for drawing (and without extraneous data).

If this data is still so much that drawing takes a lot of time (i.e. if you draw diagrams with tons of data points), you will send to calculate which parts of your window need to be repainted and rewritten () only on this. However, this piece should be a lat resort. 99% of your productivity will depend on moving the algorithms to a separate stream and providing the painter access to directly visualized data.

If you look at the recommendations for updating TableModel with external data, then you have a job that receives the data happening in the background thread (usually SwingWorker) and then is sent to the actual model via invokeLater () (This is why the data does not change until your paint () tries to read it.), and then fires the appropriate events from the model update, which tell the table which cells have been changed. The table then knows how much of its viewport needs to be repainted and calls the corresponding repaint () method. During this time, the background thread may continue to retrieve data and add new updates to the event queue through invokeLater.

+5
source

Well, if you want to improve the responsiveness of the GUI, you can do a long job in SwingWorker , although I do not know that this will speed up your application.

+1
source

I have a Java application that transfers the source data and draws real-time plots accordingly. This is handled by calling methods from the class I wrote that uses the Graphics object.

To execute another answer: you really have to use JFreeChart . This is a good library for drawing diagrams, and you can dynamically change the displayed dataset (and do much more).

+1
source

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


All Articles