简体   繁体   中英

How to make the transparency transparent in Java graphics?

 private static void convertTo2D(BufferedImage image, Graphics g, int locX, int locY, int sizeWidth, int sizeHeight) {
    int imageHeight = image.getHeight();
    int imageWidth = image.getWidth();
    Image img = image.getScaledInstance((sizeWidth), (sizeHeight), Image.SCALE_SMOOTH);
    image = new BufferedImage((sizeWidth), (sizeHeight), BufferedImage.TYPE_INT_RGB);
    image.getGraphics().drawImage(img, 0, 0, null);
    //The horizontal of image
    for(int x=0; x<(sizeWidth); x++) {
        //The vertical of image
        for(int y=0; y<(sizeHeight); y++) {
            //Get RGB color and draw it
            int color = image.getRGB(x, y);
            Color clr = new Color(color, true);
            g.setColor(clr);
            // Alpha = 0 - Fully Transparent
            if(clr.getAlpha() !=1) {
                g.drawLine((x + locX), (y + locY), (x + locX), (y + locY));
            }
        }
    }
}

This is my code. Basically I don't want it to draw the pixel if it's a transparent pixel (A transparent pixel would be a pixel that has the checkerboard as it's background when you view images on google). What am I doing wrong? Currently the background of these images is just black, so the pixel does get drawn for it then?...

What I would do is paint the background first, then paint the image over it, something like...

protected BufferedImage makeImageFrom(BufferedImage original) {
    BufferedImage img = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = img.createGraphics();
    g2d.setColor(Color.WHITE);
    g2d.fillRect(0, 0, img.getWidth(), img.getHeight());
    g2d.setColor(Color.LIGHT_GRAY);
    int row = 0;
    for (int y = 0; y < img.getHeight(); y += 10) {
        int offset = (row % 2 == 0) ? 10 : 0;
        for (int x = 0; x < img.getWidth(); x += 20) {
            g2d.fillRect(offset + x, y, 10, 10);
        }
        row++;
    }
    g2d.drawImage(original, 0, 0, this);
    g2d.dispose();
    return img;
}

登机检查

And the code used to test it...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test {

    public static void main(String args[]) throws ParseException {
        new Test();
    }

    public Test() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage img;

        public TestPane() throws IOException {
            BufferedImage original = ImageIO.read(get your own image);
            img = makeImageFrom(original);
        }

        protected BufferedImage makeImageFrom(BufferedImage original) {
            BufferedImage img = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = img.createGraphics();
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, img.getWidth(), img.getHeight());
            g2d.setColor(Color.LIGHT_GRAY);
            int row = 0;
            for (int y = 0; y < img.getHeight(); y += 10) {
                int offset = (row % 2 == 0) ? 10 : 0;
                for (int x = 0; x < img.getWidth(); x += 20) {
                    g2d.fillRect(offset + x, y, 10, 10);
                }
                row++;
            }
            g2d.drawImage(original, 0, 0, this);
            g2d.dispose();
            return img;
        }

        @Override
        public Dimension getPreferredSize() {
            return img == null ? new Dimension(100, 100) : new Dimension(img.getWidth(), img.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (img != null) {
                int x = (getWidth() - img.getWidth()) / 2;
                int y = (getHeight() - img.getHeight()) / 2;
                g.drawImage(img, x, y, this);
            }
        }

    }
}

I guess I didn't explain best. What I meant is, when the background is checkered (meaning it's a transparent image), the background should be clear and just have whatever has been painted in the background behind it. However right now, it will just fill the rest of the image height and width that are empty pixels with black? How do I prevent that? I was told by a friend it has to do with the alpha?

I'd be really nice if you could provide an image of what you want and what you have ;P

I modified the above code to use a transparent image as default base, clear the background and paint the rest as per before...I also changed the background color of the TestPane to verify it ;)

透明

protected BufferedImage makeImageFrom(BufferedImage original) {
    BufferedImage img = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = img.createGraphics();
    g2d.setBackground(new Color(255, 255, 255, 0));
    g2d.clearRect(0, 0, img.getHeight(), img.getHeight());
    g2d.setColor(Color.LIGHT_GRAY);
    int row = 0;
    for (int y = 0; y < img.getHeight(); y += 10) {
        int offset = (row % 2 == 0) ? 10 : 0;
        for (int x = 0; x < img.getWidth(); x += 20) {
            g2d.fillRect(offset + x, y, 10, 10);
        }
        row++;
    }
    g2d.drawImage(original, 0, 0, this);
    g2d.dispose();
    return img;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM