Results 1 to 6 of 6
- 01-06-2011, 06:47 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 55
- Rep Power
- 0
Transparant ImgIcon at background
Hello all!
I've a JPanel with a background. I want to add some transparants pictures on it (to start: a tree). The picture is a PNG, and I'm sure it is a transparant picture. Altough it still draws it as not-transparant:
JPanel of background:
And here the part of the tree: (note: the first three lines of 'SpelElement' I don't use anymore, but needs to be there, because when I delete it it won't show the tree at all...)Java Code:public LevelPaneel() { setLayout(null); addMouseListener(this); addMouseMotionListener(this); backgr = new ImageIcon("pics/achtergrond.png"); background = backgr.getImage(); } ... public void paintComponent(Graphics g) { int width = getWidth(); int height = getHeight(); g.drawImage(background, 0, 0, this.getWidth(),this.getHeight(), this); }
Hope ya could help me out!Java Code:public class SpelElement extends JPanel { protected int breedte = 120, hoogte = 145; protected String tekst; protected Color kleur; protected ImageIcon boom, muur; public SpelElement(int x, int y) { tekst = "Boom"; kleur = Color.GREEN; setBounds(x - (breedte / 2), y - (hoogte / 2), breedte, hoogte); int MARGE = 0; // afstand vanaf de bovenrand int KNOP_BREEDTE = 120; // breedte in pixels int KNOP_HOOGTE = 145; // breedte in pixels int KNOP_AFSTAND = 0; // verschuiving int X_LOCATIE = 0; // linkerkant van de knop JLabel imgLabel; boom = new ImageIcon("pics/boom.png"); imgLabel = new JLabel(boom); imgLabel.setBounds(X_LOCATIE, KNOP_AFSTAND + MARGE, KNOP_BREEDTE, KNOP_HOOGTE); add(imgLabel); System.out.println(imgLabel); repaint(); }
-
For better help, post a very simple compilable and runnable program along with an image or a link to the image so we can see the problem on our own computers. The simpiler and shorter the code, the better your chances of getting decent help. Much luck!
- 01-06-2011, 07:26 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 55
- Rep Power
- 0
That's the problem... In a simple program I get it to work...
- 01-07-2011, 01:44 AM #4
My GameEngine performs this task simply. Here is the code.
You can also cause the image to become translucent too.
private BufferedImage mbt(BufferedImage image,
final Color color){
BufferedImage dimg = new BufferedImage(image.getWidth(),
image.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = dimg.createGraphics();
g.setComposite(AlphaComposite.Src);
g.drawImage(image, null, 0, 0);
g.dispose();
for(int i = 0; i < dimg.getHeight(); i++) {
for(int j = 0; j < dimg.getWidth(); j++) {
if(dimg.getRGB(j, i) == color.getRGB()) {
dimg.setRGB(j, i, 0x00FFFFFF);
}
}
}
return dimg;
}
private BufferedImage ti(BufferedImage image, float transperancy) {
// Create the image using the
BufferedImage aimg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TRANSLUCENT);
// Get the images graphics
Graphics2D g = aimg.createGraphics();
// Set the Graphics composite to Alpha
g.setComposite(AlphaComposite.getInstance(AlphaCom posite.SRC_OVER, transperancy));
// Draw the LOADED img into the prepared reciver image
g.drawImage(image, null, 0, 0);
// let go of all system resources in this Graphic
g.dispose();
// Return the image
return aimg;
}Last edited by rdjava; 01-07-2011 at 01:56 AM.
- 01-07-2011, 04:30 AM #5
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
This sounds to me like a build issue, where some classes were built using an older class file of SpelElement, and so when you change it, something is going haywire in the JVM. If that's what's going on, it might also be the reason your images are not properly transparent.
Another possibility is that you are not setting 'setOpaque(false)' for whatever component the snipped 'paintComponent' you have in your OP belongs to. If your component is opaque, then it's still going to be visible if you draw an invisible image over it.
- 01-07-2011, 11:57 AM #6
Member
- Join Date
- Dec 2010
- Posts
- 55
- Rep Power
- 0
Similar Threads
-
How to Run a .exe in background???
By jazz2k8 in forum New To JavaReplies: 9Last Post: 01-21-2011, 11:27 AM -
want to run my midlet in background!
By jackofall in forum CLDC and MIDPReplies: 1Last Post: 03-31-2009, 07:14 PM -
Background in JFrame ( GUI).
By Twister03 in forum AWT / SwingReplies: 2Last Post: 03-12-2009, 03:24 AM -
Background image
By leiferouis in forum New To JavaReplies: 9Last Post: 03-08-2009, 05:49 PM -
ImageIcon background
By Yannick in forum New To JavaReplies: 11Last Post: 02-12-2009, 04:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks