• Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting an image as a background of a JFrame  RSS feed

 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am trying to have a jpg show up as the background of a JFrame, the user can then click on parts of that image and depending on the mouse position methods are called.

I have tried to add the background by extending JFrame:



The frame is created with



mainFrame is created and works fine, I can add components and whatnot - but the background is grey as ever. When I change the size of the frame (I mean by draging the corner of the frame, not within the code) the background flickers a bit and I can see that the jpg is there, it just doesn't show. I've tried setting the background to null, but that just makes MyFrame use the default background inherited from JFrame - so nothing happens.

I would appreciate your help.

Martin
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your problem is probably caused by the contentPane

easieast way is to use a JPanel and add that to your frame
there is an example in the Swing FAQ (main page)
 
Rob Spoor
Sheriff
Posts: 20897
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
super.paint() clears your contents, you call that first, then draw the image.

Also, you should load the image once (in the constructor), then use the loaded image. Reloading the image every time will slow down your painting.
 
jack wenttohill
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This will solve your problem and use mouse listener to fire event on coord..
 
Rob Spoor
Sheriff
Posts: 20897
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack,

I really urge you to follow the instructions in the private message Amit sent you. It is not optional, and failure to comply will lead to the closing of your account.

Other than that, you're missing calls to super.paint(g) and super.update(g); omitting these will mess up your user interface with older drawings remaining. And that's not just the image, but also the border.
 
salvin francis
Bartender
Posts: 1479
21
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont see the need to use the paint method to draw image here.
though the OP mentions background, it seems he would probably need a label containing an image with click listener added to it.

personally i would prefer this approach, you can add this to a scroll panel if the image is too large,
this way the user can scroll to other parts of the image and click.


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!