Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Need help with transparency

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with transparency

    I am a complete Java newbie. I'm currently reading some books, but I'd like some help with what I thought would be simple things to do... except that they seem to be very, very hard. I've been googling for hours, but finding nothing that seemed to work.

    Here's what I'm looking to do:
    Image.jpg

    I want to create a borderless window with a background; I want this background's transparency set at full, so that the corners show the image of whatever's underneath, just as in the picture. Then, I want to add an image (or button) to a specific x, y location in the window (which is NOT resizeable, which is my desire) and then have the user able to click the button; button1 does function1, button 2 does function2, etc. But the images (or buttons) must ALSO have transparency, so that you can see the background image.

    I wrote this program in VB.NET already; I'm trying to rewrite it in Java, as I can no longer work in VB... the program is simply taking up too much memory and is far too slow. Java should handle the whole process better.

    Can anyone help me with this?

    Thanks!


  2. #2
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with transparency

    Here's my current code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.io.IOException;
     
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
     
    public class Dominion {
        public static void main(String[] args) throws IOException {
            //final Image image = ImageIO.read(new URL("http://sstatic.net/so/img/logo.png"));
        	final Image image = Toolkit.getDefaultToolkit().createImage("Pictures/Screens/Screen_Main.PNG");
            final JFrame frame = new JFrame();
            frame.add(new ImagePanel(image));
            frame.setUndecorated(true);
            frame.setBackground(new Color(0.0f,0.0f,0.0f,0.0f));
            frame.setSize(600, 400);
     
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
     
            JButton b = new JButton(new ImageIcon("Pictures/Buttons/Button_Buy_1.PNG"));
     
            b.setSize(150, 50);
            b.setLocation(20,30);
            frame.getContentPane().add(b);
        }
    }
     
    @SuppressWarnings("serial")
    class ImagePanel extends JPanel {
        private Image image;
     
        ImagePanel(Image image) {
            this.image = image;
            };
        ;
     
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
               g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
        }
     
    }

    This gives a frameless window with a background image. But the image is not transparent. It also gives a button, but the button has no transparency, and only shows up when you mouse over it, instead of being visible immediately upon loading. (It's in the upper-left corner). Can anyone help me with this?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with transparency

    One possible problem is the frame is being shown before everything is added to it. Try setting it visible after everything is added.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with transparency

    Well, when I do that, it seems as if the entire screen is the button. You see the button, centered in the screen (not placed as I specified), and no background whatsoever - just gray.

    Argh!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with transparency

    You need to study how to use the layout managers to position your components where you want them.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with transparency

    Ok, I've solved 1 problem. I now have the background image with foreground image on it. Here's my code:

    class ImagePanel extends JPanel
      {
        private Image image;
        private Image button1;
     
        ImagePanel(Image image, Image button1)
          {
            this.image = image;
            this.button1 = button1;
          };;
     
        @Override
        public void paintComponent(Graphics g)
          {
            super.paintComponent(g);
               g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
               g.drawImage(button1, 20, 30, 150, 50, this);
          }
      }

    By adding the image to the panel THIS way, it draws the image, with transparency, on the background. Now, to get the background itself transparent...

    --- Update ---

    After a LOT of work, I now have my window, with no border, and the image being drawn on it with transparency. You can now mouse over the button and have it change to its second image, or click on it to open another window. Although the second window is merely a simple, empty JFrame, at least I have the concept working so far. The only thing I have yet to accomplish is getting the background of the JFrame to be transparent.

  7. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with transparency

    Ok, I switched gears entirely. I have an entirely NEW window, which now has a checked listbox on it. The problem is, the background of the window isn't transparent. Can anyone help me with this? Here's the code:

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Point;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.AbstractAction;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.KeyStroke;
    import javax.swing.ListSelectionModel;
     
    @SuppressWarnings({"serial", "rawtypes", "unchecked"})
    public class ListPanel extends JFrame
      {
         public JList KingdomList;
         public JScrollPane scrollPane;
         public Image image;
         public ImageIcon m_image;
     
         public int winc = 0;
         public int hinc = 0;
     
         public ListPanel()
           {
             setUndecorated(true);
             setBackground(new Color(0.0f, 0.0f, 0.0f, 0.0f));
             getContentPane().setLayout(null);
             ((JPanel) getContentPane()).setOpaque(false);
             image = Toolkit.getDefaultToolkit().createImage("Pictures/Screens/Screen_Setup.PNG");
             m_image = new ImageIcon(image);
             JPanel backPanel = new JPanel();
     
             backPanel.setOpaque(false);
             Object[] items = new CheckListItem[]
               {
                 new CheckListItem("One"),
                 new CheckListItem("Two"),
                 new CheckListItem("Three"),
                 new CheckListItem("Four"),
                 new CheckListItem("Five"),
                 new CheckListItem("Six"),
                 new CheckListItem("Seven"),
                 new CheckListItem("Eight"),
                 new CheckListItem("Nine"),
                 new CheckListItem("Ten"),
                 new CheckListItem("Eleven"),
                 new CheckListItem("Twelve"),
                 new CheckListItem("Thirteen"),
                 new CheckListItem("Fourteen"),
                 new CheckListItem("Fifteen"),
                 new CheckListItem("Sixteen"),
                 new CheckListItem("Seventeen"),
                 new CheckListItem("Eighteen"),
                 new CheckListItem("Nineteen"),
                 new CheckListItem("Twenty"),
               };
     
             KingdomList = new JList(items);
             KingdomList.setOpaque(false);
             KingdomList.setBackground(new Color(1.0f, 1.0f, 1.0f, 1.0f));
             KingdomList.setFont(new Font("Arial", Font.BOLD, 16));
             KingdomList.setCellRenderer(new CheckBoxListRenderer());
             KingdomList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
             KingdomList.setVisibleRowCount(5);
             KingdomList.addMouseListener(new MouseAdapter()
               {
                 @Override
                 public void mouseClicked(MouseEvent event)
                   {
                     selectItem(event.getPoint());
                   }
               });
     
             KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0);
             Object mapKey = keyStroke.toString();
             KingdomList.getInputMap().put(keyStroke, mapKey);
             KingdomList.getActionMap().put(mapKey, new AbstractAction()
               {
                 public void actionPerformed(ActionEvent event)
                   {
                     toggleSelectedItem();
                   }
               });
     
             scrollPane = new JScrollPane(KingdomList);
             scrollPane.setSize(new Dimension(451, 301));
             scrollPane.setLocation(51, 230);
             scrollPane.setOpaque(false);
             scrollPane.getViewport().setOpaque(false);
     
             getContentPane().add(scrollPane);
             backPanel= new JPanel()
               {
                 public void paintComponent(Graphics g)
                   {
                      super.paintComponent(g);
                      winc = m_image.getIconWidth();
                      hinc = m_image.getIconHeight();
                      int w = getParent().getWidth();
                      int h = getParent().getHeight();
     
                      for (int i=0;i<h+hinc;i=i+hinc)
                        {
                           for (int j=0;j<w+winc;j=j+winc)
                             {
                                m_image.paintIcon(this,g,j,i);
                             }
                        }
                   }
                };
     
             getLayeredPane().add( backPanel, new Integer( Integer.MIN_VALUE ) );
             backPanel.setBounds(0, 0, 500, 500);
             backPanel.setSize(855, 640);
             setVisible(true);
           }
     
         public static void main(String[] args)
           {
             ListPanel w = new ListPanel();
             Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
             w.setBounds(dim.width/2-427, dim.height/2-320, 855, 640);
             w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           }
     
         private void selectItem(Point point)
           {
             int index = KingdomList.locationToIndex(point);
     
             if (index >= 0)
               {
                 CheckListItem item = (CheckListItem)KingdomList.getModel().getElementAt(index);
                 item.setSelected(!item.isSelected());
                 KingdomList.repaint(KingdomList.getCellBounds(index, index));
               }
           }
     
         private void toggleSelectedItem()
           {
             int index = KingdomList.getSelectedIndex();
     
             if (index >= 0)
               {
                 CheckListItem item = (CheckListItem)KingdomList.getModel().getElementAt(index);
                 item.setSelected(!item.isSelected());
                 KingdomList.repaint(KingdomList.getCellBounds(index, index));
               }
           }
      }
    ----
    class CheckListItem
      {
        private Object item;
        private boolean selected;
     
        public CheckListItem(Object item)
          {
            this.item = item;
          }
     
        public Object getItem()
          {
            return item;
          }
     
        public boolean isSelected()
          {
            return selected;
          }
     
        public void setSelected(boolean isSelected)
          {
            this.selected = isSelected;
          }
     
        @Override
        public String toString()
          {
            return item.toString();
          }
      }
    ----
    import java.awt.Component;
    import javax.swing.JCheckBox;
    import javax.swing.JList;
    import javax.swing.ListCellRenderer;
     
    @SuppressWarnings({ "rawtypes", "serial" })
    class CheckBoxListRenderer extends JCheckBox  implements ListCellRenderer
      {
        public Component getListCellRendererComponent(JList comp, Object value, int index, boolean isSelected, boolean hasFocus)
          {
            setEnabled(comp.isEnabled());
            setSelected(((CheckListItem) value).isSelected());
            setFont(comp.getFont());
            setText(value.toString());
     
            if (isSelected)
              {
                setBackground(comp.getSelectionBackground());
                setForeground(comp.getSelectionForeground());
              }
            else
              {
                setBackground(comp.getBackground());
                setForeground(comp.getForeground());
              }
     
            return this;
          }
      }

Similar Threads

  1. Transparency to bufferedImage
    By Serthy in forum AWT / Java Swing
    Replies: 21
    Last Post: August 29th, 2013, 05:11 AM
  2. Resizing an image without losing transparency
    By beansnbacon in forum AWT / Java Swing
    Replies: 1
    Last Post: July 31st, 2013, 07:59 AM
  3. Setting a transparency level of a picture issue
    By Asido in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 18th, 2012, 05:42 AM
  4. Replies: 3
    Last Post: July 17th, 2012, 11:59 PM
  5. Image transparency issue
    By Brt93yoda in forum Java Theory & Questions
    Replies: 6
    Last Post: August 31st, 2010, 02:25 PM