Results 1 to 4 of 4
  1. #1
    sublixt's Avatar
    sublixt is offline Member
    Join Date
    Jul 2011
    Location
    somewhere
    Posts
    37
    Rep Power
    0

    Default draw a translucent black rectangle

    im trying to draw a translucent black rectangle over an image. is there a way of doing this without replacing the image underneath?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: draw a translucent black rectangle

    Yes, just draw it in the paintComponent of whatever component you're in. For more details, you'll have to tell more first.

  3. #3
    sublixt's Avatar
    sublixt is offline Member
    Join Date
    Jul 2011
    Location
    somewhere
    Posts
    37
    Rep Power
    0

    Default Re: draw a translucent black rectangle

    Well I'm using a Graphics2D object for drawing everything on the image is an Image object drawn on with draw image. I was wondering was either where I should look to find the method for drawing the rectangle or what the method is that does this kind of drawing because I looked in both Graphics and Graphics2D and couldn't find it.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: draw a translucent black rectangle

    Are you doing this drawing in a paintComponent method? Or with a Graphics2D object extracted from a BufferedImage?

    Either way, one key is layering -- to make sure that the rectangle is drawn over the current image. This could be as simple as making sure that the rectangle drawing code comes after the other image drawing code, or could involve separating the two drawings (my recommendation) by say drawing the image in a BufferedImage, and displaying that in a paintComponent method and then drawing the rectangle after this in the same paintComponent method, or you could even draw on a completely different component such as a GlassPane. Lots of options although you'll have to take care of the repainting of "dirty" regions when creating translucent images as well described by camickr in his blog Backgrounds With Transparency.

Similar Threads

  1. Draw rectangle in a frame
    By Mothrat in forum Java 2D
    Replies: 0
    Last Post: 12-13-2010, 10:52 AM
  2. Replies: 11
    Last Post: 11-29-2010, 09:40 PM
  3. Draw String in Rectangle
    By DavidG24 in forum AWT / Swing
    Replies: 3
    Last Post: 05-20-2009, 07:05 AM
  4. Replies: 2
    Last Post: 03-22-2009, 01:53 PM
  5. How to Draw a Rectangle in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:09 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
  •