Results 1 to 4 of 4
  1. #1
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default 2 JPanels, one partly see-through

    I want to display a (complicated) background and to display things moving on that background. I thought I could do this by adding one JPanel displaying the background and then adding another JPanel on top of it with the moving bits. I tested this in the following way:

    I added the JPanel with the background to a JFrame (center with the borderLayout) and then added another empty JPanel on top of it (by also adding it to the center). However the second JPanel turned out to be opaque, so it simplied showed the empty JPanel on top. My question is, is it possible to make sure that when there is no Color added in some places of the second JPanel that then the bottom JPanel is shown?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You could make the overlaying JPanel non-opaque by calling setOpaque(false) on it. Are you using a JLayeredPane for this as it seems tailor-made for your problem.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Also, when I have a background as you describe, I often draw it in a BufferedImage and simply display that buffered image as the background for my JPanel (the first call in my JPanel's paintComponent method, after super.paintComponent(g) is a call to g.drawImage(myBufferedImage....)), and then draw my dynamic stuff in the same paintComopent but just after the BufferedImage.

  4. #4
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    This is strange, even when I call setOpaque(false), the JPanel on top still remains opaque. Only when I add the background-pane first is it shown.

    I tried adding a paint methode to the JPanel that is supposed to go on top, to see if drawing something on a part of the panel would make the other parts, but the whole JPanel is still opaque. The relevant part of the code:
    Java Code:
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		huidigSpel= new spel(this, route, veldGrootte); //making the background
    		huidigCreepDisplay = new creepDisplay(huidigSpel);//making the forground
    		huidigCreepDisplay.setOpaque(false);//setting the forground opaque
    		speelveld.setLayout(new BorderLayout());//adding both of them to a shared JPanel
    		speelveld.add("Center", huidigSpel);//adding them to one JPanel together
    		speelveld.add("Center", huidigCreepDisplay);
    		
    		f.add("Center", speelveld);//adding that to the JFrame
    		f.pack();
    ps: adding the JPanel's directly to the JFrame instead of to a shared JPanel had ne effect.

Similar Threads

  1. Problems regarding JPanels in JPanels
    By ColtonPhillips in forum AWT / Swing
    Replies: 2
    Last Post: 07-19-2010, 08:33 PM
  2. Partly editable SourceViewer
    By shravan28 in forum SWT / JFace
    Replies: 7
    Last Post: 05-28-2009, 11:29 AM
  3. HELP with jPanels
    By maverik_vz in forum AWT / Swing
    Replies: 1
    Last Post: 03-12-2009, 11:46 AM
  4. Replies: 3
    Last Post: 01-11-2009, 11:20 PM
  5. Returning a string (Partly Bold)
    By TampaTechGuy in forum New To Java
    Replies: 3
    Last Post: 02-03-2008, 09:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •