Results 1 to 5 of 5
  1. #1
    maseck is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default Draw image with select background and foreground color

    I'm making a enhanced ASCII interface. I have encountered a problem.

    I must put a image of color X on a background of color Y efficiently.

    example
    http://www.shnetworks4.net/~asciiwor...turntokroz.gif
    look a the C in red or the double ~

    A tileset like this one http://img143.imageshack.us/img143/4869/jasciitiles.png will be used. Each tile is 16x16. The areas that are white will become color X. Grays should became less color X, but this may not be an option.

    I think this pertains to the use of the graphic's XOR mode.
    Last edited by maseck; 11-17-2010 at 07:58 PM. Reason: editing to make readers less trigger happy

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default

    Looks like link spam to gaming sites to me. Abuse reported.

    db

  3. #3
    maseck is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    I had no intention of advertisement. One mistake I made was putting the link at the end and not next to what it referenced. I moved the link closer to what it was a reference to. I also removed one of the links. I use links often because I have poor skills with communicating ideas.

    I am sorry if I caused any distress but I am also frustrated.

    ---------------------
    EDIT: I made some code that works but it causes an error (Class Cast Exception). The change that caused this error was drawing the image a second time. I also wish to know if my implementation is efficient.

    You will need this image with the magenta background.
    Java Code:
    import java.awt.AlphaComposite;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsEnvironment;
    import java.awt.Image;
    import java.awt.Transparency;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JApplet;
    
    public class JASCIITest extends JApplet
    {
    	BufferedImage images;
    	static Color imageBack = new Color(255, 0, 255);
    	
    	public void init()
    	{
    		try
    		{
    			images = ImageIO.read(new File("jasciitiles.PNG").toURI().toURL());
    		} catch (IOException e){
    			e.printStackTrace();
    		}
    		images = applyMask(images, imageBack);
    		images = resize(images, images.getWidth()*4, images.getHeight()*4);
    	}
    	
    	public void paint(Graphics g)
    	{
    		//this works but not sure if it's the best way
    		Color tileBack = Color.CYAN,
    		tileFore = Color.GREEN;
    		
    		g.setColor(Color.WHITE);
    		g.setXORMode(tileFore);
    		g.drawImage(images, 0, 0, null);
    		g.setXORMode(tileBack);
    		g.drawImage(images, 0, 0, null);
    		g.fillRect(0, 0, images.getWidth(), images.getHeight());
    		g.setPaintMode();
    	}
    
    	/*
    	 * THESE ARE FINE, HERE FOR COMPILING
    	 * DID NOT WRITE
    	 * FROM GTGE (LEGAL OBLIGATION)
    	 */
    	public static BufferedImage applyMask(Image img, Color keyColor) {
    		BufferedImage alpha = createImage(img.getWidth(null), img
    				.getHeight(null), Transparency.BITMASK);
    		
    		Graphics2D g = alpha.createGraphics();
    		g.setComposite(AlphaComposite.Src);
    		g.drawImage(img, 0, 0, null);
    		g.dispose();
    		
    		for (int y = 0; y < alpha.getHeight(); y++) {
    			for (int x = 0; x < alpha.getWidth(); x++) {
    				int col = alpha.getRGB(x, y);
    				if (col == keyColor.getRGB()) {
    					// make transparent
    					alpha.setRGB(x, y, col & 0x00ffffff);
    				}
    			}
    		}
    		
    		return alpha;
    	}
    	
    	public static BufferedImage resize(BufferedImage src, int w, int h) {
    		int transparency = src.getColorModel().getTransparency();
    		BufferedImage image = createImage(w, h, transparency);
    		
    		Graphics2D g = image.createGraphics();
    		//g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
    		//		RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    		g.drawImage(src, 0, 0, w, h, // destination
    				0, 0, src.getWidth(), src.getHeight(), // source
    				null);
    		g.dispose();
    		
    		return image;
    	}
    	
    	public static final GraphicsConfiguration CONFIG = GraphicsEnvironment
    	.getLocalGraphicsEnvironment().getDefaultScreenDevice()
    	.getDefaultConfiguration();
    	
    	public static BufferedImage createImage(int width, int height, int transparency) {
    		return CONFIG.createCompatibleImage(width, height,
    				transparency);
    	}
    }
    Last edited by maseck; 11-25-2010 at 02:04 AM. Reason: DERP

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default

    I had no intention of advertisement.
    Fair enough. Please accept my apologies.
    You will need the image with the magenta background I posted in the first post.
    And the AdvImageUtil class and maybe something more -- I stop reading code when it becomes apparent that it can't be compiled and run. To get better help sooner, post a SSCCE that clearly demonstrates your problem. And go through that link before you post it.

    db

  5. #5
    maseck is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Okay, I fixed my mistake... I also read that web-page completely, should be good.

Similar Threads

  1. Replies: 13
    Last Post: 09-29-2010, 10:55 PM
  2. background color with jpanel
    By hannerz06 in forum New To Java
    Replies: 6
    Last Post: 03-31-2010, 03:25 AM
  3. Replies: 6
    Last Post: 01-02-2009, 09:36 PM
  4. BackGround & ForeGround
    By hungleon88 in forum Advanced Java
    Replies: 3
    Last Post: 08-31-2008, 07:20 AM
  5. How to change the foreground color of a disabled control
    By arunkumarsimhadri in forum New To Java
    Replies: 0
    Last Post: 07-09-2008, 04:30 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
  •