| The Essential JViews Framework > Graphic Objects > Creating a New Graphic Object Class > Basic Steps |
Basic Steps |
INDEX
PREVIOUS
NEXT
|
The procedure to create a derived class of the IlvGraphic class can be broken down into seven main steps:
In this example we are going to create the class ShadowEllipse. This file is located at:
<installdir>/jviews-framework81/codefragments/shadow-ellipse/src/ShadowEllipse.java
To create the class:
ShadowEllipse.java that defines the new class and the necessary overloaded methods. Not every method needs to be overloaded.
import ilog.views.*; import ilog.views.io.*; import ilog.views.graphic.*; import java.awt.*; import java.io.*; |
IlvGraphic class.
drawrect of type IlvRect.
A constructor is defined to create a new shadow ellipse. These constructors simply set the value of the definition rectangle or create a new ShadowEllipse from an existing ShadowEllipse instance:
This method demonstrates that drawing an object is merely a call to some of the primitive methods contained in the AWT Graphics class.
/**
* Draws the object.
* We override the draw method to define the way the object will appear.
* @param dst The AWT object that will perform the
* drawing operations.
* @param t This parameter is the transformer used to draw the object.
* This parameter may be a translation, a zoom or a rotation.
* When the graphic object is drawn in a view (IlvManagerView),
* this transformer is the transformer of the view.
*/
public void draw(Graphics dst, IlvTransformer t)
{
// We first copy the rectangle that defines the bounding
// rectangle of the object because we do not want to modify it.
IlvRect r = new IlvRect(drawrect);
// To compute the bounding rectangle of the object in
// the view coordinate system, we apply the transformer 't'
// to the definition rectangle.
// The transformer may define a zoom, a translation or a rotation.
// We are using applyFloor so the resulting rectangle
// is correctly projected for drawing in the view.
// The object's coordinate system is defined by 'float' values
// and we need 'int' values to be able to draw. applyFloor will
// apply the transformation to 'r' and then calls Math.floor to
// translate 'float' values to 'int' values.
if (t != null)
t.applyFloor(r);
else
r.floor();
// The variable 'r' now contains the bounding rectangle
// of the object in the view's coordinate system and we will
// draw in this rectangle. In this rectangle, we will draw
// two ellipses. We first draw the shadow ellipse on the
// bottom right corner of the definition rectangle, then the main
// ellipse on the top-left corner. Each ellipse will be of size
// (r.width-thickness, r.height-thickness).
int thick = thickness;
// Computes a correct value for thickness.
// Since we want the size of the ellipses
// to be (r.width-thickness, r.height-thickness), we
// must check that the thickness is not too big.
if ((r.width <= thick) || (r.height <= thick))
thick = (int)Math.min(r.width, r.height);
// Sets the size of the ellipses.
r.width -= thick;
r.height -= thick;
// 'r' now contains the bounding area of the main ellipse.
// Computes a rectangle to draw the shadow.
// We copy the variable 'r', we will need it for the
// second ellipse.
IlvRect shadowRect = new IlvRect(r);
shadowRect.translate(thick, thick);
// Draws the shadow ellipse
dst.setColor(getShadowColor());
dst.fillArc((int)shadowRect.x,
(int)shadowRect.y,
(int)shadowRect.width,
(int)shadowRect.height,
0, 360);
// Draws the main ellipse.
dst.setColor(getColor());
dst.fillArc((int)r.x,
(int)r.y,
(int)r.width,
(int)r.height,
0, 360);
}
The method draw fills the two ellipses. The bounding rectangle, drawrect, actually covers both ellipses.
The method boundingBox simply transforms the bounding box. It creates a copy of the rectangle drawrect even if the transformer is null. This is so the returned rectangle can be modified by ILOG JViews.
Override the applyTransform method to apply a transformation to the shape of the ShadowEllipse rectangle.
Override the copy method to call the ShadowEllipse copy constructor to make a new instance.
/** * Copies the object. */ public IlvGraphic copy() { // We simply call the copy constructor that is defined above. return new ShadowEllipse(this); } |
Public accessors are added to the graphic object. These accessors deal with thickness and color. They appear in bold type in the following code example.
| Copyright © 1987-2007 ILOG S.A. All rights reserved. Documentation homepage. Legal terms. | PREVIOUS NEXT |