Java Reference
In-Depth Information
GeneralPath polygon =
new GeneralPath(GeneralPath.WIND_EVEN_ODD, xcoords.length);
polygon.moveTo(x + 25, y);
for (int i = 0; i < xcoords.length; i++) {
polygon.lineTo(xcoords[i], ycoords[i]);
}
polygon.closePath();
g2.fill(polygon);
BasicStroke wideStroke = new BasicStroke(2.0f);
g2.setColor(Color.black);
g2.setStroke(wideStroke);
g2.draw(polygon);
}
public void setColor(Color color) {
m_color = color;
}
}
The paint frame bundle contains another implementation of the SimpleShape API :
org.foo.paint.DefaultShape . This class lazily delegates to the real shape via the
OSG i service registry, so it also needs to implement the new setColor() method. The
correct implementation follows the same approach used in DefaultShape.draw() :
check that you have access to the real shape from the registry and, if you don't,
request it. You'll use a broken implementation instead and assume you already have
access to the shape instance:
Updates
assigned color
public void setColor(Color color) {
m_shape.setColor(color);
}
This sort of mistake could be made by a new team member who doesn't know about
the lazy delegation approach and assumes that m_shape has been initialized else-
where. If the application happened to call draw() early on, this bug could go unno-
ticed for a long time, because m_shape would always be valid by the time the code
reached setColor() . But one day, someone may reasonably change the application so
it calls setColor() first, as follows from the ShapeComponent class, and the bug will
bite. (This example may seem a little contrived, but it's surprisingly hard to write bad
code when you really want to!)
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
SimpleShape shape = m_frame.getShape(m_shapeName);
shape.setColor(getForeground());
shape.draw(g2, new Point(getWidth() / 2, getHeight() / 2));
}
You now have a broken OSG i application, which will throw an exception whenever you
try to paint shapes. Let's see if you can debug it using the JDK provided debugger, jdb
( http://java.sun.com/javase/6/docs/technotes/tools/solaris/jdb.html ) .
Search WWH ::




Custom Search