Salve a tutti.
Sono nuovo e non sò se sto scrivendo nella sezione corretta...
Sto cercando di scrivere una piccola applicazione java che simula il tetris, ma mi sono imbattuto in un curioso problema:
i colori dei blocchi (rappresentati tramite Jlabel) non vengono visualizzati se non alla fine dell'animazione, qualcuno sa dirmi perchè?
Questo è il sorgente (è solo una bozza provvisoria)
come editor uso netbeans 6.5 e la versine del java la jdk1.6.0_13 su un pc con windows XP sp3codice:public class provaForme extends javax.swing.JFrame { static final int DIM_BUTTON = 20; //public Dati d; public int N = 11, M = 21; int riga, col; boolean blocchi[][]; boolean visitati[][]; Label[][] labelSfondo = new Label[N][M]; javax.swing.JTextField jTextField1; javax.swing.JButton jButton1; javax.swing.JLabel[][] label = new javax.swing.JLabel[N][M]; /** Creates new form provaForme */ public provaForme() { initComponents(); this.setTitle("TETRIS"); this.setSize(DIM_BUTTON * N + 7, DIM_BUTTON * M + 94); this.setBounds(640 - (this.getWidth() / 2), 400 - (this.getHeight() / 2), DIM_BUTTON * N + 7, DIM_BUTTON * M + 94); posizionaBottoni(); //gioca(); } public void posizionaBottoni() { blocchi = new boolean[N][M]; visitati = new boolean[N][M]; //bottoni = new javax.swing.JToggleButton[N][M]; int riga; int col; int numeroBottone = 0; jTextField1 = new javax.swing.JTextField(); jButton1= new javax.swing.JButton("Start"); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(null); getContentPane().add(jTextField1); getContentPane().add(jButton1); jTextField1.setBounds(0, (DIM_BUTTON * M), 80, 40); jTextField1.setEditable(false); jButton1.setBounds(100, (DIM_BUTTON*M),80,40 ); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { forma1(); } }); // for (riga = 0; riga < N; riga++) { for (col = 0; col < M; col++) { label[riga][col] = new javax.swing.JLabel(); labelSfondo[riga][col] = new Label(); label[riga][col].setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(255, 255, 255), new java.awt.Color(0, 0, 51))); label[riga][col].setOpaque(true); label[riga][col].setVisible(false); label[riga][col].setBounds(riga * DIM_BUTTON, col * DIM_BUTTON, DIM_BUTTON, DIM_BUTTON); labelSfondo[riga][col].setBounds(riga * DIM_BUTTON, col * DIM_BUTTON, DIM_BUTTON, DIM_BUTTON); labelSfondo[riga][col].setBackground(Color.BLACK); labelSfondo[riga][col].setVisible(true); getContentPane().add(label[riga][col]); getContentPane().add(labelSfondo[riga][col]); //label[riga][col].setMargin(new java.awt.Insets(0, 0, 0, 0)); //numeroBottone++; } } //forma1(); } //blocco quadrato blu public void forma1() { labelSfondo[5][0].setVisible(false); labelSfondo[6][0].setVisible(false); labelSfondo[5][1].setVisible(false); labelSfondo[6][1].setVisible(false); label[5][0].setBackground(Color.BLUE); label[6][0].setBackground(Color.BLUE); label[5][1].setBackground(Color.BLUE); label[6][1].setBackground(Color.BLUE); label[5][0].setVisible(true); label[6][0].setVisible(true); label[5][1].setVisible(true); label[6][1].setVisible(true); label[5][0].setIcon(new javax.swing.ImageIcon(getClass().getResource("bomba2.PNG"))); // NOI18N //ritardo animazione try { Thread.sleep(100); } catch (Exception e) { } //ciclo che fa scorrere il blocco for (riga = 0; riga < M - 2; riga++) { labelSfondo[5][riga].setVisible(true); labelSfondo[6][riga].setVisible(true); labelSfondo[5][riga+2].setVisible(false); labelSfondo[6][riga+2].setVisible(false); label[5][riga].setVisible(false); label[6][riga].setVisible(false); label[5][riga+2].setVisible(true); label[6][riga+2].setVisible(true); label[5][riga+2].setBackground(Color.BLUE); //label[10][riga+2].setIcon(new javax.swing.ImageIcon(getClass().getResource("bomba2.PNG"))); // NOI18N label[6][riga+2].setBackground(Color.BLUE); try { Thread.sleep(100); } catch (Exception e) { } blocchi[5][riga + 2] = true; blocchi[6][riga + 2] = true; blocchi[5][riga] = true; blocchi[6][riga] = true; } } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(null); pack(); }// </editor-fold> /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new provaForme().setVisible(true); } }); } // Variables declaration - do not modify // End of variables declaration }

Rispondi quotando
