4

so I want to write a matrix explorer which enables me to reorder rows and columns of a matrix. For this porpouse I used the Jtable class. Now the problem that I have is that it is very difficult to reorder a matrix by looking at double values, so I would like to print the matrix not with the double values but with circles in which the radius of the circle represents the value. So that I can tell the difference between big values and small values quicker.

Anybody has any idea how I can turn this double values into filled circles with JTable or any table class for that matter?

woolagaroo
  • 1,542
  • 2
  • 22
  • 31

2 Answers2

12

Here's an example of a custom renderer that implements the Icon interface to do the drawing. This approach permits easier control over the relative positioning of the text and icon. Note that the renderer scales based on the assumption of normalized values in the interval [0, 1); you may want to query your data model for the minimum and maximum values instead.

icon renderer

class DecRenderer extends DefaultTableCellRenderer implements Icon {

    private static final int SIZE = 32;
    private static final int HALF = SIZE / 2;
    DecimalFormat df;

    public DecRenderer(DecimalFormat df) {
        this.df = df;
        this.setIcon(this);
        this.setHorizontalAlignment(JLabel.RIGHT);
        this.setBackground(Color.lightGray);
    }

    @Override
    protected void setValue(Object value) {
        setText((value == null) ? "" : df.format(value));
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(Color.blue);
        double v = Double.valueOf(this.getText());
        int d = (int)(v * SIZE);
        int r = d / 2;
        g2d.fillOval(x + HALF - r, y + HALF - r, d, d);
    }

    @Override
    public int getIconWidth() {
        return SIZE;
    }

    @Override
    public int getIconHeight() {
        return SIZE;
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • that really goooood, phaaa +1 – mKorbel Jul 13 '11 at 20:36
  • Cell Renderers, be it ListCellRenderer or TableCellRenderer, they all go over my head, LOL :-), still I do scratch my head, when it comes to these topics, though as I search and read more and more, it seems they are not something to be left behind, when it comes to Swing. Wonderful things one can do with them, in many aspects. I had seen this example a long time back, but then painting was a distant thing for me in Swing. Now will run through all your examples slowly slowly, learning many new things :-) – nIcE cOw Aug 19 '12 at 15:54
  • 1
    @GagandeepBali: Such renderers and editors are examples of the [_flyweight pattern_](http://en.wikipedia.org/wiki/Flyweight_pattern), an underlying concept I found helpful. – trashgod Aug 19 '12 at 16:05
  • This is one interesting thingy. Patterns are interesting too, seems like another thing in my `ToDoList` now :-) – nIcE cOw Aug 19 '12 at 16:19
  • +1 just because this post made me aware of rendering hints. :-) Quite nice. – The111 Jan 18 '13 at 03:04
  • See also this related [example](http://stackoverflow.com/a/21756629/230513) that varies brightness. – trashgod Feb 13 '14 at 19:59
3

You will have to write your custom Cell Renderer.

The component will be used as a rubber stamp; the paint method is called for each cell.

Draw the circle in the paint method;

g.fillOval(x - radius / 2, y - radius / 2, radius, radius);

Will draw a circle of radius with center point at (x,y).

Pindatjuh
  • 10,550
  • 1
  • 41
  • 68