简体   繁体   English

如何在java中的2d矩阵中将double值表示为圆圈

[英]How to represent double values as circles in a 2d matrix in java

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. 对于这个porpouse,我使用了Jtable类。 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? 任何人都知道我怎么能用JTable或任何表类将这个双值转换成实心圆?

Here's an example of a custom renderer that implements the Icon interface to do the drawing. 这是一个自定义渲染器的示例,它实现了用于绘图的Icon界面。 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) ; 注意,渲染器基于区间[0, 1)的归一化值的假设进行缩放; you may want to query your data model for the minimum and maximum values instead. 您可能希望查询数据模型的最小值和最大值。

图标渲染器

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;
    }
}

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. 为每个单元paint方法。

Draw the circle in the paint method; 在paint方法中绘制圆圈;

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

Will draw a circle of radius with center point at (x,y) . 将绘制一个radius圆,中心点为(x,y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM