0

enter image description here

I know how to create a translucent window but I included that just for the sake of completeness.
Now here is what I am trying to do....

Create an undecorated JFrame with 60% opacity

Soft window edge.

Referring to the image above, you can see that the edges are sharp and well-defined. I want to make them soft

Give it a rounded-rectangle shape.

I can give a shape using AWTUtilities.setWindowShape(Window,Shape) but I was wondering how I can create a rounded rectangle.

Create a reflection of the `BufferedImage` to be used as background

Why don't you use Photoshop? you may ask but it is tedious to create a reflection of every image that u wanna try to use as a background. Instead I was wondering if there is a programmatic way to:

  • Take a BufferedImage
  • Flip it vertically. In other words, upside down.
  • change its opacity to a desired value
  • set size of JFrame to be twice of the original buffered image
  • Help!!!


    import java.awt.*;
    import java.awt.geom.AffineTransform;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    public class ImageReflection extends JFrame{
        public ImageReflection(){
            ImageIcon baseIcon = new ImageIcon("src/images/mentalist-logo.png");
            ImageIcon reflectIcon = new ImageIcon("src/images/mentalist-logo.png");
            JLabel baseLabel = new JLabel(baseIcon);
            JLabel reflectLabel = new JLabel();
    
            Graphics2D g2D = (Graphics2D) reflectIcon.getImage().getGraphics();
            g2D.rotate(180);
            reflectLabel.setIcon(reflectIcon);
    
    
            this.add(reflectLabel);
            this.setVisible(true);
            this.pack();
    
        }
        public static void main(String[] args) {
            new ImageReflection();
        }
    }
    

    I get an UnsupportedOperationException at Graphics2D g2D = (Graphics2D) reflectIcon.getImage().getGraphics();.
    This is my code to turn the image upside down.

    An SO User
    • 24,612
    • 35
    • 133
    • 221
    • I have solved always with JWindow, it was easier than with JFrame. –  Dec 27 '12 at 12:49
    • where is your code you tried? writing in big fonts won't help you much I guess – vishal_aim Dec 27 '12 at 12:50
    • @vishal_aim I said, I can only change the opacity. – An SO User Dec 27 '12 at 12:51
    • 1
      you can try with `JWindow` and `( (JComponent) initWindow.getContentPane ( ) ).setBorder(new CompoundBorder(..))` and `AWTUtilities.setWindowOpaque ( initWindow, false );` I created my custom rounded border and used here – vishal_aim Dec 27 '12 at 13:02
    • 2
      @vishal_aim if you have a possible solution, please post it as an answer. It makes it easier for folks to find later. – Sean Connolly Dec 27 '12 at 13:16
    • The question is how to create the GUI as I stated. I could fix a few of them . WHat is left is creating a reflection – An SO User Dec 27 '12 at 15:31
    • 1
      there is transparency (4th. paramaters for Color) and translucency, have to accepting those two f*** differiences – mKorbel Dec 27 '12 at 15:37

    2 Answers2

    3

    What is left is creating a reflection.

    You can alter the graphics context's AffineTransform like this:

    BufferedImage newImage = new BufferedImage(
        oldImage.getWidth(), oldImage.getHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = newImage.createGraphics();
    g2d.translate(0, newImage.getHeight());
    g2d.scale(1, -1);
    g2d.drawImage(oldImage, 0, 0, null);
    g2d.dispose();
    
    trashgod
    • 203,806
    • 29
    • 246
    • 1,045
    • Adding my code to the question, check it out. I get an exception. – An SO User Dec 27 '12 at 21:57
    • Right; `getGraphics()` is only valid for images created with `createImage(w, h)`; use `ImageIO.read()` to get `oldImage ` as a `BufferedImage`, for [example](http://stackoverflow.com/a/5129757/230513). – trashgod Dec 27 '12 at 22:27
    2

    possibly you can try something like this (may be of some help if not all), this is what I tried earlier:

    final JWindow initWindow = new JWindow();
    SwingWorker<Object, Object> task = new SwingWorker<Object, Object>()
                {
            public Object doInBackground() 
            {
                //show init window
                //....
                initWindow.getContentPane().add(text);
    
               ( (JComponent) initWindow.getContentPane ( ) ).setBorder(new CompoundBorder(new CustomRoundedBorder(), new LineBorder(new Color(...)));
    
               AWTUtilities.setWindowOpaque ( initWindow, false );
    
               initWindow.setAlwaysOnTop(true);
    
               initWindow.setVisible(true);
                 //....
                return null;
            }
            public void done() 
            {
            }
        };
        task.execute();
    
    vishal_aim
    • 7,636
    • 1
    • 20
    • 23