-1

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

}}
user253751
  • 57,427
  • 7
  • 48
  • 90
  • 3
    Please post your relevant code here with your question, not in a link. Note that your first job before coming here is to debug to try to isolate the problem as this will help you limit the code that you must post, and will make it easier for us to understand your problem and help you. Best if you could create and post a [minimal example program or MCVE](http://stackoverflow.com/help/mcve). – Hovercraft Full Of Eels Mar 11 '15 at 01:22
  • 3
    Can't see your code project...I'd almost go as far to say that's it just spam for the file sharing service... – MadProgrammer Mar 11 '15 at 01:24
  • 2
    Chit and your link requires cookies and does not look to be a trustworthy site. Bad bad boy! – Hovercraft Full Of Eels Mar 11 '15 at 01:24
  • *"the code i think is necessary."* Uncompilable code snippets are not an MCVE. An MCVE needs to be one source file (so only one `public` class), with relevant imports and a `main(String[])` to put it on-screen. Unless a minimal example can be compiled, run, and show the problem *without any changes or additions*, it is not an MCVE. – Andrew Thompson Mar 11 '15 at 02:32
  • i dont know how else i can post this. my project uses more then 10 classes. i cant post just 1 or it wont work. the file i have my `main(String[])` in. just makes an new object of the DitMoetWerken class. like so `DitMoetWerken oframe = new DitMoetWerken(new Spel(), 1);` – Tom Dirinck Mar 11 '15 at 02:34
  • `protected void paintComponent(Graphics g) { super.paintComponent(g); this.setBackground(kleur);` Remove `this.setBackground(kleur);`! That would cause the UI to repaint and that method to be called in an infinite loop.. – Andrew Thompson Mar 11 '15 at 02:34
  • *"i cant post just 1 or it wont work"* If you cannot prepare an MCVE, by either trimming the project down, or by starting fresh and building one up, then I doubt we can help. It is quite common that the real problem is not in the places (or code) that the programmer suspects it is. – Andrew Thompson Mar 11 '15 at 02:37
  • again im sorry if this is a big mess for you to help me. im very very new. if i remove that, how should i paint my squares then? maybe a for each? for every 'vak' that got added? – Tom Dirinck Mar 11 '15 at 02:38
  • *"maybe a for each?"* That would probably be a good approach. Loops can often be used to good effect when displaying a layout or windowing problem. In [this answer](http://stackoverflow.com/a/7143398/418556) I use a loop to create 3 windows, whereas in [this example](http://stackoverflow.com/a/28912133/418556) a loop is used to add 3 panels or 'cards' to a card layout. And a tip: Add @HovercraftFullOfEels (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Mar 11 '15 at 03:19
  • @AndrewThompson My problem got solved. I misplaced `setVisible(true);` How can I close this question? – Tom Dirinck Mar 11 '15 at 08:48

1 Answers1

0

In the class DitMoetWerken. setVisible(); is supposed to be placed underneath this.add(spelFrame,BorderLayout.CENTER);. this solved my empty window problem.