简体   繁体   English

java jpanel与repaint()同步还是:侦听器是否可添加到swing的重画?

[英]java jpanel synchronization with repaint() or: Is a Listener addable to swing's repaint?

I'm writing a plugin for a miscropy program and have problems with the repaint() method. 我正在为一个杂项程序编写插件,并且repaint()方法有问题。

short question: Is there any way to get informed as soon as the repaint of a JPanel was done or synchronize the code with it? 简短的问题:JPanel的重绘完成后,是否有任何方法可以通知您或与之同步代码?

detailed version: 详细版本:

My program can plot a set of data in a xy-chart to a JPanel and show it using jfree.chart; 我的程序可以将xy图表中的一组数据绘制到JPanel并使用jfree.chart进行显示。 In another part of the programm I have many datasets (~100) that I want to plot and save as images. 在程序的另一部分中,我有许多要绘制并保存为图像的数据集(约100个)。 I've also found a solution, but I really don't like it. 我也找到了解决方案,但我真的不喜欢它。 The Problem can be reduced to a notification about the paint status of a JPanel. 可以将问题简化为有关JPanel绘画状态的通知。

In the part that shall save all images I have this solution: 在应保存所有图像的部分中,我有以下解决方案:

PlotSpectrum spectrumWindow = getTheWindow(); //pseudo code...
// some stuff
ti = storage.getImage(channel, slice, frame, position);
spectrumWindow.plotData(false, andor.captureSpectrum(ti.pix), wave,
                 centerWave, fineGrating, exposureTime,
                 slitWidth, substractBackground);
spectrumWindow.repaint(); // probably not necessary
sleep(100);               // this annoys me...
spectrumWindow.savePlot(path, true, config, null);

spectrumWindow is a JPanel that is also displayed in another window and it all works fine. spectrumWindow是一个JPanel,它也显示在另一个窗口中,并且一切正常。 BUT I really don't like that sleep(100) in there... without it I'm asking for a repaint but it isn't done till I try to save a "snapshot" of (thats what savePlot is doing...). 但是我真的不喜欢那儿的sleep(100)...没有它,我要重新粉刷,但是直到我尝试保存“快照”(即savePlot正在做的事情。 )。 I know, other Thread and these damn synchronization problems... With the sleeping I'm just making it unnecessary slow and if I wait not long enough the images are not completly drawn (eg lower half missing) 我知道,其他线程以及这些该死的同步问题...随着睡眠的进行,我只是使其变得不必要的缓慢,如果等待的时间不够长,图像将无法完全绘制(例如,缺少下半部分)

Is there any way to get informed as soon as the repaint was done? 重涂完成后,是否有任何方法可以得到通知? I probably would be also fine with a Listener, better would be a solution with a monitor or sth comparable or a method that is repainting NOW (doesn't exists as far I know?) The main GUI (include the JPanel spectrumWindow) and the earlier pasted code are running in different Threads. 我可能对监听器也没问题,最好是与监视器或其他类似的解决方案,或者现在重新绘制的方法(据我所知不存在吗?)主GUI(包括JPanel SpectrumWindow)和早期粘贴的代码在不同的线程中运行。

The probably also important parts of my code are following here. 我的代码中可能也很重要的部分如下。 Please excuse if some brackets aren't matching or some variables aren't declared, I removed very much code. 如果某些括号不匹配或未声明某些变量,请原谅,我删除了很多代码。

thanks schetefan24 谢谢schetefan24

class PlotSpectrum extends ApplicationFrame // that extends JFrame
{
public void plotData(boolean backgroundGiven, int[] spect, double[] wave_,
                     double centerWave, boolean fineGrating_, double exposureTime,
                     double slitWidth, boolean substractBackground)
{
//process data and store internally
replot();
}

private void replot()
{
XYSeries series = new XYSeries("Spectrum");
//add data to series
XYSeriesCollection collection = new XYSeriesCollection(series);
//setting up some labels and such stuff...
 JFreeChart chart = ChartFactory.createXYLineChart(
        title,
        "Wavelength [nm]", 
        yLabel, 
        collection,
        PlotOrientation.VERTICAL,
        false,
        false,
        false
    );
 dataPanel.add(new ChartPanel(chart)); // this is contained in a Frame
}

public void savePlot(String path, boolean overWriteAll, HashMap<String,String> config, int[][] addData)
{
File output = new File(path);
//some more stuff, ask overwrite etc
if(image)
{
    BufferedImage im = createImage();
    String extension = path.substring(path.lastIndexOf(".")+1, path.length());
    ImageIO.write(im, extension, output);
} else {
//that is an textexport, works fine
}
}

public BufferedImage createImage()
{
    JPanel panel = (JPanel) flipChart.getSelectedComponent();
    int w = panel.getWidth();
    int h = panel.getHeight();
    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bi.createGraphics();
    panel.paint(g);
    return bi;
}
}

that I want to plot and save as images. 我想绘制并另存为图像。

  1. add the data to a non visible panel. 将数据添加到不可见的面板。
  2. create a BufferedImage of the panel 创建面板的BufferedImage
  3. create an ImageIcon using the Image from above 使用上方的图片创建一个ImageIcon
  4. update a JLabel (that has already been added to the frame) using the setIcon(...) method 使用setIcon(...)方法更新JLabel (已添加到框架中)
  5. the above step should generate a PropertyChange event when the Icon changes. 当图标更改时,以上步骤应生成一个PropertyChange事件。 You can use a ProperChangeListener to listen for this event. 您可以使用ProperChangeListener侦听此事件。 When you receive the event you can repeat steps 1 - 4. 收到事件后,您可以重复步骤1-4。

Check out Screen Image . 签出屏幕图像 It will help you create an image of a non-visible component. 它将帮助您创建不可见组件的图像。

Note, you don't really need steps 4-5. 请注意,您实际上并不需要步骤4-5。 I just added them so you have a visual of the plots as they are being processed. 我只是添加了它们,因此您可以直观地看到正在处理的绘图。 If you don't want the visual then maybe you just display text on a JLabel indicating which plot is currently being converted. 如果您不想要视觉效果,那么也许您只是在JLabel上显示文本,指示当前正在转换哪个绘图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM