0
 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?...

  • The first thing I would do is paint the background and then paint the image over it – MadProgrammer Oct 04 '17 at 21:32
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Oct 05 '17 at 04:28

1 Answers1

1

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;
}

Check boarded

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 ;)

Transparent

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;
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 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? – Jared Scarito Oct 04 '17 at 22:26
  • Apologies completely! It's very hard to explain without images, you are right! Basically I have let's say a pick background and this image: https://img00.deviantart.net/d9fa/i/2017/079/7/d/batman___transparent_by_asthonx1-db2yliv.png See, how it has the checkerboard as the background meaning it's transparent? What I want to do is then draw the image on the graphics board and I want that transparency to transfer so that pixel wouldn't get drawn. When I do it currently, it takes those pixels and just makes them black. Does that make more sense? So sorry for all the confusion!! – Jared Scarito Oct 04 '17 at 22:40
  • 1
    Okay, I still have no idea what you're trying to do - are you trying mask the image in some way? A better idea might be to provide an image of what you're trying to do – MadProgrammer Oct 05 '17 at 02:07
  • *"I still have no idea what you're trying to do"* I (upvoted your answer but..) voted to close the question. Too little information here. – Andrew Thompson Oct 05 '17 at 04:29