Re: New to OOP and polymorphism

From: hiwa (HGA03630_at_nifty.ne.jp)
Date: 02/24/04

  • Next message: Tom N: "Re: Mouse Events not being registered"
    Date: 23 Feb 2004 18:30:09 -0800
    
    

    "Stephen" <sedelblut@hotmail.com> wrote in message news:<NEr_b.16893$W74.660@newsread1.news.atl.earthlink.net>...
    > Hi, I am working on a drawing program for class. It consists of a superclass
    > Shape2D where I have subclasses Circle, Square, and Triangle inheriting from
    > Shape2D. I also have a main program that is a JFrame where the shapes are
    > drawn. I am trying to use polymorphism and the Shape2D class stores the x
    > and y coordinates, and has an abstract method draw(). Here's the problem, I
    > don't know how to use the paint( Graphics g ) method of JFrame to draw the
    > shape using the draw method of each shape. Ideally I would like the shapes
    > to be drawn like this:
    >
    > public void paint( Graphics g )
    > {
    > Shape2D = new Circle( x, y, radius, fillColor );
    > Shape2D.draw();
    > }
    >
    > Something like that (I know that is not a real way to do it, it's just the
    > best way I can convey my idea ). How do I do this?? Thanks!

    I think you will have to write a JPanel extended class or JLabel
    extended class. And you'll add its object to JFrame's ContentPane.

    For example:
    [code]
    public class DrawShapePanel extends Jpanel{
      Shape shape;

      public DrawShapePanel(Shape s){
        shape = s;
        ...
      }
    ...
    ...
      paintComponent(Graphics g){
        super.paintComponent(g);
        drawShape(shape, g);
        ...
      }

      drawShape(Shape shp, Graphics g){
        shp.draw(g);
      }
    }
    [/code]


  • Next message: Tom N: "Re: Mouse Events not being registered"

    Relevant Pages

    • Re: New to OOP and polymorphism
      ... I am working on a drawing program for class. ... > Shape2D where I have subclasses Circle, Square, and Triangle inheriting ... I also have a main program that is a JFrame where the shapes are ... > don't know how to use the paint(Graphics g) method of JFrame to draw the ...
      (comp.lang.java)
    • New to OOP and polymorphism
      ... I am working on a drawing program for class. ... Shape2D where I have subclasses Circle, Square, and Triangle inheriting from ... I also have a main program that is a JFrame where the shapes are ... and has an abstract method draw(). ...
      (comp.lang.java)