Java Swing

2018-01-09 19:23 更新

Java Swing教程 - Java Swing


半透明窗口

我們可以通過使用中定義的枚舉來檢查是否支持透明窗口WindowTranslucency。

  • PERPIXEL_TRANSPARENT: A pixel in a window is either opaque or transparent. That is, the alpha value for a pixel is either 0.0 or 1.0.
  • TRANSLUCENT: all pixels in a window have the same translucency, which can be defined by an alpha value between 0.0 and 1.0.
  • PERPIXEL_TRANSLUCENT: each pixel in a window can have its own alpha value between 0.0 and 1.0. It lets we define the translucency in a window on a per pixel basis.

并非所有平臺都支持所有三種半透明形式。

以下代碼顯示了如何檢查平臺上的半透明支持。

import static java.awt.GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT;
import static java.awt.GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT;
import static java.awt.GraphicsDevice.WindowTranslucency.TRANSLUCENT;
//from w w  w .  ja v a  2s  .c  o m
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;

public class Main {
  public static void main(String[] args) {
    GraphicsEnvironment graphicsEnv = GraphicsEnvironment
        .getLocalGraphicsEnvironment();

    GraphicsDevice graphicsDevice = graphicsEnv.getDefaultScreenDevice();

    boolean isSupported = graphicsDevice
        .isWindowTranslucencySupported(PERPIXEL_TRANSPARENT);
    System.out.println("PERPIXEL_TRANSPARENT  supported: " + isSupported);

    isSupported = graphicsDevice.isWindowTranslucencySupported(TRANSLUCENT);
    System.out.println("TRANSLUCENT  supported: " + isSupported);

    isSupported = graphicsDevice
        .isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);
    System.out.println("PERPIXEL_TRANSLUCENT  supported: " + isSupported);
  }
}

上面的代碼生成以下結(jié)果。



透明窗口

以下代碼顯示如何創(chuàng)建一個透明窗口。

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Main extends JFrame {
  public Main() {
    super();
    JButton closeButton = new JButton("Close");
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setUndecorated(true);
    this.setOpacity(0.80f);
    this.setSize(200, 200);
    // Center it on the screen
    this.setLocationRelativeTo(null);
    this.add(closeButton, BorderLayout.SOUTH);
    closeButton.addActionListener(e -> System.exit(0));
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
      Main frame = new Main();
      frame.setVisible(true);
    });
  }
}


漸變窗口

以下代碼使用JPanel創(chuàng)建從左邊緣的不透明到在右邊緣逐漸變透明的漸變效果。

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Paint;
//  w w w.  j ava  2 s.com
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Main extends JFrame {
  public Main() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    // Make sure the frame is undecorated
    this.setUndecorated(true);
    this.setBackground(new Color(0, 0, 0, 0));
    this.setSize(200, 200);
    // Center it on the screen
    this.setLocationRelativeTo(null);

    this.getContentPane().setLayout(new GridLayout(0, 1));
    this.add(new TranslucentJPanel(Color.RED));
    JButton closeButton = new JButton("Close");
    this.add(closeButton);
    closeButton.addActionListener(e -> System.exit(0));
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
      Main frame = new Main();
      frame.setVisible(true);
    });
  }
}

class TranslucentJPanel extends JPanel {
  private int red = 240;
  private int green = 240;
  private int blue = 240;

  public TranslucentJPanel(Color bgColor) {
    this.red = bgColor.getRed();
    this.green = bgColor.getGreen();
    this.blue = bgColor.getBlue();
  }

  @Override
  protected void paintComponent(Graphics g) {
    int width = this.getWidth();
    int height = this.getHeight();
    float startPointX = 0.0f;
    float startPointY = 0.0f;
    float endPointX = width;
    float endPointY = 0.0f;
    Color startColor = new Color(red, green, blue, 255);
    Color endColor = new Color(red, green, blue, 0);

    Paint paint = new GradientPaint(startPointX, startPointY, startColor,
        endPointX, endPointY, endColor);

    Graphics2D g2D = (Graphics2D) g;
    g2D.setPaint(paint);
    g2D.fillRect(0, 0, width, height);

  }
}

形窗口

我們可以使用Swing通過使用Window類的setShape(Shape s)方法創(chuàng)建一個自定義形狀的窗口。

使用成形窗口必須滿足以下三個條件:

  • The platform must support PERPIXEL_TRANSPARENT translucency.
  • The window must be undecorated. we can make a JFrame or JDialog undecorated by calling the setUndecorated(false) method on them.
  • The window must not be in full-screen mode.
import java.awt.BorderLayout;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
/*from   w  w  w  .  j av  a 2 s. co m*/
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Main extends JFrame {
  public Main() {
    this.setUndecorated(true);

    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setSize(200, 200);

    Ellipse2D.Double ellipse = new Ellipse2D.Double(0, 0, 200, 100);
    Rectangle2D.Double rect = new Rectangle2D.Double(0, 100, 200, 200);

    Path2D path = new Path2D.Double();
    path.append(rect, true);
    path.append(ellipse, true);
    this.setShape(path);
    JButton closeButton = new JButton("Close");
    this.add(closeButton, BorderLayout.SOUTH);
    closeButton.addActionListener(e -> System.exit(0));
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
        Main frame = new Main();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      });
  }
}
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號