Skip to Content
Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition
book

Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition

by Ivor Horton
December 2004
Beginner
1512 pages
43h 39m
English
Wrox
Content preview from Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition

19.6. Managing Shapes

When you create shapes in Sketcher, you'll have no idea of the sequence of shape types that will occur. This is determined totally by the person using the program to produce a sketch. You'll therefore need to be able to draw shapes and perform other operations on them without knowing what they are—and of course polymorphism can help here.

You don't want to use the shape classes defined in java.awt.geom directly as you'll want to add your own attributes such as color or line style for the shapes that can be drawn in Sketcher and store them as part of the object. You could consider using the shape classes as base classes for your shapes, but you couldn't use the GeneralPath class in this scheme of things because, as I said earlier, the class has been defined as final and therefore cannot be subclassed. You could consider defining an interface that all your shape classes would implement. However, some methods have a common implementation in all your shape classes, which would mean that you would need to repeat this code in every class.

Taking all of this into account, the easiest approach might be to define a common base class for the Sketcher shape classes and include a member in each class to store a shape object of one kind or another. You'll then be able to include a polymorphic method to return a reference to a shape as type Shape for use with the draw() method of a Graphics2D object.

You can start by defining a base class, Element, from which you'll derive ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Ivor Horton's Beginning Java®, Java 7 Edition

Ivor Horton's Beginning Java®, Java 7 Edition

Ivor Horton
PURE Java™ 2

PURE Java™ 2

Kenneth Litwak
Just Java™ 2

Just Java™ 2

Peter van der Linden

Publisher Resources

ISBN: 9780764568749Purchase book