title: How to add a paintComponent() to a JPanel
body: <p>I have 2 classes, Class2 and Class4. I wish to add the paint in Class2 from Class4 onto a JPanel in Class4 using the p4.add(c2o) and f.add(p4) methods. I have no problems adding GUI but I simply couldn't add graphics.</p>
  
  <p><strong>Class4:</strong></p>
  
  <pre><code>import java.awt.BorderLayout;
  import javax.swing.JFrame;
  import javax.swing.JOptionPane;
  import javax.swing.JPanel;
  
  public class Class4 {
  
  
      public void mainMethod(int event){
          JFrame f = new JFrame("Love Test");
          if(event == 0){
  
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              f.setSize(500,200);
              f.setLayout(null);
  
              JPanel p = new JPanel(new BorderLayout());
              p.setBounds(150, 0, 350, 75);
              Class3 c3o = new Class3();
              p.add(c3o);
              f.add(p);
  
              JPanel p2 = new JPanel();
              Class7 c7o = new Class7();
              p2.add(c7o);
              p2.setBounds(0, 75, 500, 40);
              f.add(p2);
  
              JPanel p3 = new JPanel();
              p3.setBounds(0, 0, 150, 75);
              Class5 c5o = new Class5();
              p3.add(c5o);
              f.add(p3);
  
              f.setVisible(true);
  
          }
  
          if(event == 5){
  
              JPanel p4 = new JPanel();
              Class2 c2o = new Class2();
              p4.add(c2o);
              p4.setBounds(0,115,500,55);
              f.add(p4);
          }
  
      }
  
  
  }
  </code></pre>
  
  <p><strong>Class2:</strong></p>
  
  <pre><code>import javax.swing.JOptionPane;
  import javax.swing.JPanel;
  
  import java.util.Random;
  import java.awt.*;
  
  
  
  public class Class2 extends JPanel{
  
      public void paint(Graphics g){
          super.paint(g);
          this.setBackground(Color.WHITE);
          g.setColor(new Color(0,0,0));
          String str = "";
          String str2 = "";
          Random rlove = new Random();
          int love = rlove.nextInt(101);
          Class3 c3o = new Class3();
          str = c3o.boy.getText() + " and " + c3o.girl.getText() + "'s amount of love is " + love + "%.";
          if(love &gt;= 0 &amp;&amp; love &lt; 10){         
              str2 = "Stop thinking about that person anymore!";
          }
          if(love &gt;= 10 &amp;&amp; love &lt; 20){
              str2 = "1/100 percent chance...";
          }
          if(love &gt;= 20 &amp;&amp; love &lt; 30){
              str2 = "Little hope...";
          }
          if(love &gt;= 30 &amp;&amp; love &lt; 40){
              str2 = "Not even a kiss, stop talking about French kisses! (wise words of Rubesh)";
          }
          if(love &gt;= 40 &amp;&amp; love &lt; 50){
              str2 = "Near passing";
          }
          if(love &gt;= 50 &amp;&amp; love &lt; 60){
              str2 = "Just Pass";
          }
          if(love &gt;= 60 &amp;&amp; love &lt; 70){
              str2 = "Some effort needed.";
          }
          if(love &gt;= 70 &amp;&amp; love &lt; 80){
              str2 = "Grade A. A good chance.";
          }
          if(love &gt;= 80 &amp;&amp; love &lt; 90){
              str2 = "Good pair. Very good relationship!";
          }
          if(love &gt;= 90 &amp;&amp; love &lt; 100){
              str2 = "Perfect pair. Lost for words.";
          }
          if(love == 100){
              str2 = "OH MY GOD!!!!!";
          }
          g.drawString(str,10,10);
          g.drawString(str2,10,30);
      }
  
  
  }
  </code></pre>
  
  <p>How do I add the paintComponent to JPanel p4? Thanks!</p>