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! 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?
> 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?
"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: -
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);
-
}
-
}
-
"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.
That code is perfect. Exactly what I was looking for. Thanks. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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
...
|
by: richard pickworth |
last post by:
Can anyone explain polymorphism?(very simply).
thanks
richard
|
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...
|
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...
|
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...
|
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...
|
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++,...
|
by: Bart Friederichs |
last post by:
Hello,
I created the following inheritance:
class Parent {
public:
void foo(int i);
};
class Child : public Parent {
|
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
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |