Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By jim829

Thread: Transparent container

  1. #1
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Rep Power

    Post Transparent container

    Hello I am trying to put a button (in a container) on a jpanel in a jframe with a background but the container is covering up the background with white color.

    Below I have an excellent SCCE:

    Java Code:
    package drawFramePackage;
    // Author: Nathan Nelson
    import javax.imageio.ImageIO;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import java.awt.Dimension;
    import javax.swing.JFrame;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.AffineTransform;
    import java.awt.image.BufferedImage;
    public class RealTimeStrategy extends JPanel {
    	private static final long serialVersionUID = 1L;
    	JFrame jframe;
    	Graphics2D g2d;
    	BufferedImage linetest;
    	JButton button;
    	Container pane;
    	public RealTimeStrategy() throws IOException {
    		linetest = File("linetest.png"));
    		//pane = new Container();
    		jframe = new JFrame();
    		jframe.setSize(new Dimension(1320, 680));
    		pane = jframe.getContentPane();
    		//pane.setBackground(new Color(0, 0, 0, 255));
    		button = new JButton();
    		button.setBounds(20, 20, 50, 50);
    	public void paint(Graphics g) {
    		g2d = (Graphics2D) g;
    		g2d.drawImage(linetest, new AffineTransform(), this);
    	 * @param args
    	 * @throws IOException 
    	public static void main(String[] args) throws IOException {
    		// TODO Auto-generated method stub
    		new RealTimeStrategy();
    Basically I just need to set the containers background to transparent. Any idea / suggestions / experience around this anyone?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Transparent container

    Well this is probably more than you expected (or wanted) but here goes. First, I highly recommend you read up on painting in the Java tutorials in my signature. Here is a link to the painting section. Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing).

    In your program above you are adding the button to the JFrame. You should be adding it to the JPanel. And unless you need to transform the image you can just draw the image with drawImage(image,0,0,null).

    In the interim, I suggest the following:

    1. For a JPanel, don't use paint. Use paintComponent. If you need to use paint, you're probably doing something wrong.
    2. For a JFrame, the add method is a helper method which will add components to the content pane for you.
    3. Don't use absolute positioning. Use layout managers. It will benefit you in the long run.

    Here is a typical setup I use when using JFrames. It''s not sacrosanct and others may do it differently. But it works for me.

    Java Code:
    public class MyClass extends JPanel {
     // in this case, not for outside use
     private MyClass(int width, int height) {
          frame = new JFrame("My Title");
          // let's you click on the red square to exit.
          // set the size of the JPanel, not the frame
          setPreferredSize(new Dimension(width, height));
          // validates the frame and sizes the components.
          // centers the frame on the screen
        public void paintComponent(Graphics g) {
              // other stuff here
    Last edited by jim829; 01-02-2014 at 05:29 AM.
    natdizzle likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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. Help with adding a Container within a Container
    By JoKeR313 in forum New To Java
    Replies: 4
    Last Post: 01-02-2011, 04:59 AM
  3. transparent
    By chathura992 in forum NetBeans
    Replies: 2
    Last Post: 12-30-2010, 04:03 PM
  4. transparent jframe?
    By majkel in forum AWT / Swing
    Replies: 12
    Last Post: 10-09-2010, 05:06 PM
  5. Replies: 3
    Last Post: 04-21-2009, 11:01 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts