title: Access Graphics Rendering Method from Another Class in Java
body: <p>I have created a class called <code>SimpleCanvas</code> which <code>extends</code> the <code>JPanel</code> class, and I would like to render some graphical objects onto a <code>SimpleCanvas</code> object. I have also created another class called <code>Rectangle</code>, and I would like to render a simple <code>Rectangle</code> object onto a <code>SimpleCanvas</code> object named <code>mainCanvas</code>. However, my issue is that I can not figure out how to access the <code>render</code> method of my <code>Rectangle</code> class, and allow it to be called from within my <code>SimpleCanvas</code> class when a <code>redraw</code> event is called for the <code>mainCanvas</code> object.</p>
  
  <p>Here is my code:</p>
  
  <p><strong>SimpleCanvas Class:</strong></p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  import java.util.Vector;
  
  public class SimpleCanvas extends JPanel {
      private int x, y, width, height;
      private Vector objects = new Vector();
  
      public SimpleCanvas(int x, int y, int width, int height) {
          setPosition(x, y);
          setWidth(width);
          setHeight(height);
      }
  
      public void setPosition(int x, int y) {
          this.x = x;
          this.y = y;
      }
  
      public void setWidth(int width) {
          this.width = width;
      }
  
      public void setHeight(int height) {
          this.height = height;
      }
  
      public void addObject(Object object) {
          this.objects.add(object);
      }
  
      public void paintComponent(Graphics g) {
          for (int i = 0; i &lt; this.objects.size(); i++) {
              this.objects.get(i).render(g);
          }
      }
  }
  </code></pre>
  
  <p><strong>SimpleWindow Class:</strong></p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  
  public class SimpleWindow extends JFrame {
      private int x = 0;
      private int y = 0;
      private int width = 0;
      private int height = 0;
      private Color color = Color.WHITE;
      private String name = "DEFAULT_NAME";
  
      public SimpleWindow(int x, int y, int width, int height, Color color, String name) {
          this.name = name;
          setSize(width, height);
          setLocation(x, y);
          setBackground(color);
          setVisible(true);
      }
  }
  </code></pre>
  
  <p><strong>Rectangle Class:</strong></p>
  
  <pre><code>import java.awt.*;
  
  public class Rectangle {
      private int x = 0;
      private int y = 0;
      private int width = 0;
      private int height = 0;
      private Color color = Color.WHITE;
      public Rectangle(int x, int y, int width, int height, Color color) {
          this.x = x;
          this.y = y;
          this.width = width;
          this.height = height;
          this.color = color;
      }
      public void render(Graphics g) {
          g.setColor(this.color);
          g.fillRect(this.x, this.y, this.width, this.height);
      }
  }
  </code></pre>
  
  <p><strong>Main Class:</strong> </p>
  
  <pre><code>import java.awt.*;
  
  public class Main {
      public static void main(String[] args) {
          SimpleWindow mainWindow = new SimpleWindow(0, 0, 640, 360, Color.WHITE, "Simple Windowing System");
          SimpleCanvas mainCanvas = new SimpleCanvas(0, 0, 640, 360);
          Rectangle mainRectangle = new Rectangle(0, 0, 50, 50, Color.BLUE);
          mainCanvas.addObject(mainRectangle);
          mainWindow.setContentPane(mainCanvas);
      }
  }
  </code></pre>
  
  <p>I feel as though I also might be approaching the problem of rendering graphics to the <code>mainCanvas</code> object the wrong way.</p>
  
  <p>Thank you in advance for any help!</p>