I'm building a simple chess game and am stuck on trying to paint drawings on a Panel
I have a board ready and on the board there are panels. those panels are 70x70px and next thing i want to do is use THAT ENTIRE SURFACE to draw chess pieces.
i made an abstract class Pieces that extends JPanel.
public abstract class Piece extends JPanel
One of the pieces is ofcourse, a Pawn :
public class Pawn extends Piece
in the Pawn class I have paint component :
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int w = getWidth();
int h = getHeight();
g.setColor(Color.GREEN);
g.fillOval(0, 0, w, h);
System.out.println("height:"+h+" width:"+w);
}
This does not seem to work. the output prints height = 10px and width = 10px; .... but it is suposed to be 70px and 70px. I also see the green oval painted inside a 10x10 square, which is inside my Panel...
I tried a setSize(70,70) which does not realy do the trick... I also tried SetPreferredSize but well It did not really work out either. I really want to keep my abstract class and subclasses...
@@@ EDIT @@@
I've used setPreferredSize again and I can now actually draw my green circle on the Jpanel. but an error remains as in it is not the whole surface of the Jpanel Used... there remains a gap at the top , a screenshot: