By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,805 Members | 1,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,805 IT Pros & Developers. It's quick & easy.

New to OOP and polymorphism

P: n/a
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!
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Stephen" <se*******@hotmail.com> wrote in message
news:NE*****************@newsread1.news.atl.earthl ink.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!


public void paint( Graphics g )
{
Shape2D aShape = new Circle( x, y, radius, fillColor );
aShape.draw();
}

Like that?
Jul 17 '05 #2

P: n/a
> public void paint( Graphics g )
{
Shape2D aShape = new Circle( x, y, radius, fillColor );
aShape.draw();
}

Like that?

Sorry, my mistake. What I meant was, what do I put in the Shape2D and in the
draw method class to make it draw on the JFrame client program using the
circle, square, and triangle classes - and any others I want to add later,
hence the whole idea object-oriented programming. Doing { public Graphics
draw() {} } does not work. Then in the client doing g.paint(
aShape.draw() ). ????? Anyone understand what I mean?
Jul 17 '05 #3

P: n/a
"Stephen" <se*******@hotmail.com> wrote in message news:<NE*****************@newsread1.news.atl.earth link.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:
Expand|Select|Wrap|Line Numbers
  1. public class DrawShapePanel extends Jpanel{
  2. Shape shape;
  3.  
  4. public DrawShapePanel(Shape s){
  5. shape = s;
  6. ...
  7. }
  8. ....
  9. ....
  10. paintComponent(Graphics g){
  11. super.paintComponent(g);
  12. drawShape(shape, g);
  13. ...
  14. }
  15.  
  16. drawShape(Shape shp, Graphics g){
  17. shp.draw(g);
  18. }
  19. }
  20.  
Jul 17 '05 #4

P: n/a
"Stephen" <se*******@hotmail.com> wrote in message
news:oY*****************@newsread3.news.atl.earthl ink.net...
public void paint( Graphics g )
{
Shape2D aShape = new Circle( x, y, radius, fillColor );
aShape.draw();
}

Like that?

Sorry, my mistake. What I meant was, what do I put in the Shape2D and in

the draw method class to make it draw on the JFrame client program using the
circle, square, and triangle classes - and any others I want to add later,
hence the whole idea object-oriented programming. Doing { public Graphics
draw() {} } does not work. Then in the client doing g.paint(
aShape.draw() ). ????? Anyone understand what I mean?


Check out the code I posted here:
http://www.google.com/groups?&selm=B...xas.net&rnum=3

It's a basic example of what you're trying to do. The Circle class is like
your Shape2D class. It's a shape that knows how to draw itself on the
screen. It's sparse on the comments, but just ask if you have questions. If
you'd like to email me about it, remove NO and SPAM from my email address.
Jul 17 '05 #5

P: n/a
That code is perfect. Exactly what I was looking for. Thanks.
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.