Results 1 to 7 of 7
  1. #1
    Durnus is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Question Making your own font/foreground-background color?

    I'm making a game. (I know... isn't everyone?) This game so far uses an image containing the first 256 ASCII characters. I've been drawing them to the screen with a buffer, using the background color successfully. But now I want the actual letters, not just the transparent space, to be colored.

    There are two options for me here. Either I make my own font, which may be prohibitively hard, or I turn all of the pixels not transparent to the new color. So my question is which one would work, and how to do it.

    Thanks.

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

    Default

    > I turn all of the pixels not transparent to the new color
    Never tried it with drawImage, but you may be able to do that using Graphics#setXORMode.

    db

  3. #3
    Durnus is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    Ooh, looks promising! It doesn't seem to work in practice, and I couldn't find much documentation, so I'll just post something here.

    Java Code:
    bufferGraphics.setColor(Color.BLUE);
    bufferGraphics.setXORMode(new Color(255,255,255));
    bufferGraphics.drawImage(charImg,0,0, null);
    Without the first two lines it works fine, but once I add in the setcolor and XORMode, it doesn't paint anything at all. I assume I got the ordering wrong, but it might just be that the XORMode doesn't work as it should.

    One thing to note is that I am using the colors 255,255,255 and transparent (default bufferedImage non-drawn pixels with BufferedImage.TYPE_INT_ARGB.) in charImg, and the target (bufferGraphics) also uses TYPE_INT_ARGB.

    All I can find about XORMode suggests that this should work, thanks for any insight about this.

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

    Default

    If the character image has white and transparent, and you want to substitute that white with blue, you should
    -- setColor(Color.WHITE
    -- setXORMode(Color.BLUE)

    When drawing operations are performed, pixels which are the current color are changed to the specified color, and vice versa.

    db

  5. #5
    Durnus is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    Wow, it works! I can't believe I read that backwards.

    Thanks again for the help!

    EDIT:
    Dang, setXORMode doesn't work with the ARGB bufferedimage type. Just putting this out there just in case someone else can read this instead of taking thirty minutes trying to figure out why it works somewhere but not somewhere else.
    Last edited by Durnus; 12-29-2008 at 05:19 PM.

  6. #6
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Smile Alpha channel

    The Color class is used to encapsulate colors in the default sRGB color space or colors in arbitrary color spaces identified by a ColorSpace. Every color has an implicit alpha value of 1.0 or an explicit one provided in the constructor. The alpha value defines the transparency of a color and can be represented by a float value in the range 0.0 - 1.0 or 0 - 255. An alpha value of 1.0 or 255 means that the color is completely opaque and an alpha value of 0 or 0.0 means that the color is completely transparent. When constructing a Color with an explicit alpha or getting the color/alpha components of a Color, the color components are never premultiplied by the alpha component.
    Just use the four fp constructor signature, gives pleasing screens....
    Color(float,float,float,float);
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  7. #7
    Durnus is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    Oh, whoa. Never thought the problem would be in my color constructions. Thanks, it works perfectly now!

Similar Threads

  1. Replies: 5
    Last Post: 04-25-2009, 08:00 PM
  2. BackGround & ForeGround
    By hungleon88 in forum Advanced Java
    Replies: 3
    Last Post: 08-31-2008, 07:20 AM
  3. Disabled JButton font color
    By p900128 in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2008, 08:16 AM
  4. 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
  5. window background color?
    By javan00b in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 10:43 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
  •