Results 1 to 5 of 5
- 11-17-2010, 02:43 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
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
- 11-17-2010, 04:22 AM #2
Looks like link spam to gaming sites to me. Abuse reported.
db
- 11-17-2010, 08:12 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
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
- 11-18-2010, 05:29 AM #4
Fair enough. Please accept my apologies.I had no intention of advertisement.
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.You will need the image with the magenta background I posted in the first post.
db
- 11-25-2010, 02:22 AM #5
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Draw text over background image using JLabel and JFrame
By vytska in forum New To JavaReplies: 13Last Post: 09-29-2010, 10:55 PM -
background color with jpanel
By hannerz06 in forum New To JavaReplies: 6Last Post: 03-31-2010, 03:25 AM -
Making your own font/foreground-background color?
By Durnus in forum Java 2DReplies: 6Last Post: 01-02-2009, 09:36 PM -
BackGround & ForeGround
By hungleon88 in forum Advanced JavaReplies: 3Last Post: 08-31-2008, 07:20 AM -
How to change the foreground color of a disabled control
By arunkumarsimhadri in forum New To JavaReplies: 0Last Post: 07-09-2008, 04:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks