1

I have a custom JPanel class which I add to my Frame and constantly update. This class has an overidden paintComponent and a JButton. How do I draw the JButton over my graphics drawn in my paintComponent? I constantly update this paintCompoment through a loop which repaint()'s it, But I do not change the JButton. My custom JPanel class is a subclass of my Main class.

My Panel Class

public class interTower extends JPanel implements ActionListener{
    public interTower(){
        setVisible(true);
        setPreferredSize(new Dimension(SCREENWIDTH, SCREENHEIGHT));

        JButton interTower_back = new JButton("Back");
        interTower_back.setActionCommand("interTower_back");
        interTower_back.addActionListener(this);
        add(interTower_back);
    }

    @Override

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        drawMaster(g2d);
        g2d.dispose();

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }
}
Colourfit
  • 357
  • 2
  • 16
  • 2
    1) Move the `super.paintComponent(g);` to be the first line in the method.. 2) `g.dispose();` don't dispose of a `Graphics` object you did not create. 3) The `g2d` is the same graphics object. Don't dispose of it either. 3) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 4) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Jul 17 '16 at 15:15
  • 1
    *"Was already like that."* show an MCVE of code that is ***actually failing*** rather than posting 'something like' the code being used. – Andrew Thompson Jul 17 '16 at 15:19
  • @AndrewThompson This piece of code is failing – Colourfit Jul 17 '16 at 15:21
  • *"This piece of code is failing"* Well, duh.. But that piece of code is an uncompilable code snippet, not an MCVE. – Andrew Thompson Jul 17 '16 at 15:26
  • @AndrewThompson Moved the super call and got rid of the dispose call for the unused graphics object – Colourfit Jul 17 '16 at 15:26

1 Answers1

1

By following AndrewThompsons wise words, I have fixed my issue! Disposing the graphics object seems to stop the rendering of the button. Removing the dispose call off of both of the object seems to of fixed my issue. Thanks AndrewThompson.

Colourfit
  • 357
  • 2
  • 16