3

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 :-)

4

1 回答 1

2

You need to override the JPanel's paintComponent rather than the chart window's if you want to paint on them. What happens is that everytime the JPanel paints itself the default paint would overwrite your images.

     class PanelloVolumi extends JPanel{
         //some code

         public void paintComponent(Graphics g){

             //paint one image here
         }

     }

And do the same for the other JPanel.

Then add instances of these JPanels to your Applet.

于 2012-12-15T10:52:31.613 回答