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!!!)
Re: Apply Image as background to JPanel
you should add
Code:
private Image image;
private boolean maintainAspectRatio;
Re: Apply Image as background to JPanel
Quote:
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.
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? :ehh:
:wave:
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
Re: Apply Image as background to JPanel
I found this link. it might be usefull
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... :ehh:
Re: Apply Image as background to JPanel
Did you change the file name?
Re: Apply Image as background to JPanel
uh, no, what filename? :ehh:
I got my class with the constructor working well... :confused:
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...
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.
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!!! :rolleyes:
:wave: