-1

I have a project in which I have several components, . The problem is that when the repaint () command is executed, the JLabels are not painted. I want to make it clear that it is NOT a problem that there are graphics that are being painted on or anything. Apparently what happens is that within repaint (), when I force the JLabel to repaint, they do not paint unless you change the JLabel chain.

Initialization of the JLabel and define its parameters:

puntuacionL.setFont(new Font("Marker Felt", Font.PLAIN, 20));
                puntuacionL.setBounds(710, 212, 150, 30);
                puntuacionL.setOpaque(true);
                puntuacionL.setBackground(Color.CYAN);
                puntuacionL.setForeground(Color.white);
                puntuacionL.setVisible(true);


                comoJugar.setFont(new Font("Marker Felt", Font.PLAIN, 20));
                comoJugar.setBounds(710, 245, 150, 30);
                comoJugar.setOpaque(true);
                comoJugar.setBackground(Color.CYAN);
                comoJugar.setForeground(Color.white);
                comoJugar.setVisible(true);

I have tried two options to force the redefinition of JLabel:

1. mietiqueta.setText("whatever"). In this option what I do is to redefine in the repaint () label string.

2. Repaint the component: mietiqueta.paintComponent(g)

I would have uploaded a capture of the result, which would have been very useful, but you need at least 10 reputation points

Repaint() method:

 @Override
            public void update(Graphics g){
                paint(g);
            }
            @Override
            public void paint(Graphics g) {
                if (offGraphics == null)  {

                offImage = createImage(900,900);
                offGraphics = (Graphics2D) offImage.getGraphics();
                }

                puntuacionL.setText("  Puntuación: "+Integer.toString(puntuacion));
                comoJugar.setText("  Pulsa H para ayuda");
                Graphics2D g2d = (Graphics2D) g;
                puntoIncialBala();
                Image fondo = new ImageIcon(getClass().getResource("/imagenes/fondo.jpeg")).getImage();
                Image img2 = new ImageIcon(getClass().getResource("/imagenes/fondoMapa.png")).getImage();
                offGraphics.drawImage(img2, 30, 30, this);
                int posVidaX = 710;
                offGraphics.setColor(Color.CYAN);
                offGraphics.fillRoundRect(710, 30, 150, 100, 10, 10);

                for (int k = 0; k < vidas; k++) {
                    Image vida = new ImageIcon(getClass().getResource("/imagenes/vida.png")).getImage();
                    offGraphics.drawImage(vida, posVidaX, 50, this);
                    posVidaX = posVidaX + vida.getWidth(this);
                }
                timeLabel.paintComponents(g);
                for (int k = 0; k < arrayCasilla.size(); k++) {
                    offGraphics.drawImage(arrayCasilla.get(k).getImg(), (int) arrayCasilla.get(k).getY(), (int) arrayCasilla.get(k).getX(), this);
                }
                Image img = new ImageIcon(getClass().getResource("/imagenes/BalaCanon.png")).getImage();

                if (flagBala == true) {
                    //puntuacionL.setText("  Puntuación: "+Integer.toString(puntuacion));
                    if (pintarCasilla == true) {
                        puntuacionL.setText("  Puntuación: "+Integer.toString(puntuacion));
                        comoJugar.setText("  Pulsa H para ayuda");
                        queHayCasilla();
                        if (casilla[x][y].getTipo().compareTo("cangrejo") == 0) {
                            puntuacion = puntuacion + 1;
                        } else if (casilla[x][y].getTipo().compareTo("ron") == 0) {
                            posVidaX = 710;
                            //puntuacion = puntuacion + 1;
                            vidas--;
                            if (vidas == 0) {
                                timer.stop();
                                etiquetaFin.setVisible(true);
                                finalizar.setVisible(true);
                                contenedorFinal.setVisible(true);
                            }
                            offGraphics.setColor(Color.CYAN);
                            offGraphics.fillRoundRect(710, 30, 150, 100, 10, 10);
                            for (int k = 0; k < vidas; k++) {
                                Image vida = new ImageIcon(getClass().getResource("/imagenes/vida.png")).getImage();
                                offGraphics.drawImage(vida, posVidaX, 50, this);
                                posVidaX = posVidaX + vida.getWidth(this);
                            }

                        } else if (casilla[x][y].getTipo().compareTo("cofre") == 0) {
                            puntuacion = puntuacion + 2;

                        }
                        if (posY[y] < 441) {
                            arrayCasilla.add(casilla[x][y]);
                        }
                        for (int k = 0; k < arrayCasilla.size(); k++) {
                            if (posY[y] < 441) {
                                offGraphics.drawImage(arrayCasilla.get(k).getImg(), (int) arrayCasilla.get(k).getY(), (int) arrayCasilla.get(k).getX(), this);
                            }
                        }
                        animacion.stop();
                        if (!animacion.isRunning()) {
                            i = 0;
                        }
                        pintarCasilla = false;
                        flagBala = false;
                    } else {
                        offGraphics.drawImage(img, (int) x1, (int) x2, this);
                    }

                }
                try {
                    imgB = ImageIO.read(getClass().getResource("/imagenes/canon.png"));
                } catch (IOException ex) {
                    Logger.getLogger(PanelCanon.class.getName()).log(Level.SEVERE, null, ex);
                }
                offGraphics.setColor(Color.gray);
                offGraphics.fillRect(30, 720, 646, 122);

                AffineTransform tx = AffineTransform.getRotateInstance(Math.PI / 2 - anguloRotacion, imgB.getWidth(this) / 2, imgB.getHeight(this) / 2);
                AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
                tx.rotate(Math.PI / 2 - anguloRotacion);
                System.out.println(tx.toString());
                offGraphics.drawImage(op.filter(imgB, null), 360, 740, null);
                g2d.drawImage(offImage, 0, 0, this);

                puntuacionL.setText("  Puntuación: "+Integer.toString(puntuacion));
                comoJugar.setText("  Pulsa H para ayuda");
                System.out.println("Posicion bala x" + x1);
                System.out.println("Posicion bala y: " + x2);

            }

I have a timer that may be interfering in some way, this timer is responsible for controlling the countdown.

Timer code:

     timer = new Timer(1000, new ActionListener() {
     @Override
       public void actionPerformed(ActionEvent e) {

         if (seconds == 0 && minutes == 0) {
             timer.stop();
             etiquetaFin.setVisible(true);
             finalizar.setVisible(true);
             contenedorFinal.setVisible(true);
         } else if (seconds > 0) {
             seconds--;
         } else if (minutes > 0) {
             minutes--;
             seconds = 59;
         }
             revalidate();
     //puntuacionL.paintImmediately(710, 212, 150, 30);
     //puntuacionL.setText("  Puntuación: "+Integer.toString(puntuacion));
     //comoJugar.setText("  Pulsa H para ayuda");
     timeLabel.setText("   "+timeFormatter.format(minutes) + ":"  + timeFormatter.format(seconds));

   }
});
timer.start();

And this is where the magic happens and I do not understand anything anymore. As you can see inside the event I have commented the lines:

puntuacionL.setText("  Puntuación: "+Integer.toString(puntuacion));
comoJugar.setText("  Pulsa H para ayuda");

If within the event of the Timer I redefine the Labels, with a different string from the paint () they are painted, but of course it does not work for me, since I want the string not to be modified.

I have not put the whole class because it is quite large, but if someone still needs the complete context, happy to edit the question and put all the code.

deadlock
  • 164
  • 1
  • 14

1 Answers1

0

I am really not sure what you are trying to do, but you appear not to understand the basic concepts of using components and of custom painting.

Here are the problems I see:

  1. Don't override update(...). There is no reason to do this.

  2. Don't override paint(). Custom painting is done by overriding the paintComponent(...) method of the component.

  3. Don't read images in the painting method. A component is repainted when you specifically invoke repaint() on the component or when Swing determines the components needs to be painted. Images should be read in the constructor of your class to make the painting code as efficient as possible.

  4. Don't change the properties of your class in the painting method. This would especially apply to the changing the text of an external component like a JLabel.

  5. Don't invoke paintComponents() on any component from within the painting method.

  6. Don't access the Timer in the painting method. Again, the whole purpose of the painting code is to paint the current state of the component, not alter the state. The purpose of the Timer is the change the state of the component every time it fires. So it is the Timer logic that should be changing the text on the JLabel. The JLabel will then automatically repaint itself. See: Program freezes during Thread.sleep() and with Timer for a basic example of undateing a JLabel from withing a Timer.

Read the section from the Swing tutorial on Custom Painting for more information on the basic of painting.

camickr
  • 321,443
  • 19
  • 166
  • 288