开发者

Intermediate image

I have a problem with intermediate image. The image is showing only once. After i move the image "line" is not showing anymore.

public void paintLine(Graphics g) {
    if (lin开发者_运维知识库e == null) {
        line = new BufferedImage(1, height, BufferedImage.TYPE_INT_ARGB);

        Graphics gImg = line.getGraphics();
        float[] data = datas[index];
        for (float f : data) {
            float[] rgb = ColorMap.getPixelColor(f);
            gImg.setColor(new Color(rgb[0], rgb[1], rgb[2]));
            gImg.drawRect(0, (int)yPos--, 1, 1);
        }
        gImg.dispose();
    }
    xIncr++;
    g.drawImage(line, (int)xPos - xIncr, (int)yPos, null);
    graph.repaint();
}

This method is called in paintComponent ofJPanel. If i recreate the image "line" each time, it is displaying properly with really poor performance.


Emm... I am not pretty sure owing to less information in your question but, as a rule, image may disappear because of doubleBuffered(false) option. So you need to set the double buffered option to true manually for your canvas. Code something like this

public class MyLabel extends JLabel{


public MyLabel()
{

this.setDoubleBuffered(true);

}

public void paintComponent(Graphics g)
{
  this.paintLine(g);
}

public void paintLine(Graphics g) {
    if (line == null) {
        line = new BufferedImage(1, height, BufferedImage.TYPE_INT_ARGB);

        Graphics gImg = line.getGraphics();
        float[] data = datas[index];
        for (float f : data) {
            float[] rgb = ColorMap.getPixelColor(f);
            gImg.setColor(new Color(rgb[0], rgb[1], rgb[2]));
            gImg.drawRect(0, (int)yPos--, 1, 1);
        }
        //gImg.dispose();
    }
    xIncr++;
    g.drawImage(line, (int)xPos - xIncr, (int)yPos, null);
    graph.repaint();
}
}

Now I guess the conception is more clear

Good luck

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜