您可以继承JPanel - 这是我的ImagePanel的一个片段,它可以将图像放置在五个位置之一:左上角、右上角、中间、左下角或右下角:
protected void paintComponent(Graphics gc) {
super.paintComponent(gc);
Dimension cs=getSize();
gc=gc.create();
gc.clipRect(insets.left,insets.top,(cs.width-insets.left-insets.right),(cs.height-insets.top-insets.bottom));
if(mmImage!=null) { gc.drawImage(mmImage,(((cs.width-mmSize.width)/2) +mmHrzShift),(((cs.height-mmSize.height)/2) +mmVrtShift),null); }
if(tlImage!=null) { gc.drawImage(tlImage,(insets.left +tlHrzShift),(insets.top +tlVrtShift),null); }
if(trImage!=null) { gc.drawImage(trImage,(cs.width-insets.right-trSize.width+trHrzShift),(insets.top +trVrtShift),null); }
if(blImage!=null) { gc.drawImage(blImage,(insets.left +blHrzShift),(cs.height-insets.bottom-blSize.height+blVrtShift),null); }
if(brImage!=null) { gc.drawImage(brImage,(cs.width-insets.right-brSize.width+brHrzShift),(cs.height-insets.bottom-brSize.height+brVrtShift),null); }
}
MemoryImageSource
可能比将它们转换为JPEG或PNG格式并像大多数答案建议的那样使用ImageIO
读取更有效率。您可以使用createImage
从构造有图像数据的MemoryImageSource
中获取一个Image
,并按照其中一个答案所建议的方式进行显示。 - Alden