Reputation: 33
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. 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);
}
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:
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 (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.
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.
Upvotes: 3
Views: 309
Reputation: 159754
Can you add repaint() to your Refrescar method:
public void refrescar() {
jpanel.add(jlabel);
jframe.add(jpanel);
jframe.pack();
jframe.repaint();
}
Upvotes: 1