Results 1 to 14 of 14
- 08-23-2009, 11:12 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
- 08-24-2009, 03:28 AM #2
You can override the paintComponent of the JDesktopPane (i think) if not you can attach a JPanel to the JDesktopPane instead:
Java Code:private Image backImage = null; //member variable ... //in the constructor backImage = new ImageIcon(this.getClass().getResource("backImage.jpg")).getImage();
Java Code:public void paintComponent( Graphics g ) { if(backImage == null) super.paintComponent(g); else { Graphics2D g2d = (Graphics2D)g; //scale the image to fit the size of the Panel double mw = backImage.getWidth(null); double mh = backImage.getHeight(null); double sw = getWidth() / mw; double sh = getHeight() / mh; g2d.scale(sw, sh); g2d.drawImage(backImage, 0, 0, this); } }
- 08-24-2009, 01:57 PM #3
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
it didn't work!!..wt did u mean about "attach a JPanel to the JDesktopPane instead"??
-
Please tell us more here. What didn't work? Did you get a compilation error? Throw an exception? Not see an image?
I think that your best bet here is to post some compilable code. Don't post your entire program but instead just create a simple program that has a JDesktopPane that uses an image that we all can use (using an image off the internet would probably work best), that has no extraneous code, but that demonstrates your problem, a Short, Self Contained, Correct (Compilable), Example or "SSCCE". If you can post this, I can almost guarantee that you'll be able to get "helpful" help soon.
Best of luck.
- 08-24-2009, 09:50 PM #5
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
wt didn't work is that the image didn't show!
the code is the same that was wrote up there!
- 08-24-2009, 10:25 PM #6
It works fine for me:
Java Code:public class myDesktopPane extends javax.swing.JDesktopPane { private Image backImage = null; //member variable public myDesktopPane() { try { backImage = new javax.swing.ImageIcon(this.getClass().getResource("backImage.jpg")).getImage(); } catch(Exception e) { System.out.println("Could not find file in folder: " + this.getClass().getResource(".")); } } public void paintComponent( Graphics g ) { if(backImage == null) super.paintComponent(g); else { Graphics2D g2d = (Graphics2D)g; //scale the image to fit the size of the Panel double mw = backImage.getWidth(null); double mh = backImage.getHeight(null); double sw = getWidth() / mw; double sh = getHeight() / mh; g2d.scale(sw, sh); g2d.drawImage(backImage, 0, 0, this); } } }My Hobby Project: LegacyClone
- 08-24-2009, 11:04 PM #7
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
and how I call it from any method?..cuz I have the graphics! I don't know how to use graphics!!
-
- 08-24-2009, 11:38 PM #9
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
this class how can I call it from the main?!
is this right?...and instead of null wt should I write?Java Code:MyDesktopPane j=new MyDesktopPane(); j.paintComponent(null);
-
You don't call paintComponent directly, and in fact almost never do so. Instead it is called by the JVM when necessary such as when it is drawing your GUI. You can suggest to the JVM when it should call this by calling repaint() on the JPanel. For instance if you were doing an animation, you'd call repaint each time you changed the position of something that you were drawing. You will learn all this and more if you go through the tutorials which I suggest you do without delay.
- 08-25-2009, 12:16 AM #11
Wow, that is like saying "hey, i have a cement mixer outside... how to do i build a house." I would suggest you start from the basics and through the tutorials as Fubarable suggested. :)
Last edited by mrmatt1111; 08-25-2009 at 12:19 AM.
My Hobby Project: LegacyClone
- 08-25-2009, 12:23 AM #12
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
ok I will...thnx alot u really helped me ;)
-
Hey mr matt, took a look at your legacy clone project code... pretty neat stuff. thanks for sharing the link in your signature.
- 08-25-2009, 01:10 AM #14
Similar Threads
-
Background Image of Applet
By BangJava in forum Java AppletsReplies: 8Last Post: 11-24-2010, 05:48 AM -
How to set the background of binary image?
By hyz_zsu in forum Java 2DReplies: 1Last Post: 04-09-2009, 03:14 AM -
Background image
By leiferouis in forum New To JavaReplies: 9Last Post: 03-08-2009, 05:49 PM -
how i can put image in background of frame??
By ahmed13 in forum NetBeansReplies: 4Last Post: 01-15-2009, 05:47 PM -
Image as background
By Java.child in forum AWT / SwingReplies: 2Last Post: 10-02-2008, 11:37 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks