I'm trying to make a game for school, like Puralax. This is my first year of Java.
Currently I'm testing through 'viewTest' and the 'DitMoetWerken.java' class to make my complete JFrame.
My guess is that the VakUI doesn't get painted but I don't know why. These should be the squares in my matrix.
This is where I call all my JFrames:
public class DitMoetWerken extends JFrame {
Spel spel;
public DitMoetWerken(Spel spel, int level) throws HeadlessException {
this.spel = spel;
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(800,500);
setTitle("Puralax");
setVisible(true);
spel.beginLevel(level);
SpelFrame spelFrame = new SpelFrame(spel,new JButton("terugknop"),new JButton("resetknop"));
this.add(spelFrame,BorderLayout.CENTER);
This is where I make my roster for my game. It is a matrix where the length is 3x3 in this level:
public class SpelbordUI extends JPanel {
private final Spel spel;
private SpelRooster spelRooster;
private boolean vakChecker = false;
private Vak bewaardVak;
public SpelbordUI(Spel spel) {
this.spel = spel;
this.spelRooster = spel.getSpelRooster();
initAll();
}
private void initAll(){
this.removeAll();
this.setLayout(new GridLayout(spelRooster.getLengte(),spelRooster.getLengte(),20,20));
for (int i = 0; i < spelRooster.getLengte(); i++) {
for (int j = 0; j < spelRooster.getLengte(); j++) {
VakUI vak = new VakUI(spelRooster.getRooster()[i][j]);
this.add(vak);
}
}
}}
A 'vak' is meant to be a square in my language. Sorry for any possible confusion.
The class of this VakUI looks like this, where I think my problem is with the paintComponent
. I think it should just fill up the vak's in VakUI because of the this.setBackground(kleur)
or should I draw them in new squares?
public class VakUI extends JPanel {
private Color kleur;
private Vak vak;
public VakUI(Vak vak) {
this.vak = vak;
this.kleur = vak.getKleur();
}
public Vak getVak() {
return vak;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(kleur);
}}