米琪卡哇伊
以下是实现绘图界面的自定义渲染器示例Icon。这种方法可以更容易地控制文本和图标的相对位置。请注意,渲染器基于区间中的归一化值的假设进行缩放[0, 1); 您可能希望查询数据模型的最小值和最大值。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;
}}