I have a Java code where I implemented a translucent JPanel with an image drawn on it with Graphics 2D. This image is a PNG that includes a white rectangle, 80% opaque, all over
This is how I might approach this kind of idea...
public class OverlayTextArea {
public static void main(String[] args) {
new OverlayTextArea();
}
public OverlayTextArea() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new ImagePane());
frame.setLayout(new BorderLayout());
frame.add(new TransparentTextArea());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TransparentTextArea extends JTextArea {
public TransparentTextArea() {
setOpaque(false);
setBorder(new CompoundBorder(new EmptyBorder(10, 10, 10, 10), new LineBorder(Color.LIGHT_GRAY)));
}
@Override
protected void paintComponent(Graphics g) {
g.setColor(new Color(255, 255, 255, 128));
Insets insets = getInsets();
int x = insets.left;
int y = insets.top;
int width = getWidth() - (insets.left + insets.right);
int height = getHeight() - (insets.top + insets.bottom);
g.fillRect(x, y, width, height);
super.paintComponent(g);
}
}
public class ImagePane extends JPanel {
private BufferedImage background;
public ImagePane() {
try {
background = ImageIO.read(new File("/path/to/background.img"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
int x = (getWidth() - background.getWidth()) / 2;
int y = (getHeight() - background.getHeight()) / 2;
g.drawImage(background, x, y, this);
}
}
}
}
Feedback
super.paintComponent
, failing to do so can lead it some serious trouble, especially when you're dealing with transparent components.paintXxx
methods, like loading images. These methods are intended to return quickly and may be called multiple times within succession...