title: Painting method paints things from other components
body: <p>I am trying to make a simple Java program with GUI using Java Swing.</p>
  <p>I have painting panel (gPanel) in the center of the screen, panel with buttons (buttonSet) in the west and panel with labels (labelPanel) in the east. To paint over gPanel I use paintComponent method and since I have two buttons, which are supposed to draw different things (and change label on the right of the screen), I decided to put switch case in paintComponent method for it to choose the correct actual painting method.</p>
  <p>When I run the program everything looks fine - program uses the first method to paint and there is a sampletext.png image shown in the middle of the screen with yellow background, as it should be. Button number 1 also uses this method to draw over gPanel, so pressing it draws the same thing.</p>
  <p><strong>Now Button number 2 uses the second painting method and this is where things go wrong.</strong> It draws sampleimage.png over the gPanel, but also parts of left and right panels (i.e. buttons from left buttonSet panel and orange colour that is background colour of side panels) are drawn, though it shouldn't happen. Also the whole gPanel becomes gray (I think it happens because of label on the right that becomes very long after pressing Button number 2, because when the label was shorter gPanel didn't turn gray and left the previously drawn things instead).</p>
  <p>Pressing Button number 1 paints things from first method properly, so pressing it after pressing Button number 2 &quot;reverts&quot; the changes.</p>
  <p>What do I have to do to make my second painting method work properly?
  Also why adding border to buttonSet and labelPanel works but adding it to gPanel doesn't?</p>
  <pre><code>    package com.inferjus.drawingproject;
      
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.awt.image.*;
      import java.io.*;
      import javax.imageio.*;
      import javax.swing.border.*;
      
      /**
       *
       * @author inferjus
       */
      
      public class DrawingProject
      {
          private JFrame frame;
          private graphicPanel gPanel;
          private JPanel buttonSet;
          private JPanel labelPanel;
          private JLabel label;
          private int painter=0;
          
          public static void main(String[] args)
          {
              DrawingProject program=new DrawingProject();
              program.prepareGUI();
          }
          
          public int getPainter()
          {
              return painter;
          }
          
          public void setPainter(int x)
          {
              painter=x;
          }
          
          public void prepareGUI()
          {
              //setting JFrame and icon
              frame=new JFrame(&quot;Drawing Project&quot;);
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                  try { frame.setIconImage(ImageIO.read(getClass().getResource(&quot;/resources/sampleicon.png&quot;)));} 
                  catch (IOException e) { e.printStackTrace(); }
      
              //border for components
              Border bigBlackBorder=new LineBorder(Color.black, 3);
                  
              //setting JPanel (graphicPanel) for drawing images
              gPanel=new graphicPanel();
                  gPanel.setBorder(bigBlackBorder); // &lt;--- why it does not work?
                  
      
              //setting JPanel for buttons on the left of the screen
              buttonSet=new JPanel();
                  buttonSet.setLayout(new BoxLayout(buttonSet, BoxLayout.Y_AXIS));
                  buttonSet.setBorder(bigBlackBorder);
      
              //setting JButtons
              JButton buttonOne=new JButton(&quot;Button number 1&quot;);
                  buttonOne.addActionListener(new buttonOneListener());
                  buttonSet.add(buttonOne);
                  buttonSet.setBackground(Color.orange);
              JButton buttonTwo=new JButton(&quot;Button number 2&quot;);
                  buttonTwo.addActionListener(new buttonTwoListener());
                  buttonSet.add(buttonTwo);
      
              //setting JLabels on the right of the screen
              label=new JLabel(&quot;Default label&quot;);
                  label.setFont(new Font(&quot;Consolas&quot;, Font.PLAIN, 20));
              labelPanel=new JPanel();
                  labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.Y_AXIS));
                  labelPanel.setBackground(Color.orange);
                  labelPanel.setBorder(bigBlackBorder);
              JLabel popeLabelTitle=new JLabel(&quot;What does the label say?&quot;);
                  popeLabelTitle.setFont(new Font(&quot;Consolas&quot;, Font.BOLD, 24));
              
              //adding JLabels to labelPanel
              labelPanel.add(BorderLayout.NORTH, popeLabelTitle);
              labelPanel.add(BorderLayout.CENTER, label);
              
              //adding components to JFrame
              frame.getContentPane().add(BorderLayout.CENTER, gPanel);
              frame.getContentPane().add(BorderLayout.EAST, labelPanel);
              frame.getContentPane().add(BorderLayout.WEST, buttonSet);
              frame.setVisible(true);
          }
          
          class graphicPanel extends JPanel
          {
              private BufferedImage sampletext=null;
              private BufferedImage sampleimage=null;
              
              @Override
              public void paintComponent(Graphics g)
              {
                  //for Button One paint sampletext.png, for Button Two paint sampleimage.png
                  switch (painter)
                  {
                  case 0:
                      paintSampletext(g);
                      break;
                      
                  case 1:
                      paintSampleimage(g);
                      break;
                  }
              }
              
              //paint yellow background and put sampletext.png in the middle
              private void paintSampletext(Graphics g)
              {
                  if (sampletext==null)
                  {
                      gPanel.setSampletextPNG();
                  }
                  g.setColor(Color.yellow);
                  g.fillRect(0,0, gPanel.getWidth(), gPanel.getHeight());
                  g.drawImage(sampletext, gPanel.getWidth()/2-sampletext.getWidth()/2, gPanel.getHeight()/2-sampletext.getHeight()/2, this);          
                  g.setColor(Color.black);
                  g.drawRect(gPanel.getWidth()/2-sampletext.getWidth()/2, gPanel.getHeight()/2-sampletext.getHeight()/2, sampletext.getWidth(), sampletext.getHeight());
                  g.dispose();
              }
              
              //paint sampleimage.png over what is already displayed
              private void paintSampleimage(Graphics g)
              {
                  if (sampleimage==null)
                  {
                      gPanel.setSampleimagePNG();
                  }
                  int x=(int)((Math.random()*gPanel.getWidth())-sampleimage.getWidth());
                  int y=(int)((Math.random()*gPanel.getHeight())-sampleimage.getHeight());
                  g.drawImage(sampleimage, x, y, gPanel);
                  g.dispose();
              }
              
              public void setSampletextPNG()
              {
                  try { sampletext=ImageIO.read(getClass().getResource(&quot;/resources/sampletext.png&quot;)); }
                  catch (IOException ex) { System.out.println(&quot;Image error&quot;); }            
              }
              
              public void setSampleimagePNG()
              {
                  try { sampleimage=ImageIO.read(getClass().getResource(&quot;/resources/sampleimage.png&quot;)); }
                  catch (IOException ex) { System.out.println(&quot;Image error&quot;); }
              }
          }
          
          class buttonOneListener implements ActionListener
          {
              @Override
              public void actionPerformed(ActionEvent e)
              {
                  label.setText(&quot;Reaction to button number 1: change of label.&quot;);
                  setPainter(0);
                  gPanel.repaint();
              }
          }
          
          class buttonTwoListener implements ActionListener 
          {
              @Override
              public void actionPerformed(ActionEvent e)
              {
                  label.setText(&quot;Reaction to button number 2: change of label + drawing images over gPanel.&quot;);
                  setPainter(1);
                  gPanel.repaint();
              }
          }
      }
  </code></pre>
  <p>Tree of my project:</p>
  <ul>
  <li><p>DrawingProject</p>
  <p>-JRE System Library</p>
  <p>-src</p>
  <p>--com.inferjus.drawingproject</p>
  <p>---DrawingProject.java</p>
  <p>--resources</p>
  <p>---sampleicon.png</p>
  <p>---sampleimage.png</p>
  <p>---sampletext.png</p>
  </li>
  </ul>
  <p><a href="https://i.stack.imgur.com/9PadU.png" rel="nofollow noreferrer">what shows after running the program by default or after pressing Button One</a></p>
  <p><a href="https://i.stack.imgur.com/uF4c9.png" rel="nofollow noreferrer">what shows after pressing Button Two one time</a></p>
  <p><a href="https://i.stack.imgur.com/MBNPC.png" rel="nofollow noreferrer">what shows after pressing Button Two a few times</a></p>