Custom Painting JPanel

I am not very good at this, and I hope to get help from people who understand this problem much more than I do.

So here's the deal. My application has a JPanel background with an image drawn above it. Then there is a little JPanel that I am trying to create for a custom picture. I wanted to have a JPanel with rounded corners and a translucent background, so I modified the paintComponent method to fill a translucent rounded rectangle. But when I place the components inside, for example, JComboBox, a list of elements appears, and I click on another place to close it. JPanel paints itself in an original way, making it translucent around, but with a small rectangle painted in the background gray. I see that he has to do something with calling paintComponent on his parrent or paintChildren, but I don’t know how to organize these methods or where to put them. I also come across transparent colors stacked on top of each other.

Here is a sample source code:

public class RoundedPanel extends JPanel {

   private final int radius;


   public RoundedPanel(int cornerRadius) {
      radius=cornerRadius;
   }

   public void paintComponent(Graphics g) {
        Color bg = getBackground();
        g.setColor(new Color(bg.getRed(),bg.getGreen(),bg.getBlue(),40));
        g.fillRoundRect(0,0, getWidth()-1, getHeight()-1, radius, radius);
        g.setColor(new Color(0,0,0,70));
        g.drawRoundRect(0,0, getWidth()-1, getHeight()-1, radius, radius);
   }

   public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(400, 300);
        frame.setLocation(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel content = new JPanel();
        JPanel wl = new JPanel();
        JPanel el = new JPanel();
        JPanel sl = new JPanel();
        JPanel nl = new JPanel();
        RoundedPanel rp = new RoundedPanel(50);
        JComboBox combobox = new JComboBox();

        frame.setContentPane(content);
        content.setBackground(Color.red);
        content.setLayout(new BorderLayout());
        wl.add(new JButton("west"));
        el.add(new JButton("east"));
        sl.add(new JButton("south"));
        nl.add(new JButton("north"));
        content.add(wl,BorderLayout.WEST);
        content.add(el,BorderLayout.EAST);
        content.add(nl,BorderLayout.NORTH);
        content.add(sl,BorderLayout.SOUTH);

        content.add(rp,BorderLayout.CENTER);
        rp.setBackground(Color.BLACK);

        combobox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Třída 1.B", "Třída 1.C", "Třída 2.C" }));
        rp.add(combobox);
        frame.setVisible(true);
    }
}

I hope some of me help :-) thanks

EDIT: I found out that JComboBox (and its popup menu) draws correctly if the popup menu overlaps outside the JPanel that contains the JComboBox and has its own paintComponent method.

+3
source share
1 answer

Try the following:

RoundedPanel rp = new RoundedPanel(50);
rp.setOpaque(false);
+2
source

Source: https://habr.com/ru/post/1765263/


All Articles