简体   繁体   English

未调用PaintComponent

[英]PaintComponent is not being called

I´m just starting to learn how to program so excuse me if my question is just silly. 我刚开始学习如何编程,如果我的问题很愚蠢,请原谅我。 I have been trying for over two days to find a solution for this problem and I just can't find it over the net so I need your help. 我已经尝试了两天以上找到解决这个问题的方法,但我无法通过网络找到它,所以我需要你的帮助。 Thanks in advance. 提前致谢。

So, I am trying to recreate the Parchisi game in Java. 所以,我正在尝试用Java重新创建Parchisi游戏。 I want to create a method that puts a counter in an specific position every time a player rolls the dice and obtains the number five as a result. 我想创建一种方法,每当玩家掷骰子并将结果获得数字5时,将计数器放在特定位置。 The counter has its own class, that is: 该计数器有自己的类,即:

package parchis;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Ficha extends JPanel
{
   public static int x;
   public static int y;
   public Image imagenficha;

  @Override
  public void paintComponent(Graphics g){

  super.paintComponent(g);

  System.out.println("Ejecutándose función de pintura de ficha");
  g.drawImage(imagenficha,x,y,this);
  g.setColor(Color.RED);
  g.fillRect(0,0,20,20);
}

 Ficha(int color, int locx, int locy, int ancho, int alto){

    this.setSize(60,60);
    System.out.println("El color es el "+Servidor.turno); 
    this.setBounds(locx,locy,ancho,alto); 
    x=locx;
    y=locy;
    this.setVisible(true);
} 

The checker is put over a jframe by a call of this method: 通过调用此方法将检查器放在jframe上:

public void pintarficha(){

    Ficha ficha = new Ficha(Servidor.turno,40,40,100,100);
    jframe.getContentPane().add(ficha);
    Refrescar();
}

Refrescar: Refrescar:

public static void Refrescar(){

   jpanel.add(jlabel);
   jframe.add(jpanel);
  jframe.pack();
}

The problem is that, when the method pintarficha() is called from outside a method (IE in the instantiation of one of my classes) it works properly and paints the counter, but when I put it inside of any method, PaintComponent is not being executed and I cannot understand why. 问题是,当方法pintarficha()从一个方法外部调用(在我的一个类的实例化中的IE)时,它正常工作并绘制计数器,但当我把它放在任何方法中时,PaintComponent不是执行,我不明白为什么。

Here it works: 在这里工作:

package parchis;
public class Administradordereglas {

      Administradordereglas(){
        ********** Menu.menu.pintarficha(); ****************
      }

     void juegodebots(int jugador){

         System.out.println("LLAMADA A JUEGO DE BOTS");
         int valoraañadiralasposiciones;
         valoraañadiralasposiciones= Ventanadeordenes.dado.Tiraeldado();  

         if(valoraañadiralasposiciones==5){

            System.out.println("Se ha sacado un 5, procedo a crear una nueva ficha");
         }

         Parchis.servidor.Pasarturno();
     }
}

But here it doesn't: 但这里没有:

package parchis;
public class Administradordereglas {

      Administradordereglas(){

      }
     void juegodebots(int jugador) {

         System.out.println("LLAMADA A JUEGO DE BOTS");
         int valoraañadiralasposiciones;
         valoraañadiralasposiciones= Ventanadeordenes.dado.Tiraeldado();  
         if(valoraañadiralasposiciones==5){
             **************This message appears in the console:****************** 
             System.out.println("Se ha sacado un 5, procedo a crear una nueva ficha");

            *****************Menu.menu.pintarficha();*************************
         }
         Parchis.servidor.Pasarturno();
    }
}

Thanks for your help. 谢谢你的帮助。

Can you add repaint() to your Refrescar method: 你可以在refrescar方法中添加repaint():

public void refrescar() {

    jpanel.add(jlabel);
    jframe.add(jpanel);
    jframe.pack();
    jframe.repaint();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM