我一直在搞乱这个问题,需要澄清透明度和显示对象(在这种情况下是矩形)。
//paintComponent ======================================================================
@Override public void paintComponent(Graphics g) {
super.paintComponent(g); // Ask parent to paint background.
Graphics2D g2d = (Graphics2D)g;
// create the transparency for the text & block
Composite origComp = g2d.getComposite();
Composite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f);
// create the rectangle to represent the memory partition block
// x = address position h = amount of memory (y & w are predefined for the display block)
Rectangle2D rect = new Rectangle2D.Double(x, y, w, h); // create the rectangle
g2d.setComposite(comp); // set it to transparent
g2d.setPaint(partColor); // set it's color
g2d.fill(rect); // fill it in
g2d.setComposite(origComp);
// draw the text with color, type and size and center the text in the block created above
g2d.setPaint(Color.black);
g2d.setFont(new Font("Tahoma", Font.PLAIN, 12));
g2d.drawString(text, (int)(rect.getCenterX()-text.length()), (int)rect.getCenterY());
}
有人会认为,只要不应用复合材料,就可以使这个矩形看起来几乎透明。出于某种原因,它没有。我错过了什么吗?