k1ngp1n99
asked on
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.
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
8-)
This was in
https://www.experts-exchange.com/questions/20805941/Displaying-a-drawing-defined-in-data-file.html
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.