这是渲染组件的代码,可能会给您一些提示。
class PaintCommandListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean hasFocus) {
Component c = super.getListCellRendererComponent(list, value, index, isSelected, hasFocus);
if (c instanceof JLabel && value instanceof PaintCommand) {
JLabel l = (JLabel)c;
PaintCommand pc = (PaintCommand)value;
try {
BufferedImage bi = pc.getUndoImage();
double w = bi.getWidth();
double ideal = 200d;
double ratio = w/ideal;
int aw = (int)(w/ratio);
int ah = (int)(bi.getHeight()/ratio);
BufferedImage bj = new BufferedImage(
aw,ah,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bj.createGraphics();
g.drawImage(bi, 0, 0, aw, ah, null);
g.dispose();
l.setIcon(new ImageIcon(bj));
l.setText("");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return c;
}
}