首页 > 解决方案 > 为什么不重绘 JLabel?

问题描述

我有一个项目,其中有几个组件,. 问题是当执行repaint() 命令时,JLabels 没有被绘制。我想明确表示,正在绘制的图形或任何东西都不是问题。显然,在 repaint() 中,当我强制 JLabel 重新绘制时,除非您更改 JLabel 链,否则它们不会绘制。

初始化 JLabel 并定义其参数:

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

我尝试了两个选项来强制重新定义 JLabel:

1 mietiqueta.setText("whatever") .. 在这个选项中,我所做的是在repaint ()标签字符串中重新定义。

2. 重绘组件:mietiqueta.paintComponent(g)

我会上传结果的捕获,这将非常有用,但您至少需要 10 个声望点

重绘()方法:

 @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);

            }

我有一个可能会以某种方式干扰的计时器,这个计时器负责控制倒计时。

定时器代码:

     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();

这就是魔法发生的地方,我什么都不懂了。正如您在活动中看到的那样,我评论了以下几行:

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

如果在 Timer 事件中我重新定义标签,使用与paint ()它们不同的字符串被绘制,但当然它对我不起作用,因为我希望不修改字符串。

我没有放整个课程,因为它很大,但是如果有人仍然需要完整的上下文,很高兴编辑问题并放上所有代码。

标签: javaswing

解决方案


我真的不确定您要做什么,但是您似乎不了解使用组件和自定义绘画的基本概念。

以下是我看到的问题:

  1. 不要覆盖更新(...)。没有理由这样做。

  2. 不要覆盖paint()。自定义绘制是通过覆盖paintComponent(...)组件的方法来完成的。

  3. 不要在绘画方法中阅读图像。当您在组件上专门调用 repaint() 或 Swing 确定需要绘制组件时,组件将被重新绘制。应该在类的构造函数中读取图像,以使绘画代码尽可能高效。

  4. 不要在绘画方法中更改类的属性。这尤其适用于更改外部组件(如 JLabel)的文本。

  5. 不要在绘画方法中对任何组件调用paintComponents()。

  6. 不要在绘画方法中访问 Timer。同样,绘制代码的全部目的是绘制组件的当前状态,而不是改变状态。Timer 的目的是在每次触发时更改组件的状态。因此,应该更改 JLabel 上的文本的是 Timer 逻辑。JLabel 然后会自动重新绘制自己。请参阅:Program freezes during Thread.sleep() and with Timer以了解从使用 Timer 中取消 JLabel 日期的基本示例。

阅读关于自定义绘画的 Swing 教程中的部分,以获取有关绘画基础的更多信息。


推荐阅读