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
    Posts
    105
    Rep Power
    0

    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;
    //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;
    import java.io.File;
    import java.io.IOException;
    
    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 = ImageIO.read(new File("linetest.png"));
    		//pane = new Container();
    		jframe = new JFrame();
    		jframe.setSize(new Dimension(1320, 680));
    		jframe.setResizable(false);
    		jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		jframe.setVisible(true);
    		jframe.add(this);
    		pane = jframe.getContentPane();
    		//setOpaque(true);
    		pane.setLayout(null);
    		//pane.setBackground(new Color(0, 0, 0, 255));
    		button = new JButton();
    		button.setLayout(null);
    		button.setBounds(20, 20, 50, 50);
    		//pane.add(button);
    		//pane.s
    		jframe.add(button);
    		//jframe.add(pane);
    		}
    
    	@Override
    	public void paint(Graphics g) {
    		super.paint(g);
    		g2d = (Graphics2D) g;
    		g2d.drawImage(linetest, new AffineTransform(), this);
    		repaint();
    	}
    
    	/**
    	 * @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
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    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.
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.add(this);
          // set the size of the JPanel, not the frame
          setPreferredSize(new Dimension(width, height));
          // validates the frame and sizes the components.
          frame.pack();
          // centers the frame on the screen
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
        public void paintComponent(Graphics g) {
              super.paintComponent(g);
              // other stuff here
         }
    }
    Regards,
    Jim
    Last edited by jim829; 01-02-2014 at 06: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, 05: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, 05:59 AM
  3. transparent
    By chathura992 in forum NetBeans
    Replies: 2
    Last Post: 12-30-2010, 05:03 PM
  4. transparent jframe?
    By majkel in forum AWT / Swing
    Replies: 12
    Last Post: 10-09-2010, 06:06 PM
  5. Replies: 3
    Last Post: 04-22-2009, 12:01 AM

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
  •