|
-
May 25th, 2006, 07:53 PM
#1
Thread Starter
Fanatic Member
Apply Image as background to JPanel [RESOLVED]
How can I apply an image as a background to a JPanel? I can do this fine with the graphic object, but it's drawing is temporary... so I thought of getting to do a new class extending JPanel...
what I got so far is this:
Code:
package rukupaint;
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class clsPaintPanel extends JPanel {
/** Creates a new instance of clsPaintPanel */
public clsPaintPanel() {
}
protected void paintComponent (Graphics g) {
super.paintComponent (g);
if (this.image != null) {
Graphics2D g2d = (Graphics2D) g;
int iw = this.image.getWidth(this);
int ih = this.image.getHeight(this);
int w = iw != 0 ? iw : 1;
int h = ih != 0 ? ih : 1;
double sx = ((double) getWidth()) / ((double) w);
double sy = ((double) getHeight()) / ((double) h);
AffineTransform at = new AffineTransform ();
if (this.maintainAspectRatio) {
sx = sy = Math.max(sx, sy);
}
at.scale(sx, sy);
g2d.drawImage(this.image, at, this);
}
}
}
I've seen this on another forum, I do not claim the code to be mine, I just need to understand how to logically solve my problem...
But there doesn't seem to be an "image" property to the JPanel...
help on this would be appreciated!!! 
(By the way if there is some object I havn't seen that runs a background picture property, don't hesitate sharing the class/object, thx!!!)
Last edited by Ruku; May 31st, 2006 at 03:49 AM.
-
May 26th, 2006, 03:20 AM
#2
Re: Apply Image as background to JPanel
you should add
Code:
private Image image;
private boolean maintainAspectRatio;
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
May 26th, 2006, 05:11 AM
#3
Frenzied Member
Re: Apply Image as background to JPanel
 Originally Posted by Ruku
How can I apply an image as a background to a JPanel? I can do this fine with the graphic object, but it's drawing is temporary... so I thought of getting to do a new class extending JPanel...
what I got so far is this:
Code:
package rukupaint;
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class clsPaintPanel extends JPanel {
/** Creates a new instance of clsPaintPanel */
public clsPaintPanel() {
}
protected void paintComponent (Graphics g) {
super.paintComponent (g);
if (this.image != null) {
Graphics2D g2d = (Graphics2D) g;
int iw = this.image.getWidth(this);
int ih = this.image.getHeight(this);
int w = iw != 0 ? iw : 1;
int h = ih != 0 ? ih : 1;
double sx = ((double) getWidth()) / ((double) w);
double sy = ((double) getHeight()) / ((double) h);
AffineTransform at = new AffineTransform ();
if (this.maintainAspectRatio) {
sx = sy = Math.max(sx, sy);
}
at.scale(sx, sy);
g2d.drawImage(this.image, at, this);
}
}
}
I've seen this on another forum, I do not claim the code to be mine, I just need to understand how to logically solve my problem...
But there doesn't seem to be an "image" property to the JPanel...
help on this would be appreciated!!!
(By the way if there is some object I havn't seen that runs a background picture property, don't hesitate sharing the class/object, thx!!!)
You paint the image... Simply add this class and use it in your other class.
-
May 29th, 2006, 06:29 PM
#4
Thread Starter
Fanatic Member
Re: Apply Image as background to JPanel
Still can't solve this, sorry if I've been unclear... What I want is just to draw an image that sticks to the object... such as .net's picturebox...
since there is no picturebox in Java I thought of using a jPanel, but when I move a window over what I've drawned the image disapears!
So how can I do this sticky image thing? 

To ComputerJy: My class is working, yeah I forgot some declarations in the code i've pasted...!!
To System_Error: I did, but what happens is that all my image gets white and nothing is drawned... o_O
Last edited by Ruku; May 29th, 2006 at 07:22 PM.
-
May 29th, 2006, 07:21 PM
#5
Re: Apply Image as background to JPanel
I found this link. it might be usefull
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
May 29th, 2006, 07:55 PM
#6
Thread Starter
Fanatic Member
Re: Apply Image as background to JPanel
thx for the link... but I don't understand what exacly makes the image sticky within the paintComponent(Graphics g) sub.... o_O
(Graphics2D g2d = (Graphics2D) g
looks like the 2 most important lines are:
super.paintComponent(g);
g2d.drawImage(this.image, at, this);
buuut it doesn't quite work... nothing gets drawned...
-
May 30th, 2006, 12:47 AM
#7
Re: Apply Image as background to JPanel
Did you change the file name?
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
May 30th, 2006, 01:59 PM
#8
Thread Starter
Fanatic Member
Re: Apply Image as background to JPanel
uh, no, what filename?
I got my class with the constructor working well... 
the only thing I didn't get is that ImageFill class within the link you gave me... can't find it... and I've imported everythin' they did...
Last edited by Ruku; May 30th, 2006 at 02:12 PM.
-
May 30th, 2006, 04:12 PM
#9
Re: Apply Image as background to JPanel
In their code, the image is captured from a file "background.jpg" in the constructor, make sure you change that file to a valid image file.
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
May 30th, 2006, 08:41 PM
#10
Thread Starter
Fanatic Member
Re: Apply Image as background to JPanel
Code:
protected void paintComponent(Graphics g) {
try {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
catch (Exception ex) {
}
}
got everything working...!
I just needed to call the repaint(); method and everything was good!!! 
Last edited by Ruku; May 31st, 2006 at 03:49 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|