Duvida update JApplet

0 respostas
D

Olé galera, tudo certo ?
Estou com uma duvida em um applet, preciso fazer um doubleBuffer no applet e pra isso estou usando o método update, que teoricamente teria q ser chamado no repaint() antes de chamar o metodo paint, o problema é que meu applet q está chamando o metodo update nem mesmo uma vez. Coloquei um marcador nele pra saber e ele realmente não chama o update() no repaint. Alguem sabe oq pode estar acontecendo? Segue o meu cod. Obs: Estou editando no netbeans..

import java.awt.*;
import javax.swing.JPanel;

public class appletFluxoCalor extends javax.swing.JApplet implements Runnable{
int largura, altura, x;

Image offImage;
Graphics offGraphics;
Thread anima;
boolean inicia;

    int teste[]= {0,0};
    
    public void init() {
       
       largura = 450; altura = 400;
       setSize(largura, altura);
        
    
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(appletFluxoCalor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(appletFluxoCalor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(appletFluxoCalor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(appletFluxoCalor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the applet */
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {

                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    
    
    public void update(Graphics g)
    { 
System.out.printf("UPDATE teste: %d\n",teste[0]);
teste[0]++;
    Graphics2D g2d = (Graphics2D) g.create();

    offImage = createImage(getSize().width, getSize().height);
    offGraphics = offImage.getGraphics();
        
        offGraphics.setColor(getBackground());
        offGraphics.fillRect(0, 0, getSize().width, getSize().height);
        offGraphics.setColor(Color.black);
        
        g.drawImage(offImage, 0, 0, null);
        
        paint(offGraphics);
        g2d.dispose(); 
    }
    
    public void paint(Graphics g){
      if(inicia==false)
        super.paint(g);
      
      System.out.printf("PAINT teste %d\n",teste[1]);
      teste[1]++;
        g.clearRect(0, 0, getSize().width, getSize().height-jPanel_painelControle.getSize().height);
        g.drawRect(x, 0, 20, 50);
        
    }
    
    public void start(){
     anima = new Thread(this);
     anima.start();
    }
    
    public void stop(){
        if(anima!=null)
            anima.stop();
            anima=null;
    }
    
    public void run(){
    while(inicia){
        x++;
        dormir();
        repaint();
        }
    }
    
    public void dormir(){
        try{Thread.sleep(20);
        }catch(InterruptedException e){}
    }
    
private void jButton_iniciaActionPerformed(java.awt.event.ActionEvent evt) {                                               
if(inicia==false){
    anima = new Thread (this);
    anima.start();
    inicia=true;
  }
    
}                                              

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
x=10;
repaint();
}                                        

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton_inicia;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel_painelControle;
    // End of variables declaration                   

}

Obs: Eu retirei a parte do generated code !

Criado 24 de agosto de 2011
Respostas 0
Participantes 1