[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

multiple paint methods

Posted on 2000-01-24
1
Medium Priority
?
273 Views
Last Modified: 2010-08-05
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)
 {
 }
}
0
Comment
Question by:saivon
1 Comment
 
LVL 9

Accepted Solution

by:
Ovi earned 300 total points
ID: 2384232
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question