PaintComponent没有被调用 [英] PaintComponent is not being called

查看:75
本文介绍了PaintComponent没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我才刚刚开始学习编程,所以,如果我的问题很愚蠢,请原谅.我已经尝试了超过两天的时间来找到该问题的解决方案,但我只是无法在网上找到它,因此我需要您的帮助.预先感谢.

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.

因此,我正在尝试用Java重新创建Parchisi游戏.我想创建一种方法,每次玩家掷骰子时将计数器放在特定位置,结果得到数字5. 计数器有自己的类,即:

So, I am trying to recreate the Parchisi game in Java. 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. 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);
} 

通过以下方法的调用将检查器置于jframe之上:

The checker is put over a jframe by a call of this method:

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

问题是,当从方法外部(在我的一个类的实例中,即IE)调用方法pintarficha()时,它可以正常工作并绘制计数器,但是当我将其放入任何方法内部时,PaintComponent没有执行,我不明白为什么.

The problem is that, when the method pintarficha() is called from outside a method (I.E. 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.

在这里有效:

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

但是这里没有:

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

感谢您的帮助.

推荐答案

可以将repaint()添加到Refrescar方法中吗?

Can you add repaint() to your Refrescar method:

public void refrescar() {

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

这篇关于PaintComponent没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆