0

I have a chat client that launches another window when '.play' is sent in. The relevant code follows:

BoardFrame bframe = new BoardFrame();
bframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bframe.setSize(300,600);
frame.setVisible(false);
bframe.setVisible(true);
BoardPanel bpanel = new BoardPanel();
bpanel.setSize(200,500);
bpanel.setOrientation('v');
bpanel.setBackground(Color.GREEN);
bpanel.paintComponent(bpanel.getGraphics());
bpanel.setVisible(true);
bframe.getContentPane().add(bpanel,"Center");
bframe.pack();

BoardPanel is a class that extends JPanel and BoardFrame is a class that extends JFrame. Frame will be deleted, but for now i just set it to not visible. I just dont want to go messing around in those other classes if the solution is a quick one here.

Also this is my first post on here so please forgive the hideous formatting.

Ben Kramer
  • 251
  • 1
  • 2
  • 7
  • 3
    `bpanel.paintComponent(bpanel.getGraphics());` <- This is so bad on so many levels I don't even know where to begin – MadProgrammer Apr 04 '17 at 00:52
  • 3
    1. Never, ever call `paintComponent` directly, it's not your responsibility and it's not how painting works in Swing – MadProgrammer Apr 04 '17 at 00:53
  • 2
    2. Never use `getGraphics`, it can return `null` (as you've discovered) and it represents only a snap shot of the last painting cycle, anything you paint to it will be wiped clean when a new paint cycle runs - this is not how painting works in Swing – MadProgrammer Apr 04 '17 at 00:54
  • 2
    Start by going over [Painting in Swing and AWT](http://www.oracle.com/technetwork/java/painting-140037.html) and [Performing custom painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/) for more details about how painting works and how you might use it – MadProgrammer Apr 04 '17 at 00:55
  • What line is the `NullPointerException` on? Just from looking at your code, I would guess it happens because `panel.getGraphics()` is equal to `null`. Think about it; if you haven't made a window visible, how can it have a graphics context? NOTE: I'm not telling you to simply call `paintComponent` after `setVisible`. In this case, you don't even need to call it; when you need to refresh, use `repaint`. – ostrichofevil Apr 04 '17 at 00:55
  • 2
    I would say thanks @MadProgrammer that was helpful also for me I will read this tutorials – Mohammed Housseyn Taleb Apr 04 '17 at 00:57
  • 2
    3- There is a reason why `paintComponent` is `protected` by default, NEVER make it `public`, there is NO reason why anybody should be calling it directly! ... I might burst a blood vessel :P – MadProgrammer Apr 04 '17 at 00:57
  • Thank you madprogrammer! I'll read through those tutorials and really appreciate the extra effort in getting them. I should specify I know a little programming but didnt actually plan on expanding my programming skills, but my friend wanted something coded and doesn't know anybody else who could do it for him so I figure if i can get it to work, even if the solutions are ugly, thats okay – Ben Kramer Apr 04 '17 at 01:06
  • @MadProgrammer *"`paintComponent` is protected by default, NEVER make it `public`"* Damn it, I'm ***still*** forgetting that & making my examples `public`. If you ever notice me doing it, please gift me a down vote & comment to help me remember. :P – Andrew Thompson Apr 04 '17 at 01:16
  • 1
    @AndrewThompson I've been caught out with that one when copying and pasting other peoples code :P – MadProgrammer Apr 04 '17 at 01:18
  • 1
    @BenKramer I'm just a grump old programmer who get's passionate about some silly things ;) - Glad it could help – MadProgrammer Apr 04 '17 at 01:19
  • @MadProgrammer *"when copying and pasting other peoples code"* LOL! I wish I could blame it on other people's code, but bottom line, I even forget it in my own examples. Bad habit. Bad, *bad* habit. – Andrew Thompson Apr 04 '17 at 01:38

0 Answers0