Results 1 to 10 of 10
- 08-29-2010, 09:20 PM #1
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
a setOpaque(false) JPanel is not transparent
These classes should create a 400X400 window showing a blue circle on a red background. Problem: they show a blue circle on a grey (empty?) background.
Java Code:public class test { public static void main(String[] args) { new testWindow(); } }Java Code:import javax.swing.JFrame; public class testWindow extends JFrame{ private JFrame f=new JFrame(); public testWindow() { f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new background()); forground p=new forground(); p.setOpaque(false); f.add(p); f.pack(); f.setVisible(true); } }Java Code:import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.JPanel; public class background extends JPanel { public background() { } public Dimension getPreferredSize() { return new Dimension (400,400); } public void paint(Graphics g) { super.paintComponents(g); g.setColor(Color.red); g.fillRect(0,0,400,400); } }Can anyone show me how to make the "forground" JPanel transparent? As this is a simple compilable version of my bigger program, painting both the background and the forground in one JPanel doesn't help me.Java Code:import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.JPanel; public class forground extends JPanel{ public forground() { } public void paint(Graphics g) { super.paintComponents(g); g.setColor(Color.blue); g.fillOval(50,50,100,100); } public Dimension getPreferredSize() { return new Dimension (400,400); } }
- 08-29-2010, 09:51 PM #2
Member
- Join Date
- Jul 2008
- Posts
- 62
- Rep Power
- 0
your main problem is here
f.add(new background());
then
f.add(p);
look up how borderLayout works, to see what needs to be added to what
yuk!Java Code:public void paint(Graphics g) { super.paintComponents(g);
try
Java Code://public void paint(Graphics g) public void paintComponent(Graphics g) { //super.paintComponents(g); super.paintComponent(g);//singular
- 08-29-2010, 10:00 PM #3
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
Ok, So I changed the code to
And I've changed the "super.paintComponents(g);" to "super.paintComponent(g);". However the output is still the same. Note that I want the 2 JPanels to be displayed on top of each other and that any part of the forground JPanel that isn't painted, the background shows through. So that the result is a blue circle on a red background.Java Code:import java.awt.BorderLayout; import javax.swing.JFrame; public class testWindow extends JFrame{ private JFrame f=new JFrame(); public testWindow() { f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(new BorderLayout()); f.add(new background(), BorderLayout.CENTER); forground p=new forground(); p.setOpaque(true); f.add(p, BorderLayout.CENTER); f.pack(); f.setVisible(true); } }
-
I think that when adding two components BorderLayout.CENTER, the second one completely obscures the first, regardless of the second's opaque setting. I believe that a JLayeredPane may suit your purposes better.
- 08-29-2010, 11:17 PM #5
Member
- Join Date
- Jul 2008
- Posts
- 62
- Rep Power
- 0
> Note that I want the 2 JPanels to be displayed on top of each other
OverlayLayout panel to contain the 2 panels,
then add the overlayLayoutPanel to BorderLayout.CENTER
can't see why adding foregroundPanel to backgroundPanel,
then adding backgroundPanel to CENTER wouldn't also work
the way you want
- 08-30-2010, 05:12 PM #6
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
I can't see that either, all I can see that it doesn't.
Using JLayeredPane, I think it should be something like this:
However these lines gives problems: "p.addImpl(d, BorderLayout.CENTER, -1);" and "p.addImpl(d, BorderLayout.CENTER, -1);".Java Code:import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JLayeredPane; public class testWindow extends JFrame{ private JFrame f=new JFrame(); private JLayeredPane p=new JLayeredPane(); public testWindow() { f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(new BorderLayout()); p.addImpl(new background(), BorderLayout.CENTER, 0); forground d=new forground(); d.setOpaque(false); p.addImpl(d, BorderLayout.CENTER, -1); p.add(new background(), BorderLayout.CENTER); f.pack(); f.setVisible(true); } }
I looked at the API, send in the right parameters (I think), added it to the JFrame, but it says it isn't visible. When I run it anyway, this error occurs:
Java Code:Exception in thread "main" java.lang.Error: Unresolved compilation problems: The method addImpl(Component, Object, int) from the type JLayeredPane is not visible The method addImpl(Component, Object, int) from the type JLayeredPane is not visible at testWindow.<init>(testWindow.java:13) at test.main(test.java:5)
Last edited by imorio; 08-30-2010 at 05:15 PM.
-
If you're going to use JLayeredPanes, you'd best read the tutorials as you don't appear to be using it correctly.
- 08-31-2010, 06:23 PM #8
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
I read this tutorial: Java Platform SE 6. However my problems remain. Based on the source code found in that tutorial I removed my forground and background classes and rewrote my testWindow class to:
However this just creates a window with minimal size and when I enlarge the window, it is empty. This is odd because I setPreferredSize twice and add everything to the JFrame as I think I should. This is very frustrating, because debugging only leads me to conclude that the things I want to appear don't appear.Java Code:import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JLayeredPane; public class testWindow extends JFrame{ private JFrame f=new JFrame(); private JLayeredPane p=new JLayeredPane(); public testWindow() { f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(new BorderLayout()); p.setPreferredSize(new Dimension(400, 400)); p.add(createBackground(), new Integer(0)); p.add(createForground(), new Integer(-1)); p.add(new background(), BorderLayout.CENTER); f.setPreferredSize(new Dimension(400,400)); f.setVisible(true); } public JLabel createBackground() { JLabel label=new JLabel(); label.setVerticalAlignment(JLabel.TOP); label.setHorizontalAlignment(JLabel.CENTER); label.setBackground(Color.red); label.setBounds(0,0,400,400); label.setOpaque(true); return label; } public JLabel createForground() { JLabel label=new JLabel(); label.setVerticalAlignment(JLabel.TOP); label.setHorizontalAlignment(JLabel.CENTER); label.setBackground(Color.blue); label.setBounds(50,50,100,100); label.setOpaque(true); return label; } }
-
1) Where do you add your JLayeredPane to the JFrame?
2) What is new background() supposed to do?
3) If your JLabels are opaque, how does one see through them?
4) What are you trying to do when you add something to a JLayeredPane BorderLayout.CENTER?
Luck
- 09-01-2010, 09:02 PM #10
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
Similar Threads
-
transparent jframe?
By majkel in forum AWT / SwingReplies: 12Last Post: 10-09-2010, 05:06 PM -
Transparent layer
By disabled in forum SWT / JFaceReplies: 0Last Post: 03-20-2010, 04:04 PM -
swirling becomes false,why?
By arefeh in forum New To JavaReplies: 2Last Post: 01-18-2010, 06:12 PM -
SWT Transparent button
By ynnorj in forum SWT / JFaceReplies: 2Last Post: 03-04-2009, 06:33 AM -
Transparent JTextPane
By Ada in forum AWT / SwingReplies: 1Last Post: 05-31-2007, 09:50 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks