Results 1 to 6 of 6
  1. #1
    TacoManStan is offline Member
    Join Date
    Sep 2011
    Posts
    50
    Rep Power
    0

    Default BufferedImage Chnages Alpha Values When Moved

    Hey, so I have a BufferedImage that is displayed on a JFrame. I am attempting to load each pixel value seperatly, and then create a new image based off of that data. I am going to copy and paste the code below so you can get a better idea of what I am doing. Basically, the problem is that whenever I move the screen, the alpha value gets all messed up. I will attach an image showing what it looks like. The only possibility I can think of is that I didn't do this on the EDT, so there might be some issues there, but I don't know.BufferedImage Chnages Alpha Values When Moved-weird-alpha.jpg

    Java Code:
    public class ImageTester2 {
    
        static private final int RED_VALUE = 0;
        static private final int GREEN_VALUE = 0;
        static private final int BLUE_VALUE = 0;
    
        public static void main(String[] args) {
            BufferedImage image = null;
            File file = new File("TestImage.png");
            int value = 50;
    
            try {
                image = ImageIO.read(file);
            } catch (IOException j) {
                System.out.println("Image could not be read");
            }
    
            System.out.println(image.getType());
            BufferedImage image2 = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
    
            for (int x = 0; x < image.getWidth(); x++) {
                for (int y = 0; y < image.getHeight(); y++) {
                    Color color = new Color(image.getRGB(x ,y));
                    int redVal = 0;
                    int greenVal = 0;
                    int blueVal = 0;
                    if (color.getRed() + RED_VALUE < 0) {
                        redVal = 0;
                    } else if (color.getRed() + RED_VALUE > 255) {
                        redVal = 255;
                    } else {
                        redVal = color.getRed() + RED_VALUE;
                    }
                    
                    if (color.getGreen() + GREEN_VALUE < 0) {
                        greenVal = 0;
                    } else if (color.getGreen() + GREEN_VALUE > 255) {
                        greenVal = 255;
                    } else {
                        greenVal = color.getGreen() + GREEN_VALUE;
                    }
                    
                    if (color.getBlue() + BLUE_VALUE < 0) {
                        blueVal = 0;
                    } else if (color.getBlue() + BLUE_VALUE > 255) {
                        blueVal = 255;
                    } else {
                        blueVal = color.getBlue() + BLUE_VALUE;
                    }
                    Color returnColor = new Color(redVal, greenVal, blueVal, 10);
                    image2.setRGB(x, y, returnColor.getRGB());
                }
            }
    
            JFrameMain2 frame = new JFrameMain2(image2);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(image2.getWidth(), image2.getHeight());
            frame.setResizable(false);
            frame.setBackground(Color.WHITE);
            frame.setForeground(Color.WHITE);
        }
    }
    
    public class JFrameMain2 extends JFrame {
    
        private BufferedImage img;
    
        public JFrameMain2(BufferedImage img) {
            this.img = img;
        }
        
        @Override
        public void paint(Graphics g) {
            Graphics2D g2 = (Graphics2D)g;
            g2.drawImage(img, null, 0, 0);
        }
    }
    Take note that this is all experimental, so please don't critique the code layout. I know I break a couple rules in here, but I was just doing this to test out the image properties.
    Last edited by TacoManStan; 10-19-2011 at 01:38 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: BufferedImage Chnages Alpha Values When Moved

    Java Code:
    Color returnColor = new Color(redVal, greenVal, blueVal, 10);
    Every pixel has it's alpha set to 10, rather than the alpha of the original image.

  3. #3
    TacoManStan is offline Member
    Join Date
    Sep 2011
    Posts
    50
    Rep Power
    0

    Default Re: BufferedImage Chnages Alpha Values When Moved

    Ya, that is supposed to happen. Every alpha is supposed to be 10. When the image loads, every pixel has an alpha of 10 like it should. But then when I move it, a lot of the alpha values change depending on what is off-screen. I'm confused as to why the alpha values change dynamically, with the repainting of the JFrame.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: BufferedImage Chnages Alpha Values When Moved

    Call super.paint to clear the component (it is never cleared, thus the image is continually over-layed on top of previously drawn images). For what its worth - you mentioned you didn't want comments on code but I'm giving it anyways - you should draw to a JPanel and use paintComponent (calling super.paintComponent as the first statement)

  5. #5
    TacoManStan is offline Member
    Join Date
    Sep 2011
    Posts
    50
    Rep Power
    0

    Default Re: BufferedImage Chnages Alpha Values When Moved

    Lol thanks, that I am actually going to be doing that but with a JLayeredPane. Thanks a ton though, I had completely forgotten that I have to call the super method... duh. Thanks :)

  6. #6
    TacoManStan is offline Member
    Join Date
    Sep 2011
    Posts
    50
    Rep Power
    0

    Default Re: BufferedImage Chnages Alpha Values When Moved

    I have another issue. When the frame is resized, super.paint(g) is called and the screen is wiped clean. Because of that, the screen flickers as the screen is resized, regardless of double buffering... Any solutions?

    EDIT: Duh, I am still drawing to a JFrame. Nevermind, I forgot to switch over to using paintComponent().
    Last edited by TacoManStan; 10-20-2011 at 03:44 PM.

Similar Threads

  1. why business logic should be moved out of JSP ?
    By nithinurs in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-29-2011, 06:48 AM
  2. Log4J Question <moved>
    By razorSpirit in forum New To Java
    Replies: 2
    Last Post: 10-13-2010, 10:33 PM
  3. Setting alpha in BufferedImage
    By samcoles in forum Java 2D
    Replies: 7
    Last Post: 02-12-2010, 05:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •