Java creates transparent and any shape windows

Source: Internet
Author: User

Java creates transparent and any shape windows

I. Basic knowledge of Java 2D graphics

Since the release of Java swing, Java's graphics capabilities have been greatly improved. The advanced version of JDK 6 began to support the creation

Jframe with custom shapes can be created based on Java 2D image APIs. When

Java 2 provides the following basic shapes:

 

Currently, Java provides major operations for 2D graphics, such as cropping and path coverage. Java 2D and swing

The relevant APIs can be used to create a jframe with the desired shape.

 

Ii. API supports transparency and custom shapes

From release 10 of JDK 6, you can set transparent and custom shapes. Supported objects include swing jframe, jdialog,

AWT frame. And inherit the subclass of Java. AWT. Window.

 

Windows has two types of transparent effects: simple transparent effects and pixel-based transparent effects. Simple and transparent

Use Alpha to set all pixels. The smaller the value, the more transparent the minimum value is. The maximum value indicates that the value is completely opaque.

(Opaque) in windows, pixel-based transparency needs to be enabled through the following API:

AWTUtilities.setWindowOpaque (frame, false);

 

The transparency and shape settings are all called through the Java reflection mechanism. An awtutilitieswrappe class is a good example on the official JDK website.

 

3. A jframe that can change its shape and transparency

The main function of the program is to select the desired shape, and then click the OK button, the application window changes accordingly

Click the [cancel] button to launch the program. Move the mouse over the window and you can drag the window program.

 

Involved swing components include jframe, jpanel, jbutton, and jcombobox.

The absolute positioning method is used for component placement. You need to call setlayout (null) to declare the absolute positioning method.

The Running Effect of the program is as follows:

 

Select circle and click OK. The following figure shows the effect:

Select "area" and click "OK". The effect is as follows:

The main code is jcustomframe.

import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.Shape;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ComponentAdapter;import java.awt.event.ComponentEvent;import java.awt.geom.Area;import java.awt.geom.Ellipse2D;import java.awt.geom.RoundRectangle2D;import java.io.IOException;import java.util.logging.Level;import java.util.logging.Logger;import javax.imageio.ImageIO;import javax.swing.JFrame;import javax.swing.SwingUtilities;import javax.swing.UIManager;public class JCustomFrame extends JFrame {/** * gloomy fish */private static final long serialVersionUID = -523336873755438297L;    private Shape shape;    private float alpha = 1f;    private Dimension arcSize = new Dimension(50, 50);    protected static final int CIRCLE_TYPE = 1;    protected static final int RECTANGEL_TYPE = 0;    protected static final int AREA_TYPE = 2;    public JCustomFrame() {        setUndecorated(true);        setVisible(true);        setListenersForEffects();}public JCustomFrame(int width, int height) {        this();        setSize(width, height);    }    public JCustomFrame(Shape shape, int width, int height) {        this(width, height);        setShape(shape);    }    public void setShape(Shape shape) {this.shape = shape;}public JCustomFrame(float alpha, Shape shape, int width, int height) {        this(shape, width, height);        setAlpha(alpha);    } public void setAlpha(float alpha) {this.alpha = alpha;}private void setListenersForEffects() {        //It is important to upadate visual effect on form resize.        addComponentListener(new ComponentAdapter() {            @Override            public void componentResized(ComponentEvent evt) {                updateFrameEffects();            }        });    }    /**     * This updates visual effects like SHAPE form and transparency. You have to     * update also <b>shape</b> property or it paints old shape ( if you resize     * frame without resize shape .. )     */    public void updateFrameEffects() {        updateShape();        try {            AWTUtilitiesWrapper.setWindowShape(this, shape);            if (shape != null) {                AWTUtilitiesWrapper.setWindowOpacity(this, alpha);            }        } catch (Exception ex) {            Logger.getLogger(JCustomFrame.class.getName()).log(Level.SEVERE, null, ex);        }    }    public void updateShape() {if(shape == null) {shape = new RoundRectangle2D.Double(0d, 0d, getWidth(), getHeight(), arcSize.width, arcSize.height);}}public void updateShape(int type) {if(type == RECTANGEL_TYPE) {shape = new RoundRectangle2D.Double(0d, 0d, getWidth(), getHeight(), arcSize.width, arcSize.height);} else if(type == CIRCLE_TYPE) {shape = new Ellipse2D.Double(0, 0,400, 400);} else if(type == AREA_TYPE) {Shape circle1 = new Ellipse2D.Double(0, 0,400, 400);Shape circle2 = new Ellipse2D.Double(200, 100,400, 400);Area area1 = new Area(circle1);Area area2 = new Area(circle2);area1.subtract(area2);shape = area1;}}public void center() {        Toolkit tk = Toolkit.getDefaultToolkit();        Dimension screenSize = tk.getScreenSize();        int screenHeight = screenSize.height;        int screenWidth = screenSize.width;        this.setLocation((screenWidth - this.getWidth()) / 2, (screenHeight - this.getHeight()) / 2);    }public static void main(String[] args) {SwingUtilities.invokeLater(new Runnable() {            public void run() {                try {                // com.sun.java.swing.plaf.windows.WindowsLookAndFeel                UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");                } catch (Exception e) {                }                /*These are simple custom panel generated with vidual editor of Netbeans                  don't care about it, take a look only to ImagePanel inherit ( why?...)                 */                                // Cool transparent Frame                final JCustomFrame customFrame = new JCustomFrame();                customFrame.setLayout(new BorderLayout());                                // create custom JPanel                final JImagePanel panel = new JImagePanel();                java.net.URL image1 = this.getClass().getResource("ball.jpg");                try {panel.setImage(ImageIO.read(image1));panel.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if(e.getActionCommand().equals("OK")) {System.out.println("Transfer now......");customFrame.updateShape(panel.getSelectedIndex());if(panel.getSelectedIndex() == CIRCLE_TYPE) {customFrame.setSize(400, 400);} else if(panel.getSelectedIndex() == AREA_TYPE) {customFrame.setSize(400, 399); // force layout Manager re-computation} else {customFrame.setSize(400, 300);}} else if(e.getActionCommand().equals("Cancel")) {System.out.println("System Exit......");customFrame.setVisible(false);customFrame.dispose();System.exit(0);}}});} catch (IOException e) {e.printStackTrace();}DragBarHandler dragHandle = new DragBarHandler(customFrame);                customFrame.add(panel, BorderLayout.CENTER);                customFrame.add(dragHandle, BorderLayout.NORTH);                customFrame.setTitle("Ttranslucency JFrame");                customFrame.setSize(400, 300);                customFrame.setAlpha(0.8f);                customFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                customFrame.center();            }        });}}

If you want all the source code, leave email

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.