Results 1 to 2 of 2
  1. #1
    Krooger is offline Member
    Join Date
    Nov 2008
    Posts
    43
    Rep Power
    0

    Default Images on JPanels with applets?

    Hello, I'm trying to learn how to use applets but the switch from application to applet wasn't what I expected.

    I am trying to run a simple applet that just has a Panel in it with a background from an image file but I cannot seem to get it to work.

    Java Code:
    import javax.swing.JApplet;
    import javax.swing.JButton;
    
    public class DarkWaterReviwebBoot extends JApplet {
    	public void init() {
    		try {
    			javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
    				public void run() {
    					createGUI();
    		        }
    		    });
    		} catch (Exception e) {
    		  System.err.println("createGUI didn't successfully complete");
    		}
    	}
    		
    	private void createGUI() {
    		TexturedPanel titlePanel = new TexturedPanel("images/title.jpg");
    		titlePanel.add(new JButton("TEST"));
    		getContentPane().add(titlePanel);
    	}
    }
    Java Code:
    import java.awt.Dimension;
    import java.awt.Graphics;
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    
    public class TexturedPanel extends JPanel {
    	private ImageIcon bgImage;
    	 
    	public TexturedPanel(String imagePath) {
    		bgImage = new ImageIcon(imagePath);
    	}
    	 
    	public void paintComponent(Graphics g) {
    		Dimension d = getSize();
    		g.drawImage(bgImage.getImage(), 0, 0, d.width, d.height, null);
    		setOpaque(false);
    		super.paintComponent(g);
    	}
    }
    All I am doing is adding a Panel whos paint component should be overwritten to use a file image.

    Why isn't this working? When I run it, it is just a gray panel and the button is there.

    Thank You

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

    Default

    Applets are not supposed to access data directly on you computer since they are meant to be run over the internet.

    Read the secton from the Swing tutorial on How to Make Applets.

Similar Threads

  1. Posistioning Jpanels
    By owain1221 in forum Java Applets
    Replies: 7
    Last Post: 11-15-2009, 11:21 PM
  2. HELP with jPanels
    By maverik_vz in forum AWT / Swing
    Replies: 1
    Last Post: 03-12-2009, 11:46 AM
  3. images, panels and applets
    By jamesfrize in forum Java Applets
    Replies: 3
    Last Post: 03-20-2008, 04:35 PM
  4. How to add Images to JPanels?
    By Soda in forum New To Java
    Replies: 3
    Last Post: 12-08-2007, 05:54 PM
  5. Problems to show images in applets
    By Felissa in forum Java Applets
    Replies: 1
    Last Post: 07-06-2007, 09:12 PM

Posting Permissions

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