Results 1 to 2 of 2
Thread: Images on JPanels with applets?
- 03-23-2010, 03:05 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 43
- Rep Power
- 0
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); } }All I am doing is adding a Panel whos paint component should be overwritten to use a file image.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); } }
Why isn't this working? When I run it, it is just a gray panel and the button is there.
Thank You
- 03-23-2010, 03:41 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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
-
Posistioning Jpanels
By owain1221 in forum Java AppletsReplies: 7Last Post: 11-15-2009, 11:21 PM -
HELP with jPanels
By maverik_vz in forum AWT / SwingReplies: 1Last Post: 03-12-2009, 11:46 AM -
images, panels and applets
By jamesfrize in forum Java AppletsReplies: 3Last Post: 03-20-2008, 04:35 PM -
How to add Images to JPanels?
By Soda in forum New To JavaReplies: 3Last Post: 12-08-2007, 05:54 PM -
Problems to show images in applets
By Felissa in forum Java AppletsReplies: 1Last Post: 07-06-2007, 09:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks