Ich habe eine Komponente in Swing gezeichnet, und möchte jetzt auf die gezeichnete Komponente ein Panel legen. Hab es versucht einfach mit add(kom...). Dies geht leider aber nicht. Was mache ich hier verkehrt?
Java:
public class JavaSwingDrawDemo extends JFrame {
public JavaSwingDrawDemo() {
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationByPlatform(true);
setVisible(true);
JComponent centerPanel = createCenterPanel();
JPanel centerPanel1 = new JPanel();
centerPanel1.setBackground(Color.RED);
centerPanel.add(centerPanel1, BorderLayout.CENTER);
centerPanel.setComponentZOrder(centerPanel1, 0);
centerPanel.repaint();
BorderLayout layout = new BorderLayout();
getContentPane().setLayout(layout);
getContentPane().add(centerPanel, BorderLayout.CENTER);
}
private JComponent createCenterPanel() {
JComponent panel1 = new JComponent() {
@Override
public void paint(Graphics g) {
super.paint(g);
final Graphics2D g2 = (Graphics2D) g.create();
// draw the content
g2.setColor(Color.WHITE);
final int parrCompX = 20;
final int parrCompY = 20;
final int parrentInnerDistance = 20;
final Dimension parrentDim = getContentPane().getSize();
final int roundCornerSize = 15;
final int width = parrentDim.width - (parrentInnerDistance * 2);
final int height = parrentDim.height - (parrentInnerDistance * 2);
g2.fillRoundRect(parrCompX, parrCompY, width, height, roundCornerSize, roundCornerSize);
// draw the border
g2.setStroke(new BasicStroke(3f));
g2.setColor(Color.BLACK);
g2.drawRoundRect(parrCompX, parrCompY, width, height, roundCornerSize, roundCornerSize);
g2.dispose();
}
};
return panel1;
}
public static void main(String[] args) {
new JavaSwingDrawDemo();
}
}