converting a class to 2D?

I

Ian Stanley

Hi,
I have a program which adds shapes (rectangles, circles, etc) to a vector v.
this is done by:
v.add(new Rects(x, y, w, h)) ;
Is there a easier way to make the Rects class have all the charateristics of
a 2D Rectangle?(such as methods like contains etc) while keeping the
inheritance structure
effectively the same as v.add(new 2DRects(x, y, w, h)) ;
Any pointers appreciated
regards
Ian

the present way a rectangle is created is:
class shape extends JComponent {
int x, y ;
int w, h ;

shape(int x, int y, int w, int h) {
super() ;
this.x = x ;
this.y = y ;
this.w = w ;
this.h = h ;
}

class Rects extends shape {
Rects(int x, int y, int w, int h) {
super(x, y, w, h) ;
}

public void paint(Graphics g) {
g.drawRect(x, y, w, h) ;
}
}
 
S

Steve W. Jackson

:> Hi,
:> I have a program which adds shapes (rectangles, circles, etc) to a vector v.
:> this is done by:
:> v.add(new Rects(x, y, w, h)) ;
:> Is there a easier way to make the Rects class have all the charateristics of
:> a 2D Rectangle?(such as methods like contains etc) while keeping the
:> inheritance structure
:> effectively the same as v.add(new 2DRects(x, y, w, h)) ;
:> Any pointers appreciated
:> regards
:> Ian
:>
:> the present way a rectangle is created is:
:> class shape extends JComponent {
:> int x, y ;
:> int w, h ;
:>
:> shape(int x, int y, int w, int h) {
:> super() ;
:> this.x = x ;
:> this.y = y ;
:> this.w = w ;
:> this.h = h ;
:> }
:>
:> class Rects extends shape {
:> Rects(int x, int y, int w, int h) {
:> super(x, y, w, h) ;
:> }
:>
:> public void paint(Graphics g) {
:> g.drawRect(x, y, w, h) ;
:> }
:> }

I suspect you're unaware of the Shape interface already in existence in
the java.awt package. It's implemented by a series of predefined
classes, including RectangularShape (superclass of both Rectangle and
Rectangle2D).

And don't forget that you add Objects to a Vector. When you retrieve
them, you can always determine what actual Class each is an instance of
on the fly. By virtue of being instances of that particular class, they
automatically provide the relevant methods.

And then, you can decide whether your shapes really need to be
self-drawing components as you're apparently trying to do by having them
extend JComponent. If they do, then your Shape class above (following
the proper convention and always naming a class so that it begins with
an uppercase letter) should probably be renamed for clarity and then
implement that interface. That, of course, will be much more work than
simply using existing Shape implementors and having them draw into
something like a JPanel.

= Steve =
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top