Java/2D Graphics GUI/Transparent

Материал из Java эксперт
Перейти к: навигация, поиск

Determining If an Image Has Transparent Pixels

  
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;
import javax.swing.ImageIcon;
public class Main {
  public static void main(String[] argv) throws Exception {
    Image image = new ImageIcon("a.png").getImage();
    if (image instanceof BufferedImage) {
      BufferedImage bimage = (BufferedImage) image;
      System.out.println(bimage.getColorModel().hasAlpha());
    }
    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
      pg.grabPixels();
    } catch (InterruptedException e) {
    }
    ColorModel cm = pg.getColorModel();
    System.out.println(cm.hasAlpha());
  }
}



Draw ten rectangles with different levels of transparency

  
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TransparentRectangles extends JPanel {
  public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    for (int i = 1; i <= 10; i++) {
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, i * 0.1f));
      g2d.fillRect(50 * i, 20, 50, 50);
    }
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("Transparent Rectangles");
    frame.add(new TransparentRectangles());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 100);
    frame.setVisible(true);
  }
}