views:

56

answers:

2

I'm trying to render my JTable cells with a subclassed JPanel and the cells should appear as coloured rectangles with a circle drawn on them. When the table displays initially everything looks OK but then when a dialog or something is displayed over the cells when it is removed the cells that have been covered do not rendered properly and the circles are broken up etc. I then have to move the scroll bar or extend the window to get them to redraw properly.

The paintComponent method of the component I'm using to render the cells is below:

protected void paintComponent(Graphics g) { 
    setOpaque(true);
    super.paintComponent(g); 
    Graphics2D g2d = (Graphics2D) g;
    GradientPaint gradientPaint = new GradientPaint(new Point2D.Double(0, 0),    Color.WHITE, new Point2D.Double(0,
            getHeight()), paintRatingColour);
    g2d.setPaint(gradientPaint);
    g2d.fillRect(0, 0, getWidth(), getHeight());

    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    Rectangle clipBounds = g2d.getClipBounds();
    int x = new Double(clipBounds.getWidth()).intValue() - 15;
    int y = (new Double(clipBounds.getHeight()).intValue() / 2) - 6;

    if (level != null) { 
        g2d.setColor(iconColour);
        g2d.drawOval(x, y, width, height);
        g2d.fillOval(x, y, width, height); 
    } 
}
+1  A: 

As @Gnoupi observes with level, it's not clear how width and height are initialized. To meet a similar need, this example extends DefaultTableCellRenderer and implements Icon for easier control over the geometry. It works without text as well.

trashgod
A: 

Thanks trashgod. I tried your example and it managed to highlight to me what is causing the problem. It looks as if this call to getClipBounds() is not a reliable way of helping to position the oval as with fixed x and y as in your example the problem is not occurring.

Andrew