Results 1 to 5 of 5
  1. #1
    Dustin T is offline Member
    Join Date
    Sep 2011
    Location
    Madison,WI
    Posts
    3
    Rep Power
    0

    Default JLayeredPane - JApplet trouble

    Hi Everyone,
    I am fairly new to Java. I took a computer science class that was essentially an intro to Java.

    I am trying to make an applet that displays multiple transparent images of the same size as layers of the same container. The eventual goal is to be able to change out a layer for another image using some drop down menus.

    The problem I am stuck on is getting anything to show up in a JLayeredPane. I tried adding it to the contentPane of the applet. I tried setting a JInternalFrame's( or JPanel's) layeredPane to an instance of CustomizeLuckyDog JLayeredPane and setting the JApplets contentPane to the container.

    I have tried adding the images to the JLayeredPane using add(comp, Integer) and add(comp) and add(comp, JLayeredPane.DEFAULT).

    I tried various ways of bringing in the images, including making class extending JLabel and @Override paint(). In an attempt to eliminate the images as the issue I am just trying to add one JLabel b which is just a string to a Layer. I can't even get that to show up.

    I have searched around a lot and still have run into a wall. I tried validate() after adding to the layer. I tried adjusting all sizes and bounds. I am sure I am just making an obvious error.

    Thanks for the help, code below,
    Dustin T.
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JInternalFrame;
    import javax.swing.JLabel;
    import javax.swing.JLayeredPane;
    import javax.swing.JPanel;
    
    public class CustomizeAmpApplet extends JApplet {
    	// TODO Auto-generated method stub
    	URL backgroundSrc;
    	URL cornerSrc;
    	URL trimSrc;
    	URL grillSrc;
    	URL faceplateSrc;
    	
    	BufferedImage backgroundImg;
    	
    	ImageIcon background;
    	ImageIcon corner;
    	ImageIcon trim;
    	ImageIcon grill;
    	ImageIcon faceplate;
    	
    	JLabel b;
    	JLabel c;
    	JLabel t;
    	JLabel g;
    	JLabel f;
    	
    	JButton button;
    	
    	CustomizeLuckyDog amp;
    	
    	JPanel ampGui;
    	JInternalFrame customAmp;
    	
    
    public void init(){
    	this.setSize(614, 461);
        amp = new CustomizeLuckyDog();
        ampGui = new JPanel();
        ampGui.setLayout(new BorderLayout());
        customAmp = new JInternalFrame();
        customAmp.getRootPane().setLayeredPane(amp);
    	ampGui.add(customAmp, BorderLayout.CENTER);	
    	this.setContentPane(customAmp);
    	ampGui.setVisible(true);
    	this.setVisible(true);
    }
    public class CustomizeLuckyDog extends JLayeredPane {
    	CustomizeLuckyDog(){
    		super();
    		setLayout(null);
    		this.setPreferredSize(new Dimension(614, 461));
    		try {
    			backgroundSrc = new URL(getCodeBase(), "LDbackground.png");
    			System.out.print(backgroundSrc);
    			cornerSrc = new URL(getCodeBase(), "fullres/LDcorner1.png");
    			trimSrc = new URL(getCodeBase(), "fullres/LDtrim1.png");
    			grillSrc = new URL(getCodeBase(), "fullres/LDgrill1.png");
    			faceplateSrc = new URL(getCodeBase(), "fullres/LDfaceplate1.png");	
    	    } catch (MalformedURLException e) {
    	    	System.out.print("Image URL error");
    	    	System.exit(0);
    	    }
    	    try {
    			backgroundImg = ImageIO.read(new File("C://schoolFlash//CompSci//Intro_To_Android//CustomizeAmp//src//images//fullres//LDbackground.png"));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		background = new ImageIcon(backgroundSrc);
    		corner = new ImageIcon(cornerSrc);
    		trim = new ImageIcon(trimSrc);
    		grill = new ImageIcon(grillSrc);
    		faceplate = new ImageIcon(faceplateSrc);
    		b = new JLabel("background");
    		//b.setOpaque(false);
    		c = new JLabel(corner);
    		c.setOpaque(false);
    		c.setPreferredSize(new Dimension(614,461));
    		t = new JLabel(trim);
    		t.setOpaque(false);
    		t.setPreferredSize(new Dimension(614,461));
    		g = new JLabel(grill);
    		g.setOpaque(false);
    		g.setPreferredSize(new Dimension(614,461));
    		f = new JLabel(faceplate);
    		f.setOpaque(false);
    		f.setPreferredSize(new Dimension(614,461));
    	
    	this.add(b, new Integer(0));
    	b.setPreferredSize(new Dimension(614,461));
    	b.setVisible(true);
    	//this.add(g,4);
    	//this.add(f,3);
    	//this.add(t,2);
    	//this.add(c,1);
    	this.setVisible(true);
    	this.validate();
    	}
    	public void setLayerImage(BufferedImage layer, URL image){
    		
    	}
    }
    }
    Last edited by Norm; 09-19-2011 at 06:49 PM. Reason: added code tags

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: JLayeredPane - JApplet trouble

    I don't see where you use a JLayeredPane anywhere. Start with: How to Use Layered Panes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) for a working example.

    A JLayeredPane doesn't use a layout manager so you would need to set the size of each image using the preferred size of the image.

  3. #3
    Dustin T is offline Member
    Join Date
    Sep 2011
    Location
    Madison,WI
    Posts
    3
    Rep Power
    0

    Default Re: JLayeredPane - JApplet trouble

    I read about using the JLayeredPane on Sun's site where you pointed already. Here is code reduced down to where I am stuck. I created a Class which extends JLayeredPane. In this example I am trying to just add a JLabel but I see nothing. Is it not possible to extend JLayeredPane?

    Thanks for the response.

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;

    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JInternalFrame;
    import javax.swing.JLabel;
    import javax.swing.JLayeredPane;
    import javax.swing.JPanel;

    public class CustomizeAmpApplet2 extends JApplet {
    // TODO Auto-generated method stub
    URL backgroundSrc;
    URL cornerSrc;
    URL trimSrc;
    URL grillSrc;
    URL faceplateSrc;

    BufferedImage backgroundImg;

    ImageIcon background;
    ImageIcon corner;
    ImageIcon trim;
    ImageIcon grill;
    ImageIcon faceplate;

    JLabel b;
    JLabel c;
    JLabel t;
    JLabel g;
    JLabel f;

    JButton button;

    CustomizeLuckyDog amp;

    JPanel ampGui;
    JInternalFrame customAmp;


    public void init(){
    this.setSize(614, 461);
    amp = new CustomizeLuckyDog();
    ampGui = new JPanel();
    ampGui.setLayout(new BorderLayout());
    customAmp = new JInternalFrame();
    customAmp.getRootPane().setLayeredPane(amp);
    ampGui.add(customAmp, BorderLayout.CENTER);
    this.setContentPane(customAmp);
    ampGui.setVisible(true);
    this.setVisible(true);
    }
    public class CustomizeLuckyDog extends JLayeredPane {
    CustomizeLuckyDog(){
    super();
    setLayout(null);
    this.setPreferredSize(new Dimension(614, 461));

    b = new JLabel("background");
    //b.setOpaque(false);


    this.add(b, new Integer(0));
    b.setPreferredSize(new Dimension(614,461));
    b.setVisible(true);
    this.setVisible(true);
    this.validate();
    }
    public void setLayerImage(BufferedImage layer, URL image){

    }
    }
    }

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: JLayeredPane - JApplet trouble

    1) Why didn't you use "Code" tags

    2) Why did you extend JLayeredPane? Does the tutorial do that?

  5. #5
    Dustin T is offline Member
    Join Date
    Sep 2011
    Location
    Madison,WI
    Posts
    3
    Rep Power
    0

    Default Re: JLayeredPane - JApplet trouble

    So I went and reread the tutorials for applet and using JLayeredPane. What I was not doing was setting the Bounds of the component after adding it to the JLayeredPane. Also, when using JApplet you have to myapplet.setLayeredPane to the created JLayeredPane. Thanks for the heads up on the code tag. This my first time on the forum. Thanks for the help, seems like extending JLayeredPane was part of my issue too. Here is the code I got working. The result is 5 images in 5 separate layers. They are transparent .png files giving me a composite image. I will be building a button/menu driven interface which is why the one button shows up on the left.

    code:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.image.BufferedImage;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import javax.swing.ImageIcon;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JLayeredPane;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class CustomizeAmpApplet extends JApplet {
    	// TODO Auto-generated method stub
    	URL backgroundSrc;
    	URL cornerSrc;
    	URL trimSrc;
    	URL grillSrc;
    	URL faceplateSrc;
    
    	BufferedImage backgroundImg;
    
    	ImageIcon background;
    	ImageIcon corner;
    	ImageIcon trim;
    	ImageIcon grill;
    	ImageIcon faceplate;
    
    	JLabel b;
    	JLabel c;
    	JLabel t;
    	JLabel g;
    	JLabel f;
    
    	JButton button;
    
    	CustomizeLuckyDog amp;
    
    	JPanel ampGui;
    	JLayeredPane customAmp;
    
    
    	public void init(){
    		this.setSize(614, 461);
    		try {
    			SwingUtilities.invokeAndWait(new Runnable() {
    				public void run() {
    					buildGui();
    				}
    			});
    		} catch (Exception e) {
    			System.err.println("createGUI didn't complete successfully");
    		}
    	}
    	public void buildGui(){
    		amp = new CustomizeLuckyDog();
    		//this.setContentPane(amp);
    		this.setVisible(true);
    		this.setLayeredPane(customAmp);
    		//	ampGui = new JPanel();
    		//	ampGui.setLayout(new BorderLayout());
    		//	ampGui.add(amp, BorderLayout.CENTER);
    		//	ampGui.setSize(700, 560);
    		setContentPane(amp);
    
    	}
    	public class CustomizeLuckyDog extends JPanel {
    		CustomizeLuckyDog(){
    			super();
    			setLayout(new BorderLayout());
    			this.setPreferredSize(new Dimension(614, 461));
    			button = new JButton();
    			this.add(button, BorderLayout.WEST);
    			//this.setBounds(0,0,614,461);
    			this.setOpaque(false);
    
    			//get URL for images
    			try {
    				backgroundSrc = new URL(getCodeBase(), "\\LDBackground.png");
    				System.out.print(backgroundSrc);
    				cornerSrc = new URL(getCodeBase(), "\\LDCorners1.png");
    				trimSrc = new URL(getCodeBase(), "\\LDTrim1.png");
    				grillSrc = new URL(getCodeBase(), "\\LDGrill1.png");
    				faceplateSrc = new URL(getCodeBase(), "\\LDFaceplate1.png");	
    			} catch (MalformedURLException e) {
    				System.out.print("Image URL error");
    				System.exit(0);
    			}
    
    			//create image icons
    			background = new ImageIcon(backgroundSrc);//"c:\\LDBackground.png");
    			corner = new ImageIcon(cornerSrc);
    			trim = new ImageIcon(trimSrc);
    			grill = new ImageIcon(grillSrc);
    			faceplate = new ImageIcon(faceplateSrc);
    
    			//create JLabels with the image icons
    			//setOpaque(false) to let others show through(turned out to not be needed since
    			//my png files have an alpha)
    
    			b = new JLabel(background);
    			//b.setOpaque(false);
    			b.setPreferredSize(new Dimension(614,461));
    
    			c = new JLabel(corner);
    			//c.setOpaque(false);
    			c.setPreferredSize(new Dimension(614,461));
    
    			t = new JLabel(trim);
    			//t.setOpaque(false);
    			t.setPreferredSize(new Dimension(614,461));
    
    			g = new JLabel(grill);
    			//g.setOpaque(false);
    			g.setPreferredSize(new Dimension(614,461));
    
    			f = new JLabel(faceplate);
    			//f.setOpaque(false);
    			f.setPreferredSize(new Dimension(614,461));
    
    			//create JLayeredPane
    			customAmp = new JLayeredPane();
    
    			//add JLabels to their own layers in the JLayeredPane
    			customAmp.add(b, new Integer(0));
    			b.setPreferredSize(new Dimension(614,461));
    			b.setBounds(button.getPreferredSize().width+10,0,614,461);
    
    			customAmp.add(c, new Integer(1));
    			c.setPreferredSize(new Dimension(614,461));
    			c.setBounds(button.getPreferredSize().width+10,0,614,461);
    			c.setVisible(true);
    
    			customAmp.add(t, new Integer(2));
    			t.setPreferredSize(new Dimension(614,461));
    			t.setBounds(button.getPreferredSize().width+10,0,614,461);
    			t.setVisible(true);
    
    			customAmp.add(g, new Integer(3));
    			g.setPreferredSize(new Dimension(614,461));
    			g.setBounds(button.getPreferredSize().width+10,0,614,461);
    			g.setVisible(true);
    
    			customAmp.add(f, new Integer(4));
    			f.setPreferredSize(new Dimension(614,461));
    			f.setBounds(button.getPreferredSize().width+10,0,614,461);
    			f.setVisible(true);
    
    			this.setVisible(true);
    			//this.validate();
    		}
    		//will be for changing the image for a given layer 
    		public void setLayerImage(int layer, URL image){
    
    		}
    	}
    }
    Last edited by Dustin T; 09-20-2011 at 04:45 AM.

Similar Threads

  1. Replies: 1
    Last Post: 05-30-2011, 09:37 PM
  2. JLayeredPane not displaying components
    By abc in forum AWT / Swing
    Replies: 3
    Last Post: 01-06-2011, 03:40 PM
  3. How to add JScrolPane to a JLayeredPane?
    By chyrl in forum Advanced Java
    Replies: 5
    Last Post: 08-30-2010, 04:28 AM
  4. JApplet on MAC
    By ld_pvl in forum Java Applets
    Replies: 0
    Last Post: 08-23-2009, 01:40 PM
  5. JFrame to JApplet or JApplet to JApplet
    By ramesh.8189 in forum AWT / Swing
    Replies: 13
    Last Post: 02-08-2009, 06:14 AM

Tags for this Thread

Posting Permissions

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