Interface to tag drawable objects

Well let me start with some background before the actual question is asked. There must be some good examples for doin this type of question.

Drawables can be basic objects, (e.g. Line, Circle, Rectangle), or composite objects which are made up of a collection of other Drawables.

Below is the definition of the interface Drawable. The only requirement of a Drawable object is that it has a method to draw itself relative to some anchor point (ax,ay) on a Graphics context, g.

import java.awt.Graphics2D;

/*  An object that implements the Drawable interface can draw  
 *  itself on a Graphics2D context. If the object does not specify
 *  its own colours then the current Graphics colour is used.
 *  The object has a default anchor point of (0,0). The draw
 *  method gives the actual point (ax, ay) where the object should
 *  be anchored. The object is drawn relative to this point.
interface Drawable {

      /* Draw this object at (ax,ay) on a Graphics context, g. */
public void draw(Graphics2D g, int ax, int ay);

Heres the question

Write some simple classes that implement Drawable. In each case clearly document what the anchor point is. Also provide the usual toString() method for each object. Suggestions are:

* class Line, anchor point (0,0), other end at (dx,dy) – see below.
* class Circle with radius r. The anchor point (0,0) is the centre of the circle
* class Rectangle with width w, and height h. The anchor point is the top left hand corner.

class Line implements Drawable{
private int dx;      //x displacement from the anchor point
private int dy;      //y displacement from the anchor point

public Line(int x, int y) { dx=x; dy=y; }

public void draw(Graphics2D g, int ax, int ay) {
g.draw(new Line2D.Float(ax, ay, ax+dx, ay+dy));

public String toString() {
return "Line: x-displacement=" + dx + " y-displacement=" + dy;

Test the shapes by drawing them on a Graphics Component.

Create a class Picture that also implements the Drawable interface. A picture object is made up of a collection of Drawable picture components and the coordinates of where each component should be placed relative to the picture’s anchor point. One way of achieving this is to create a helper class called PictureComponent which has three attributes: a Drawable object, and coordinates (x,y) giving the position where the component should be anchored relative to the Picture's own anchor point.

Create a Picture object and add some Drawable objects to it. Test it out in your Graphics Component.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

We've been here before! The suggestion that was made earlier to you is that CIRCLE, LINE etc. should extend the 2D classes that are equivalent. All you need to do in the draw method of each is make the appropriate call to the Graphics2D class for the Shape interface. If the the 2D classes are extended, they will automatically implement Shape:

class LINE extends Line2DFloat implements Drawable {


      public void draw(Graphics2D g, int ax, int ay) {


>>The suggestion that was made earlier..

This was in

by Tim and starts thus:
      >>I would write a class for each object type (with the same classname)

This means with the same name as in the source text file, with its capitalized names, LINE, CIRCLE etc.
Mick BarryJava DeveloperCommented:
public class PictureComponent implements Drawable
   private Drawable Shape = null;
   private int X = 0;
   private int Y = 0;

   public PictureComponent(Drawable d, int x, int y)
      D = d;
      X = x;
      Y = y;

   public void draw(Graphics2D g, int ax, int ay)
      D.draw(g, X+ax, Y+ay);

public class Picture implements Drawable
   private ArrayList Drawables = new ArrayList();

   public void addDrawable(Drawable d, int x, int y)
      Drawables.add(new PictureComponent(d, x, y));

   public void draw(Graphics2D g, int ax, int ay)
      Iterator i = Drawables.iterator()
      while (i.hasNext())
          PictureComponent d = (Drawable);
          g.draw(g, ax, ay);


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

k1ngp1n99Author Commented:
yeah i think i got it objects thanks. CEHJ heres where the idea that you and time discussed to create a class for each shape comes in handy.

Mick BarryJava DeveloperCommented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.