1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
/**
* {@inheritDoc}
*/
@Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
selected = isSelected;
if (value instanceof Stroke) {
stroke = (Stroke) value;
}
else {
stroke = null;
}
return this;
}
/**
* {@inheritDoc}
*/
@Override protected void paintComponent(Graphics graphics) {
// Dans mon cas le rendu est delegue a une classe utilitaire car j'utilise la meme methode de rendu pour un ListCellRenderer, un TreeCellRenderer et un TableCellRenderer.
helper.paintComponent((Graphics2D) graphics, this, stroke, selected, getStrokePaint());
}
[...]
public void paintComponent(Graphics2D graphics, JComponent component, Stroke stroke, boolean selected, Paint strokePaint) {
Dimension size = component.getSize();
Insets insets = component.getInsets();
int width = size.width - (insets.left + insets.right);
int height = size.height - (insets.top + insets.bottom);
Graphics2D g2 = (Graphics2D) graphics.create(insets.left, insets.top, width, height);
try {
drawComponent(g2, component, width, height, stroke, strokePaint);
// Draw the selection border (if any).
if (selected) {
g2.setColor(component.getBackground());
g2.setStroke(BORDER_STROKE);
g2.drawRect(0, 0, width - 1, height - 1);
}
}
finally {
g2.dispose();
}
}
/**
* Really draw the component.
* @param graphics The graphics context in which to paint.
* @param width The width of the drawing area.
* @param height The height of the drawing area.
*/
private void drawComponent(Graphics2D graphics, JComponent component, double width, double height, Stroke stroke, Paint strokePaint) {
if (stroke != null) {
drawStroke(graphics, component, width, height, stroke, strokePaint);
}
}
/**
* Paints the stroke.
* @param graphics The graphics context in which to draw.
* @param width The width of the drawing area.
* @param height The height of the drawing area.
*/
private void drawStroke(Graphics2D graphics, JComponent component, double width, double height, Stroke stroke, Paint strokePaint) {
Stroke oldStroke = graphics.getStroke();
RenderingHints oldHints = graphics.getRenderingHints();
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
double margin = width / 10.0;
line.x1 = margin;
line.x2 = width - margin;
line.y1 = height / 2.0;
line.y2 = line.y1;
graphics.setPaint(strokePaint);
graphics.setStroke(stroke);
graphics.draw(line);
graphics.setStroke(oldStroke);
graphics.setRenderingHints(oldHints);
} |
Partager