如何在java中的二维矩阵中将双值表示为圆 [英] How to represent double values as circles in a 2d matrix in java

查看:288
本文介绍了如何在java中的二维矩阵中将双值表示为圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想编写一个矩阵浏览器,它可以让我重新排序矩阵的行和列。
对于这个porpouse,我使用了Jtable类。现在我遇到的问题是通过查看双值来重新排序矩阵是非常困难的,所以我想打印矩阵而不是双值,而是用圆圈表示圆的半径代表值。因此,我可以更快地区分大值和小值之间的区别。



任何人都知道如何使用JTable或任何表类将此双值转换为实心圆圈那个问题?

解决方案

这是一个

  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.setHorizo​​ntalAlignment(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;
}
}


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?

解决方案

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.

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

这篇关于如何在java中的二维矩阵中将双值表示为圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆