multiple paint methods

I am trying to implement polymorphism over certain classes, and wish them to draw themselves to a single canvas component in the main class. I have the main engine class (mover), which will do the animation and hold the canvas.
I will also have a superclass gamepiece which will have subclasses sprite, score, and background.
How do I use the various paint methods in order for each class to display itself on the main canvas?

 /* main class for the mover program */

import java.awt.*;
import java.awt.Graphics.*;
import java.awt.event.*;

public class mover extends Frame implements KeyListener
{
public static Canvas area;

 public mover()
 {
 setSize(500, 500);
 setVisible(true);
 setTitle("Rolly Game v1.0");
 addKeyListener(this);
 area = new Canvas();
 area.setBackground(Color.cyan);
 add(area);
 }

 public static void main(String[] args)
 {
 Frame game = new mover();
 area.repaint();

 game.addWindowListener(new WindowAdapter() {
  public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
 }

 public void keyPressed(KeyEvent e)
 {

  //if (e.getSource(getKeyCode())==VK_LEFT)
  {
  }

  //if (e.getKeyCode()==VK_RIGHT)
  {
  }
 }

 public void keyReleased(KeyEvent e)
 {
  //if (e.getSource()==VK_LEFT)
  {
  }

  //if (e.getSource()==VK_RIGHT)
  {
  }

 }

 public void keyTyped(KeyEvent e)
 {
 }

 public void update(Graphics g)
 {
 paint(g);
 }

 public void paint(Graphics g)
 {
 gamepiece.repaint();
 score.repaint();
 backdrop.repaint();
 }
}

import java.awt.*;

public class sprite extends gamepiece
{


 public sprite()
 {
 }

 public void paint(Graphics g)
 {
 g.setColor(Color.black);
 g.drawOval(75, 75, 75, 75);
 }
}

import java.awt.*;

public class score extends gamepiece
{
int score;
int s;

 public score(int s)
 {
 score = s;
 }

 public int return_score() {return score;}

 public void paint(Graphics g)
 {
 g.setColor(Color.black);
 g.drawString("SCORE", 250, 250);
 }
}

import java.awt.*;

public class backdrop extends gamepiece
{
 public backdrop()
 {
 }

public void draw_yourself() {repaint();}


 public void paint(Graphics g)
 {
 g.setColor(Color.black);
 g.drawLine(50, 100, 50, 200);
 }
}

import java.awt.*;

public class gamepiece
{
 public gamepiece()
 {
 }

 public void draw_yourself()
 {
 }

 public void paint(Graphics g)
 {
 }
}
saivonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

OviCommented:
First - I can see where you instantiate all this classes (gamepiece, score, backdrop), and where you add them on your canvas. I suggest you to use the mover class as a "manager" which can instantiate the canvas(a derived class) and treat all messages between the other classes and IMPLEMENT A DERIVED CLASS FROM JAVA.AWT.CANVAS, ON WICH YOU'LL ADD YOUR GRAPHIC OBJECTS.

Something like : mover.add(new xCanvas());, where xCanvas add on himself all your graphic classes.
               
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.