ulthanex
ulthanex

Reputation: 47

Panels/Buttons and repainting headache

As a coursework piece for school, we had to come up with a self-defined project, which I chose a game idea I was originally going to write on actionscript but decided to push for it on Java. This was initially going well, with the game code working, the sprite animations were being handled fine and everything was being maintained within a game loop. That was until i began to try and add components such as buttons and labels to it.

Currently the structure of my game is as this( GameSystem[handles loop and redrawing] -- GameMenu [Simple intro screen with start game ] <---swaps between ---> GameContainer [holds information about the game] )

My loop inside the GameSystem updates all necessary objects, repaints and then tells all the objects to repaint after.

Game System:

/**/for(int i = 0; i < UpdateList.size();i++){
/**/UpdateList.get(i).Update(delta * 1.1); // tell list the element is an object that extends an interface      
/**/}
/**/Game.repaint();

/**/@Override
/**/public void paint(Graphics g) {
/**/super.paint(g);
/**/Graphics2D g2d = (Graphics2D) g;
/**/g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
/**/        RenderingHints.VALUE_ANTIALIAS_ON);
/**/for(int i = 0; i < UpdateList.size();i++){
/**/UpdateList.get(i).paint(g2d); // tell list the element is an object that extends an interface       
/**/}
/**/}

This is where it starts getting iffy, because the game system repaints it's self each loop, clearing the frame of previous stuff, the buttons which I add in my gamemenu which is created inside the game system get cleared and only reappear briefly when you mouse over them.

Game Menu:

    //////////////////
    // Start Button //
    //////////////////
    Buttons = new JPanel();     
    Buttons.add(Start_game = new JButton("Start Game"));
    this.add(Buttons);
    Start_game.addActionListener(new Load());
    /**/}
    ///////////////////

    //////////////////
    //Paint sprite //
    //////////////////
    @Override
    /**/public void paintComponent(Graphics g) {
    /**///super.paintComponent(g);
    /**/Buttons.paintComponents(g);
    /**/}
    ////////////////////////////////////

At this point I've tried paint, paint component, revalidate() and a variety of other stuff I've gleamed from other posts but none have helped. The only way I've had the button appear visible is if I remove the call to repaint in the game loop. However this then impacts the game as none of the objects are repainted.

TL DR: How can I Handle repainting the various objects in my update list and refreshing the Gamesystem panel without making any components such buttons disappear? Sorry if this is a noobish question but this is my actual first time working with Java as I wanted to expand outwards from actionscript, if any more info is required i will gladly add to the post, thank you for your help.

Upvotes: 0

Views: 106

Answers (1)

DoubleDouble
DoubleDouble

Reputation: 1493

The problem is that you are trying to handle the painting of the buttons differently than the painting of your game.

Game System
{
    paint loop
    {
        painting Game
    }
}

Components //managing the painting itself through paintComponent, trying to run at same time as Game System.
{
    paintComponent
}

So in effect, you are wanting to switch one to the other. You could either use paintComponent() in the GameSystem instead of paint(), which manages disposing of the graphics and repainting for you (if it is already extending a jComponent)...

or you need to start managing the buttons' painting similar to how you do it for your game, instead of relying on paintComponent()

Upvotes: 1

Related Questions