import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class NonFillDemo extends JFrame {
public NonFillDemo() {
this.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1d, 1d, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0);
this.getContentPane().add(new NonFillComponent(), gbc);
this.getContentPane().add(new BackgroundComponent(), gbc);
}
//Dummy Hintergrundbild
class BackgroundComponent extends JComponent {
Stroke stroke = new BasicStroke(10);
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setStroke(stroke);
g2.setColor(Color.ORANGE);
int w = getWidth()/10;
int h = getHeight()/10;
for (int i=1; i<=20; i++)
g2.drawLine(0, i*w, i*h, 0);
}
}
class NonFillComponent extends JComponent {
public void paintComponent(Graphics g) {
int w = getWidth();
int h = getHeight();
// Erstellen des Images mit AlphaComposite
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.DARK_GRAY);
g2.fillRect(20, 20, w-40, h-40);
g2.setColor(Color.WHITE);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OUT));
g2.fillOval(50, 50, 100, 100);
g2.dispose();
//Zeichnen des Bildes auf die Komponente
g.drawImage(image, 0, 0, null);
}
}
public static void main(String[] args) {
JFrame frame = new NonFillDemo();
frame.setBounds(0, 0, 300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}