473,412 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

New to OOP and polymorphism

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
5 2375
"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
> 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
"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
"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
That code is perfect. Exactly what I was looking for. Thanks.
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
3
by: E. Robert Tisdale | last post by:
polymorph just means "many form(s)". The definition in plain English http://www.bartleby.com/61/66/P0426600.html and narrower definitions in the context of computer programming ...
11
by: richard pickworth | last post by:
Can anyone explain polymorphism?(very simply). thanks richard
4
by: LP | last post by:
Hi, I understand the concept/definition of polymorphism. But what does the term "runtime polymorphism" mean? I was asked to define it during a technical interview. I gave a guy vanilla definition...
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and...
18
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
2
by: sarathy | last post by:
Hi all, I need a small clarification reg. Templates and Polymorphism. I believe templates is really a good feature, which can be used to implement generic functions and classes. But i doubt...
11
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
17
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.