Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Problem drawing an image in greyscale

    I'm trying to draw an image in grey scale. This is my code:

    Java Code:
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		setOpaque(true);	
    		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    			Graphics g1 = image.getGraphics();
    			g1.drawImage(CardData.CARD_ART[cardID], 0, 0, null);
    			g1.dispose();
    setOpaque(false);
    	}
    For some reason, nothing displays. :(

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Problem drawing an image in greyscale

    You draw something from CardData to the graphics context of the BufferedImage. But you never draw
    the image to the the screen using the graphics context passed to paintComponent.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Problem drawing an image in greyscale

    Thanks. That solved my problem! :)

  4. #4
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Problem drawing an image in greyscale

    Now I have another problem: the supposed-to-be transparent background becomes black.

  5. #5
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Problem drawing an image in greyscale

    bump

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Problem drawing an image in greyscale

    A TYPE_BYTE_GRAY BufferedImage doesn't know about transparency ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Problem drawing an image in greyscale

    How can I make it gray with some transparent areas?

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

    Default Re: Problem drawing an image in greyscale

    This is just a guess, and I may be very well wrong, but by Grayscale do you mean you're trying to make RGB and Transparency? If so, then I use TYPE_INT_ARGB, this allows Alpha which is Transparency. I'm not sure if this is what you mean, and sorry if I'm very mistaken but just guessing. P:

  9. #9
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Problem drawing an image in greyscale

    Basically, I have an image in color with some transparent areas. Upon certain conditions, I want it to be re-painted in black & white BUT keep the transparent areas.
    Right now, I can re-paint it in black and white but it loses its transparent areas.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Problem drawing an image in greyscale

    A quick internet search on "java gray scaled with transparency" provided the following:

    java - how can I convert an image to grayscale without losing transparency? - Stack Overflow

    Reminder: Please do a web search before asking your questions as they may already be answered somewhere else.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Problem drawing an image in greyscale

    Thanks jim. I'll try to google better in the future. Sorry about that. :P

Similar Threads

  1. Problem with drawing to a double buffer image
    By Komposten in forum New To Java
    Replies: 9
    Last Post: 03-05-2012, 06:30 PM
  2. BufferedImage - greyscale - color information
    By AndreB in forum Advanced Java
    Replies: 8
    Last Post: 03-04-2010, 11:09 AM
  3. Replies: 1
    Last Post: 04-17-2009, 01:44 AM
  4. Drawing on an image
    By kratra in forum New To Java
    Replies: 0
    Last Post: 02-06-2009, 09:58 PM
  5. drawing an image to an offscreen image
    By hunterbdb in forum Java 2D
    Replies: 9
    Last Post: 10-30-2008, 07:17 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
  •