Results 1 to 12 of 12
  1. #1
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    133
    Rep Power
    0

    Default BufferedImage Alpha RGB, How do I make the Alpha work?

    So what I do normally is draw the bottom layer of grass, and when I draw the second layer with trees, the tree layer has a shadow, that shadow doesn't go ontop of the grass layer, but instead it overwrites the grass layer aswell and I've checked the color codes, the shadow has combined with white, not the first layer.

    it's obvious that that's not how alpha works then, how do I do it?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,988
    Rep Power
    9

    Default Re: BufferedImage Alpha RGB, How do I make the Alpha work?

    Can you provide an SSCCE that demonstrates exactly what you're talking about?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    133
    Rep Power
    0

    Default Re: BufferedImage Alpha RGB, How do I make the Alpha work?

    Hmm, I'll give you a brief,

    I make a buffered Image TYPE_INT_ARGB, and I draw with Graphics.drawImage a image of just grass (.png), and then I draw another image also TYPE_INT_ARGB with buffered image, ontop of the grass one and it's layer 2, layer 2 has trees with shadows, the shadows are half-trasperant, instead of the shadows blending with layer 1 (the grass), it blends with white colour. And it completely overwrites, literally removes the area that has grass where the shadow now is, making the shadow there blended with white colour.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,988
    Rep Power
    9

    Default Re: BufferedImage Alpha RGB, How do I make the Alpha work?

    It's going to be pretty hard to help you without seeing some code. We don't need your whole program, just enough to see what you're talking about.

    More info here: How to create a Minimal, Complete, and Verifiable example - Help Center - Stack Overflow
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    133
    Rep Power
    0

    Default Re: BufferedImage Alpha RGB, How do I make the Alpha work?

    Rather than me having an issue, I'm asking how do I blend two Buffered TYPE_INT_ARGB Images?

  6. #6
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    133
    Rep Power
    0

    Default Re: BufferedImage Alpha RGB, How do I make the Alpha work?

    I'm not getting errors, I just need to know how I can blend two Buffered ARGB Images.
    So like, you get white half-transperant, and black half-transperant together you get grey slightly transperant.

    if you still don't get what I mean, you go to photostop or paintNet, make an empty background, NOT white. Empty, erased. And then you set opacity of a colour to like 128 and draw the colour with like a paintbrush, then you select another colour which is also 128 opacity, and you draw it on the same spot, those colours will blend rather than the second one overwriting the first one.

    If you still don't understand I'll give you the code, I don't see a reason why you need it, because I'm trying to understand how I can make an ARGB image blend. P:

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,988
    Rep Power
    9

    Default Re: BufferedImage Alpha RGB, How do I make the Alpha work?

    Without more info, the best we can do is point you to google and the tutorials: Drawing an Image (The Java™ Tutorials > 2D Graphics > Working with Images)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    133
    Rep Power
    0

    Default Re: BufferedImage Alpha RGB, How do I make the Alpha work?

    Ok, I'll give you my code, what do I need to change in order to make my tree layer's transperant parts blend with my grass layer.

    Java Code:
    package pack.script.game;
    
    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    //import java.awt.image.DataBufferInt;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    
    public class Game extends Canvas implements Runnable, KeyListener {
    	private static final long serialVersionUID = 1L;
    	
    	public boolean iRUNNING = false;
    	
    	public static final String sNAME = "Java Game";
    	public static final String sVER = "v1.00";
    	public static final int sSIZE = 20;
    	public static final int sSCALE = 16;
    	public static final int sBORDERS = 5;
    	public static int iSCALE = 2;
    	public static int tSIZE = iSCALE * sSCALE;
    	
    	public static final String sDISPLAYNAME = sNAME + " " + sVER;
    	public static final int sWIDTH = sSIZE * sSCALE;
    	public static final int sHEIGHT = (int) ((double)sWIDTH / 12D * 9D);
    	public static int iWIDTH = sWIDTH * iSCALE;
    	public static int iHEIGHT = sHEIGHT * iSCALE;
    	public static Dimension sDIMENSION = new Dimension(iWIDTH,iHEIGHT);
    	public static Map[] Maps;
    	
    	public static int[] XGRID = new int[(sWIDTH / sSCALE)];
    	public static int[] YGRID = new int[(sHEIGHT / sSCALE)];
    	
    	public static int[] rXGRID = new int[XGRID.length];
    	public static int[] rYGRID = new int[YGRID.length];
    	public static boolean qInit = false;
    	
    	public static Map mAkhuForest = new Map("Akhu Forest", 1);
    	
    	private BufferedImage iIMAGE = new BufferedImage (iWIDTH, iHEIGHT, BufferedImage.TYPE_INT_ARGB);
    	//private int[] pixels = ((DataBufferInt) iIMAGE.getRaster().getDataBuffer()).getData();
    	private JFrame sFRAME;
    	
    	public Game() {
    		setMinimumSize(new Dimension(iWIDTH,iHEIGHT));
    		setMaximumSize(new Dimension(iWIDTH,iHEIGHT));
    		setPreferredSize(new Dimension(iWIDTH,iHEIGHT));
    		
    		sFRAME = new JFrame(sDISPLAYNAME);
    		sFRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		sFRAME.setLayout(new BorderLayout());
    		sFRAME.add(this, BorderLayout.CENTER);
    		sFRAME.setSize(iWIDTH, iHEIGHT);
    		sFRAME.pack();
    		
    		sFRAME.setResizable(false);
    		sFRAME.setLocationRelativeTo(null);
    		sFRAME.setVisible(true);
    	}
    	
    	public synchronized void start() {
    		iRUNNING = true;
    		for (int i = 0; i < rXGRID.length; i++) { rXGRID[i] = 5 + (i * (iSCALE * sSCALE)); }
    		for (int i = 0; i < rYGRID.length; i++) { rYGRID[i] = 5 + (i * (iSCALE * sSCALE)); }
    		
    		mAkhuForest.setPassage(nPASSARR(mAkhuForest));
    		
    		new Thread(this).start();
    	}
    	
    	public int[][][] nPASSARR(Map mapname) {
    		int width = mapname.getWidth() / sSCALE;
    		int height = mapname.getHeight() / sSCALE;
    		int[][][] Array = new int[width][height][5];
    		return Array;
    	}
    	
    	public void run() {
    		long cLASTTIME = System.nanoTime();
    		double sNSPERTICK = 1000000000D / 60D;
    		
    		int cFRAMES = 0;
    		int cTICKS = 0;
    		
    		long cLASTTIMER = System.currentTimeMillis();
    		double delta = 0;
    		
    		while (iRUNNING) {
    			long cNOWTIME = System.nanoTime();
    			delta += (cNOWTIME - cLASTTIME) / sNSPERTICK;
    			cLASTTIME = cNOWTIME;
    			boolean shouldRender = true;
    			
    			while (delta >= 1) { cTICKS++; tick(); delta--; shouldRender = true; }
    			
    			try { Thread.sleep(2); } catch (InterruptedException e)
    			{ e.printStackTrace(); }
    			
    			if (shouldRender) { cFRAMES++; }
    			if (System.currentTimeMillis() - cLASTTIMER >= 1000) {
    				cLASTTIMER += 1000;
    				System.out.println(cFRAMES + " speed, " + cTICKS + " ticks");
    				cFRAMES = 0; cTICKS = 0;
    			}
    		}
    	}
    	
    	public void tick() {
    		BufferStrategy sBUFFERSTRATEGY = getBufferStrategy();
    		if (sBUFFERSTRATEGY == null) {
    			createBufferStrategy(3);
    			return;
    		}
    		
    		Graphics sGRAPHICS = sBUFFERSTRATEGY.getDrawGraphics();
    		sGRAPHICS.drawImage(iIMAGE, 0, 0, getWidth(), getHeight(), null);
    		
    		sGRAPHICS.setColor(Color.BLACK);
    		gRECT(sGRAPHICS, 0, 0, getWidth(), getHeight(), true);
    		
    		sGRAPHICS.setColor(Color.BLACK);
    		gRECT(sGRAPHICS, 5, 5, getWidth() - 5, getHeight() - 5, true);
    		
    		//mAkhuForest.getPostLayers();
    		
    		mAkhuForest.drawLayers(sGRAPHICS, sX(1), sY(1), false);
    		//mAkhuForest.drawLayer(sGRAPHICS, "layer2.png", sX(1), sX(1));
    		
    		//drawImage(sGRAPHICS, "res/ground/grass01.png", sX(1), sY(1), 16, 16);
    		//drawImage(sGRAPHICS, "res/ground/grass01.png", sX(20), sY(1), 16, 16);
    		//drawImage(sGRAPHICS, "res/ground/grass01.png", -10, -10);
    		
    		/*int[][][] abc = new int[(sWIDTH / sSCALE)][(sHEIGHT / sSCALE)][5];
    		Map test = new Map("testmap","res/maps/test", abc, 3);
    		System.out.print(test.getHeight());*/
    		
    		mAkhuForest.drawLayers(sGRAPHICS, sX(1), sY(1), true);
    		
    		sGRAPHICS.setColor(Color.BLACK);
    		for (int i = 0; i < sBORDERS; i++) {
    		gRECT(sGRAPHICS, 0 + i, 0 + i, getWidth() - (i + 1), getHeight() - (i + 1), false);	}
    		
    		sGRAPHICS.dispose();
    		sBUFFERSTRATEGY.show();
    	}
    	
    	public int sX(int place) { return rXGRID[place - 1]; }
    	public int sY(int place) { return rYGRID[place - 1]; }
    	
    	public static BufferedImage Img(String path) {
    		BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
    		try { image = ImageIO.read(new File(path)); }
    		catch (IOException e) { e.printStackTrace(); }
    		return image;
    	}
    
    	public void drawImage (Graphics g, String path, int x, int y) {
    		BufferedImage img = Img(path);
    		int w = img.getWidth() * iSCALE;
    		int h = img.getHeight() * iSCALE;
    		g.drawImage(img, x, y, w, h, null);
    	}
    	
    	public void gRECT(Graphics g, int x1, int y1, int x2, int y2, boolean fill) {
    		int x[] = {x1,x2,x2,x1,x1};
    		int y[] = {y1,y1,y2,y2,y1};
    		if (fill == false) {
    			g.drawPolygon(x, y, 5);
    		} else {
    			g.fillPolygon(x, y, 5);
    		}
    	}
    	
    	public void gPIX(Graphics g, int x, int y) {
    		g.drawLine(x, y, x, y);
    	}
    	
    	public static void main(String[] args) {
    		new Game().start();
    	}
    
    	public void keyPressed(KeyEvent arg0) {
    		
    	}
    
    	public void keyReleased(KeyEvent arg0) {
    		
    	}
    
    	public void keyTyped(KeyEvent arg0) {
    		
    	}
    }
    Java Code:
    package pack.script.game;
    
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.util.Arrays;
    
    public class Map {
    	String mNAME;
    	String mPATH;
    	
    	int[][][] mPASSAGE;
    	int mENTPRIOR;
    	int scale = Game.iSCALE;
    	
    	File folder;
    	File[] layers;
    	String[] nLayers;
    	
    	String mEXAMPLELAYER;
    	BufferedImage mIMAGE;
    	
    	public Map(String mNAME, int mENTPRIOR) {
    		this.mNAME = mNAME;
    		mPATH = "res/maps/" + mNAME;
    		this.mENTPRIOR = mENTPRIOR;
    		
    		this.folder = new File(getLayersPath());
    		this.layers = folder.listFiles();
    		this.nLayers = new String[this.layers.length];
    		
    		mEXAMPLELAYER = getLayersPath() + "/" + layers[0].getName();
    		
    		for (int i = 0; i < layers.length;i++) { nLayers[i] = layers[i].getName(); }
    		Arrays.sort(nLayers);
    		mIMAGE = Game.Img(mEXAMPLELAYER);
    	}
    	public String getName() { return mNAME; }
    	public String getPath() { return mPATH; }
    	public String getLayersPath() { return (mPATH + "/layers"); }
    	public int getWidth() { return mIMAGE.getWidth(); }
    	public int getHeight() { return mIMAGE.getHeight(); }
    	public void setPassage(int[][][] array) { this.mPASSAGE = array; }
    
    	public boolean getPassage(int x, int y, String area) {
    		int areaPos = 0;
    		if (area.equals("north")) { areaPos = 1; }
    		else if (area.equals("east")) { areaPos = 2; }
    		else if (area.equals("south")) { areaPos = 3; }
    		else if (area.equals("west")) { areaPos = 4; }
    		else { areaPos = 0; }
    		
    		try {
    			if (mPASSAGE[x - 1][y - 1][areaPos] == 1) {
    				return true;
    			} else {
    				return false;
    			}
    		} catch (ArrayIndexOutOfBoundsException e) {
    			return false;
    		}
    	}
    	
    	public int bConv(boolean boolvalue) {
    		int integer;
    		if (boolvalue == true) {
    			integer = 1;
    		} else {
    			integer = 0;
    		} return integer;
    	}
    	
    	public void fillPass(boolean value) {
    		Arrays.fill(mPASSAGE, bConv(value));
    	}
    	
    	public void fillTile(int x, int y, boolean value) {
    		Arrays.fill(mPASSAGE[x - 1][y - 1], bConv(value));
    	}
    	
    	public int[][][] getPassArray() { return mPASSAGE; }
    	public boolean[] getTilePassArray(int x, int y) {
    		boolean[] array = new boolean[5];
    		Arrays.fill(array, false);
    		for (int i = 0; i < mPASSAGE[x - 1][y - 1].length; i++) {
    			if (mPASSAGE[x - 1][y - 1][i] == 1) {
    				array[i] = true;
    			} else {
    				array[i] = false;
    			}
    		}
    		return array;
    	}
    	
    	public String[] getPreLayers() {
    		int lCount = 0;
    		for (int i = 0; i < mENTPRIOR; i++) { lCount++; }
    		String[] preLayers = new String[lCount];
    		for (int i = 0; i < mENTPRIOR; i++) { preLayers[i] = nLayers[i]; }
    		return preLayers;
    		
    	}
    	public String[] getPostLayers() {
    		int lCount = 0;
    		for (int i = mENTPRIOR; i < nLayers.length; i++) { lCount++; }
    		String[] postLayers = new String[lCount];
    		for (int i = mENTPRIOR + 1; i < nLayers.length + 1; i++) {
    			postLayers[i - mENTPRIOR - 1] = nLayers[i - 1];
    		}
    		return postLayers;
    	}
    	public BufferedImage getLayer(String layer) {
    		return Game.Img(getLayersPath() + "/" + layer);
    	}
    	public void drawLayer(Graphics g, String layer, int x, int y) {
    		int width = getLayer(layer).getWidth() * scale;
    		int height = getLayer(layer).getHeight() * scale;
    		g.drawImage(getLayer(layer), x, y, width, height, null);
    	}
    	public void drawLayers(Graphics g, int x, int y, boolean qPost) {
    		if (qPost == true) {
    			for (int i = 0; i < getPostLayers().length; i++) {
    				drawLayer(g, getPostLayers()[i], x, y);
    			}
    		} else {
    			for (int i = 0; i < getPreLayers().length; i++) {
    				drawLayer(g, getPreLayers()[i], x, y);
    			}
    		}
    	}
    }
    Here, there's all of it, it works without any errors.

  9. #9
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: BufferedImage Alpha RGB, How do I make the Alpha work?

    I didn't check your code because it is simply to much. I understood, that you have problems with the alpha channel?

    There is nothing that you have to take care of. Simply draw your images in the correct order. I create a quick example that can be used directly (but you still need to have the 2 pictures!):

    Java Code:
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    
    public class AlphaChannelTest extends JFrame {
    
        BufferedImage background;
        BufferedImage foreground;
    
        public void loadIamges() throws Exception {
            background = ImageIO.read(new File("/tmp/background.png"));
            foreground = ImageIO.read(new File("/tmp/picture.png"));
        }
    
        @Override
        public void paint(Graphics g) {
            g.drawImage(background, 50, 50, null);
            g.drawImage(foreground, 50, 50, null);
        }
    
        public static void main(String args[]) {
            try {
                AlphaChannelTest frame = new AlphaChannelTest();
                frame.loadIamges();
                frame.setSize(200,200);
                frame.setVisible(true);
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
    }
    The two pictures in my example was quickly created in gimp, both size 100x100.

    1st is simply a green background, no transparency.

    BufferedImage Alpha RGB, How do I make the Alpha work?-background.png

    2nd was started with a transparent background. One big red line without alpha was drawn and then another one with 19% Opacity.

    BufferedImage Alpha RGB, How do I make the Alpha work?-picture.png

    Or did I missunderstand your problem?

    With kind regards,

    Konrad

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: BufferedImage Alpha RGB, How do I make the Alpha work?

    Quote Originally Posted by augustas656 View Post
    I'm not getting errors, I just need to know how I can blend two Buffered ARGB Images.
    Alpha compositing is the answer:

    Compositing Graphics (The Java™ Tutorials > 2D Graphics > Advanced Topics in Java2D)

    You have to understand that the technology you're using here is called "java2d". If you google that ("java java2d alpha blending" for example) you get relevant results.

    Also realize that Java2D is really old and quirky and applying alpha blending might cause the performance of your application to be just fine on one machine and be terrible on another machine.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    133
    Rep Power
    0

    Default Re: BufferedImage Alpha RGB, How do I make the Alpha work?

    Quote Originally Posted by kneitzel View Post
    I didn't check your code because it is simply to much. I understood, that you have problems with the alpha channel?

    There is nothing that you have to take care of. Simply draw your images in the correct order. I create a quick example that can be used directly (but you still need to have the 2 pictures!):

    Java Code:
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    
    public class AlphaChannelTest extends JFrame {
    
        BufferedImage background;
        BufferedImage foreground;
    
        public void loadIamges() throws Exception {
            background = ImageIO.read(new File("/tmp/background.png"));
            foreground = ImageIO.read(new File("/tmp/picture.png"));
        }
    
        @Override
        public void paint(Graphics g) {
            g.drawImage(background, 50, 50, null);
            g.drawImage(foreground, 50, 50, null);
        }
    
        public static void main(String args[]) {
            try {
                AlphaChannelTest frame = new AlphaChannelTest();
                frame.loadIamges();
                frame.setSize(200,200);
                frame.setVisible(true);
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
    }
    The two pictures in my example was quickly created in gimp, both size 100x100.

    1st is simply a green background, no transparency.

    Click image for larger version. 

Name:	background.png 
Views:	10 
Size:	8.4 KB 
ID:	6042

    2nd was started with a transparent background. One big red line without alpha was drawn and then another one with 19% Opacity.

    Click image for larger version. 

Name:	picture.png 
Views:	10 
Size:	410 Bytes 
ID:	6043

    Or did I missunderstand your problem?

    With kind regards,

    Konrad
    Ermm, but that's what I've done, I've first drawn the PreLayers, which is only 1, the grass layer
    and then I've drawn the PostLayers which is only 1, the tree layer, the tree layer consists of Fully visible and partly visible, maybe that's the issue? Maybe I need to seperate shadows image from trees image?

  12. #12
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    133
    Rep Power
    0

    Default Re: BufferedImage Alpha RGB, How do I make the Alpha work?

    I've decided not to use ARGB, and use RGB instead until further notice.

Similar Threads

  1. Hypersocket 0.0.2 Alpha
    By java software in forum Java Software
    Replies: 0
    Last Post: 06-30-2013, 11:44 PM
  2. recls 1.9.1(alpha 7)
    By java software in forum Java Software
    Replies: 0
    Last Post: 03-03-2012, 05:49 PM
  3. BufferedImage Chnages Alpha Values When Moved
    By TacoManStan in forum New To Java
    Replies: 5
    Last Post: 10-20-2011, 03:41 PM
  4. Setting alpha in BufferedImage
    By samcoles in forum Java 2D
    Replies: 7
    Last Post: 02-12-2010, 05:07 AM
  5. SableCC 4-alpha.4
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 03-10-2008, 01:57 PM

Posting Permissions

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