Results 1 to 11 of 11
  1. #1
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Question How to make a image transparent in JLabel and put on top of another image

    Simply i want to make a image transparent and to place it on top of another
    Here's the code:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class gameWorld extends JFrame {
    
    	public static void main(String[] args) {
    		new gameWorld();
    		
    	}
    	gameWorld() {
    		setLayout(new BorderLayout());
    		JLabel jlbConsole = new JLabel("You have spawned!");				
    		ImageIcon worldMap = new ImageIcon("MyCoreWorld.jpg");
    		JLabel jlbWorld = new JLabel(worldMap);
    		ImageIcon player = new ImageIcon("MCOlongneckRace.png");
    		JLabel jlbPlayer = new JLabel(player);
    		add(jlbPlayer, BorderLayout.SOUTH);
    		add(jlbConsole, BorderLayout.NORTH);
    		add(jlbWorld, BorderLayout.SOUTH);
    		this.setSize(700, 700);
    		// pack();
    		setVisible(true);
    		
    	}
    
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,985
    Rep Power
    9

    Default Re: How to make a image transparent in JLabel and put on top of another image

    Is this a question?

    By the way, you should use standard naming conventions and make sure your class names start with an upper-case letter.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: How to make a image transparent in JLabel and put on top of another image

    i'm pretty sure it's a question and can i change the class names?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,985
    Rep Power
    9

    Default Re: How to make a image transparent in JLabel and put on top of another image

    It's your class name (gameWorld), so you can absolutely change it.

    The reason I'm confused about whether this is a question is that your post doesn't actually ask a question or talk about what's confusing you. I thought it was meant to be an (incomplete) tutorial instead of a question.

    To show two images on top of one another, you can either do custom painting and paint the images wherever you want, or you can use a null layout to place the JLabels themselves.

    I personally prefer the custom painting approach, but it's up to you and your context.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: How to make a image transparent in JLabel and put on top of another image

    what does the custon painting mean exactly?

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,079
    Rep Power
    6

    Default Re: How to make a image transparent in JLabel and put on top of another image

    google "java custom painting" and see for yourself.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,985
    Rep Power
    9

    Default Re: How to make a image transparent in JLabel and put on top of another image

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: How to make a image transparent in JLabel and put on top of another image

    Quote Originally Posted by KevinWorkman View Post
    It's your class name (gameWorld), so you can absolutely change it.

    The reason I'm confused about whether this is a question is that your post doesn't actually ask a question or talk about what's confusing you. I thought it was meant to be an (incomplete) tutorial instead of a question.

    To show two images on top of one another, you can either do custom painting and paint the images wherever you want, or you can use a null layout to place the JLabels themselves.

    I personally prefer the custom painting approach, but it's up to you and your context.
    Okay i want to use the null layout thing because the custom painting made me really confused. So any advice to that?

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,771
    Rep Power
    5

    Default Re: How to make a image transparent in JLabel and put on top of another image

    Actually, I'm sort of confused by your requirement. You want to place a transparent image on top of another (supposedly non-transparent) background image. I have several questions. How much of the first image is transparent? How big will the JLabels be? Are you going to move the JLabels around?

    If this is for a game, then I suggest you not move components around a null layout by changing their position but using painting to redraw the field and other game components to reflect their new position.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: How to make a image transparent in JLabel and put on top of another image

    Okay i want to make the background (the white area) of this image:
    How to make a  image transparent in JLabel and put on top of another image-mcolongneckrace.png



    And yes i'm makign a game where i will use this but the custom painting seems too hard for me right now

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,985
    Rep Power
    9

    Default Re: How to make a image transparent in JLabel and put on top of another image

    If you're making a game, then custom painting is definitely the way to go.

    But honestly, if you're too confused to ask a question, you might want to start with something smaller.

    I've written a tutorial on custom painting here: Custom Painting - Tutorials - Static Void Games
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Transparent Image flickers to none transparent image
    By totalspelnerd in forum Java 2D
    Replies: 3
    Last Post: 10-30-2013, 04:06 PM
  2. Image On a JLabel
    By corbokhan in forum New To Java
    Replies: 11
    Last Post: 11-06-2010, 12:18 AM
  3. Image in JLabel
    By ishere11 in forum AWT / Swing
    Replies: 2
    Last Post: 04-29-2010, 08:02 AM
  4. Making an Image transparent?
    By audinue in forum Java 2D
    Replies: 3
    Last Post: 01-03-2009, 07:32 PM
  5. Replies: 1
    Last Post: 07-17-2007, 04:10 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
  •