banner



How To Set Image As Background Of Jpanel

Calling setOpaque(false) on the upper JPanel should piece of work.

From your comment, information technology sounds similar Swing painting may be broken somewhere -

First - you probably wanted to override paintComponent() rather than paint() in whatsoever component you accept pigment() overridden in.

2d - when y'all do override paintComponent(), you'll kickoff want to call super.paintComponent() start to practice all the default Swing painting stuff (of which honoring setOpaque() is one).

Instance -

          import java.awt.Color; import coffee.awt.Graphics;  import javax.swing.JFrame; import javax.swing.JPanel;   public class TwoPanels {     public static void main(Cord[] args) {          JPanel p = new JPanel();         // setting layout to null so nosotros can make panels overlap         p.setLayout(null);          CirclePanel topPanel = new CirclePanel();         // drawing should exist in blue         topPanel.setForeground(Color.blueish);         // background should be blackness, except it's not opaque, and then          // groundwork will not exist drawn         topPanel.setBackground(Color.blackness);         // set opaque to imitation - background non drawn         topPanel.setOpaque(fake);         topPanel.setBounds(50, fifty, 100, 100);         // add together topPanel - components pigment in order added,          // and so add topPanel showtime         p.add(topPanel);          CirclePanel bottomPanel = new CirclePanel();         // drawing in greenish         bottomPanel.setForeground(Colour.green);         // background in cyan         bottomPanel.setBackground(Colour.cyan);         // and it will evidence this time, because opaque is true         bottomPanel.setOpaque(true);         bottomPanel.setBounds(30, thirty, 100, 100);         // add together bottomPanel last...         p.add(bottomPanel);          // frame treatment code...         JFrame f = new JFrame("Two Panels");         f.setContentPane(p);         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         f.setSize(300, 300);         f.setLocationRelativeTo(null);         f.setVisible(true);     }      // Panel with a circle drawn on it.     individual static form CirclePanel extends JPanel {          // This is Swing, so override paint*Component* - not paint         protected void paintComponent(Graphics grand) {             // call super.paintComponent to become default Swing              // painting behavior (opaque honored, etc.)             super.paintComponent(g);             int x = 10;             int y = 10;             int width = getWidth() - twenty;             int superlative = getHeight() - 20;             g.drawArc(ten, y, width, height, 0, 360);         }     } }                  

Alternatively, consider The Glass Pane, discussed in the commodity How to Utilise Root Panes. You could draw your "Feature" content in the glass pane's paintComponent() method.

Addendum: Working with the GlassPaneDemo, I added an image:

          //Set upward the content pane, where the "main GUI" lives. frame.add(changeButton, BorderLayout.Southward); frame.add(new JLabel(new ImageIcon("img.jpg")), BorderLayout.CENTER);                  

and contradistinct the drinking glass pane's paintComponent() method:

          protected void paintComponent(Graphics k) {     if (point != null) {         Graphics2D g2d = (Graphics2D) g;         g2d.setRenderingHint(             RenderingHints.KEY_ANTIALIASING,             RenderingHints.VALUE_ANTIALIAS_ON);         g2d.setComposite(AlphaComposite.getInstance(             AlphaComposite.SRC_OVER, 0.3f));         g2d.setColor(Color.yellow);         g2d.fillOval(signal.10, point.y, 120, sixty);     } }                  

enter image description here

As noted here, Swing components must honor the opaque property; in this variation, the ImageIcon completely fills the BorderLayout.CENTER of the frame's default layout.

In my particular example information technology was easier to do this:

                      panel.setOpaque(true);  panel.setBackground(new Color(0,0,0,0,)): // any colour with alpha 0 (in this case the colour is blackness                  

How To Set Image As Background Of Jpanel,

Source: https://newbedev.com/how-to-set-a-transparent-background-of-jpanel

Posted by: hallashery1962.blogspot.com

0 Response to "How To Set Image As Background Of Jpanel"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel