paintComponent on JPanel, image flashes and then disappears

Posted by mark on Stack Overflow See other posts from Stack Overflow or by mark
Published on 2012-12-15T10:48:30Z Indexed on 2012/12/15 11:03 UTC
Read the original article Hit count: 228

Filed under:
|
|
|

I have a JApplet (MainClass extends JApplet), a JPanel (ChartWindow extends JPanel) and a Grafico class.

The problem is that the Grafico class instance has 2 JPanel that should show 2 images (1 for each panel) but the images are shown and after a little while they disappears: instead of them i get a gray background (like an empty JPanel). This happens for every repaint() call (that are made in the ChartWindow class)

the MainClass init() contains

chartwindow=new ChartWindow();
add(chartwindow)

chartwindow has a Grafico instance.

it's the ChartWindow's paintComponent (override)

paintComponent(Graphics g)
{
    super.paintComponent(g);
    Image immagineGrafico=createImage(grafico.pannelloGrafico.getWidth()
          ,grafico.pannelloGrafico.getHeight()); 
     Image immagineVolumi=createImage(grafico.pannelloVolumi.getWidth()
      ,grafico.pannelloVolumi.getHeight());
  Graphics2D imgGrafico=(Graphics2D)immagineGrafico.getGraphics();
  Graphics2D imgVolumi=(Graphics2D)immagineVolumi.getGraphics();
  grafico.draw(imgGrafico,imgVolumi,mouseX,mouseY);

  ((Graphics2D)grafico.pannelloGrafico.getGraphics()).drawImage(immagineGrafico,0,0,this);
  ((Graphics2D)grafico.pannelloVolumi.getGraphics()).drawImage(immagineVolumi,0,0,this);
}

grafico's JPanels are added this way in the ChartWindow's constructor

grafico=new Grafico()
................
add(grafico.pannelloGrafico);
add(grafico.pannelloVolumi);

Tell me if you need more information, thank you very much :-)

© Stack Overflow or respective owner

Related posts about java

Related posts about swing