Images and Transparency

Ok, so I have a program wwhich displays 2 Images, one on top of the other.
The first one (rendered first) is an image loaded from a file which is completely opaque.
The second one starts off as completely transparent, but every time you click on it, a semi-transparent square appears.
The only problem is that if you click again on the same square it adds another semi-transparent square on top, making it more opaque.
I was wondering if there is an easy way to make it draw a semi-transparent sqaure the first time I click, and do nothing the second time.
Is it possible to do this using AWT instead of keeping a record of which squares have been drawn?
Relevant code below, overlay is the image concerned and graphics is its Graphics object.
private int tileWidth = 16;
private int tileHeight = 16;
private Color fillColor = new Color(255, 255, 255, 192);
private BufferedImage overlay;
private Graphics graphics;
private int mapHeight;
private int mapWidth;
Constructor() {
     overlay = new BufferedImage(mapWidth, mapHeight, BufferedImage.TYPE_INT_ARGB);
     graphics = overlay.createGraphics();
     graphics.setColor(new Color(255, 255, 255, 0));
     graphics.fillRect(0, 0, mapWidth, mapHeight);
     repaint();
     revalidate();
public void paintComponent(Graphics g) {
     g.drawImage(map, 0, 0, this);
     g.drawImage(overlay, 0, 0, this);
public void mousePressed(MouseEvent e) {
     fillTile(e.getX(), e.getY());
public void mouseDragged(MouseEvent e) {
     fillTile(e.getX(), e.getY());     
private void fillTile(int x, int y) {
     int tileX = (x / tileWidth) * tileWidth;
     int tileY = (y / tileHeight) * tileHeight;
     if (graphics != null) {
          graphics.setColor(fillColor);
          graphics.fillRect(tileX, tileY, tileWidth, tileHeight);
          repaint();
}

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
public class ImageTest extends JComponent implements MouseListener,
          MouseMotionListener {
     private int tileWidth = 16;
     private int tileHeight = 16;
     private Color fillColor = new Color(255, 255, 255, 192);
     private File imageFile;
     private Image bg;
     private BufferedImage overlay;
     private Graphics graphics;
     private int bgHeight;
     private int bgWidth;
     private JFrame frame;
     private JScrollPane jsp;
     private ImageTest() {
          createAndShowGUI();
            * Replace FILE NAME HERE with the path of the image file.
          imageFile = loadFile("FILE NAME HERE");
          if (imageFile != null) {
               bg = loadImage(imageFile);
          if (bg != null) {
               bgWidth = bg.getWidth(this);
               bgHeight = bg.getHeight(this);
               overlay = new BufferedImage(bgWidth, bgHeight,
                         BufferedImage.TYPE_INT_ARGB);
               graphics = overlay.createGraphics();
               graphics.setColor(new Color(255, 255, 255, 0));
               graphics.fillRect(0, 0, bgWidth, bgHeight);
          repaint();
          revalidate();
     private void createAndShowGUI() {
          try {
               UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          } catch (Exception e) {
               // Can't use system look and feel.
          frame = new JFrame("ImageTest");
          frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
          addMouseListener(this);
          addMouseMotionListener(this);
          jsp = new JScrollPane(this,
                    ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
          frame.getContentPane().add(jsp);
          frame.pack();
          frame.setSize(800, 600);
          frame.setVisible(true);
     public Dimension getPreferredSize() {
          if (bg != null)
               return new Dimension(bgWidth, bgHeight);
          return new Dimension(800, 600);
     public void paintComponent(Graphics g) {
          g.drawImage(bg, 0, 0, this);
          g.drawImage(overlay, 0, 0, this);
     public void mouseClicked(MouseEvent e) {
     public void mouseEntered(MouseEvent e) {
     public void mouseExited(MouseEvent e) {
     public void mouseReleased(MouseEvent e) {
     public void mouseMoved(MouseEvent e) {
     public void mousePressed(MouseEvent e) {
          fillTile(e.getX(), e.getY());
     public void mouseDragged(MouseEvent e) {
          fillTile(e.getX(), e.getY());
     private void fillTile(int x, int y) {
          int tileX = (x / tileWidth) * tileWidth;
          int tileY = (y / tileHeight) * tileHeight;
          if (graphics != null) {
               graphics.setColor(fillColor);
               graphics.fillRect(tileX, tileY, tileWidth, tileHeight);
               repaint();
     private File loadFile(String fileName) {
          File file = new File(fileName);
          try {
               if (!file.exists()) {
                    System.err.println(fileName + " does not exist.");
                    return null;
          } catch (SecurityException e) {
               System.err.println("Access denied to " + fileName + ".");
               return null;
          return file;
     private Image loadImage(File imageFile) {
          String imageName = imageFile.getAbsolutePath();
          try {
               if (imageFile.canRead()) {
                    Image i;
                    i = Toolkit.getDefaultToolkit().createImage(imageName);
                    MediaTracker media = new MediaTracker(this);
                    media.addImage(i, 0);
                    media.waitForID(0);
                    return i;
               System.err.println("Cannot read " + imageName + ".");
               return null;
          } catch (SecurityException e) {
               System.err.println("Access denied to " + imageName + ".");
               return null;
          } catch (InterruptedException e) {
               System.err.println("Loading " + imageName + " interrupted.");
               return null;
     public static void main(String[] args) {
          new ImageTest();
}Remember to replace FILE NAME HERE with the path of an image file.
A good one to use would be [this one|http://www.vgmaps.com/Atlas/GB-GBC/LegendOfZelda-OracleOfAges-Labrynna%28Present%29.png] as that is what I am using.
Edited by: KrimsonEagl on 30/11/2009 22:07

Similar Messages

  • Import Image and Transparency

    I am importing an image (GIF) with transparency properties as a drop zone. How do i make the background of this image transparent. This image serves no purpose other than just being an image. It is not a button or has any associated actions.
    Please help - Thanks

    Don't use GIF, use TIFF instead. This will preserve the transaparency.

  • Images with transparency display edge artefacts - What am I doing wrong, how can this be resolved

    Hi there
    Using MUSE CC 2014
    I need to place photoshop images of text (with transparency) over a full screen slide show but I'm getting edge artefacts around the lettering which looks, well frankly, horrible. I don't recall previous versions of MUSE being quite as bad as this, am I doing something wrong? Is there a trick to overcome these ugly artefacts?
    PSD Images are being imported SS
    Many thanks in advance for any help.

    Hi Vikas
    I was dropping a pds file onto the stage. No other layers than image and transparent bg. I was scaling the image down in Muse which as I've discovered exaggerates the visibility of the image edge artefacts. After doing a bit of reading it appears this is a known problem with Adobe Muse, something to do with the algorithms Muse uses to scale images. I also think the colour of the layer contributed a little to the effect also.
    Problem solved when I ditched the PSD route and instead imported the image SS as a PNG.

  • Imported image and making white space transparent...

    I need to make the white space around an image transparent without having to use the clipping mask tool. In Freehand, you simply click a button and the white background disappears. I need to use that exact technique to hide the white background.
    How can I do this in InDesign?
    Thank you.

    There's quick, and there's right. They're rarely in the same space.
    To make a transparent background can be a complex and fussy process. While InDesign could include a general tool to do so, it would likely be inferior to actually making the image background transparent in a native image editing tool.
    I've found most such embedded features to be adequate only for low end work - e.g., they leave a background halo on the image, or a noisy edge, or other image problems. I really don't see it as a flaw or omission that ID forces you to use a proper tool to create the background transparency.

  • Giving a black and white line work image a transparent background

    I am working in Photoshop CS4 (mac).  Currently, the image is comprised of black line on a white background.  How do I go about giving the image a transparent background?

    A day later, I have still not figured out how to accomplish what it is I am trying to do. 
    I have not yet tried what you have suggested in the last post and am about to go at it once again.  I should mention two things.  1. My  goal is to drop this image into InDesign (cs4) when completed. 2. The image has interior space betwen the line work/image itself that I would like to contain transparency.  Not sure if I am being clear here. The issue lies in when I actually drop the image into InDesign.  While the image appears to already have a transparent background, once it is dropped into InDesign, the image then assumes an opaque/white background.

  • Watermark image w/transparent borders and raindrop in pool effect

    Hi there,
    I have learned how to place a transparent image in iMovie using Quictime Pro but I can't seem to place an image with transparent borders. What I have is an icon with rounded corners and a transparent center. The icon looks very blocky right now with the hard corners, any suggestions on how to make the edges and center transparent?
    I have seen in other videos a "raindrop into a pool" effect that I'd also like to use, does any know if there is a 3rd party plug-in for iMovie or what other video software is used to create this effect?
    Thanks

    I just figured out the transparent image problem, my source .psd was CMYK, after changing it RGB and apply the alpha transparency layer it looks great.
    Anybody know the "drop in pond" effect?

  • AI CS6, ScriptUI, and images with transparency

    ESTK for AI CS6 is already known to create problems (e.g. http://forums.adobe.com/message/4711851).
    It’s possible that, in dialogs created with ScriptUI, AI CS6 also fails to display images with transparency properly. On my (Mac) system at least, such images appear with an aliased edge.
    If this is true it’s doubly unfortunate, since with CS6 Adobe chose to make AI’s background interface colour adjustable—which makes interface images with transparency something of a requirement.
    One workaround, which sadly negates the whole business of variable interface background colour, is to create dialog images with a background colour instead of transparency, and then force the dialog to display with the same background colour. For this technique we’re indebted again to Peter Kahrel’s invaluable ScriptUI for Dummies (www.kahrel.plus.com/indesign/scriptui.html). Page 69 of that publication shows how to do this—for example:
    win.graphics.backgroundColor = win.graphics.newBrush(win.graphics.BrushType.SOLID_COLOR,[0.5,0.5,0.75]);
    The process is, to say the least, quirky: and, as Peter mentions, poorly documented by Adobe. But I hope it may be helpful to others trying to use ScriptUI.
    The more I see of AI CS6 the more I like CS4! Can anyone tell me if I’m likely to miss any features or run into problems if I develop scripts for CS6 using ESTK for CS4?

    Yes this is where things can quickly become very complex too… I for the most part script for a particular version… Im almost certain scriptUI arrived with CS3 so you will need to make checks for this… There are a couple of differences between how they look from one OS to another… Oh yes a test my script cloud… just floated past the window or was it a flying pig hard to tell… I have long thought purchasing a version of an app should qualify you access to previous versions but thats never going to happen… You may get this on some expensive developer program but not for part time scripters…
    BTW if you have an old G5 knocking about… Then you can now get CS2 FOC great thats the one I've got…

  • Missing images and not able to use copy and paste anymore - just blank rect

    Hello!
    I've a problem that really scares me.
    I use severall png and pdf images. But at one point keynote looses the media and just displays these grey rectangles instead of some images. I tried to figure out if it just happens with pdf images and replaced the missing pdf images through png24 images with transparency. It first worked fine (they showed up) and then it tells images can not be saved. If I open the presentation again, the images are missing. And it seems that the more images and slides i put in the presentation, the more images are missing…
    But there is more: I even can't copy and paste these images anymore. If I try to copy one of the images I just have replaced (because they have been lost by saving), it just pastes the blank grey rectangle instead of the image.
    This really is a problem because I now have to repetitive tasks (like same animation for the same image in the same size) over and over again. And when I save the doc – they are al gone again…
    It really scares me and I don't have an idea how to solve this.
    It also happens, if I repleace an pdf image with an png image and than try to copy this new png image. However I am able to drag the image from the Finder again.
    I am working on a PB G4, 1.5 GHz, 1 GB RAM.
    My Keynote Version is 3.0.2.
    The Keynote Dokument is saved and opened from a G4 X-Serve wich is only used by Macs (so no Windows Filesystem anywhere). I still have anough space on both, my local and the server volume. I don't transfer the Keynote document. So it should not be a problem with the names on the files… And the files are just exported from Adobe Illustrator (png 24 files with "save for web" and the pdf files are saved as Illustrator pdf) – so they should really be fine and not corrupted…
    At this time the Keynote document has a size of 179 MB (because of a video) and only 16 slides. And the theme was built from scratch – so no import from Powerpoint or so…
    I looked through the Keynote preferences and found a thing like (sorry self-translated from german version to english) "scale placed images to slide-size". I wondered if keynote saves a copy of the sacaled image after that and corrupts the image itself? But I don't think so.
    I've had these problems with earlier versions of Keynote, but it occured just sporadically and I could solve it through pasting it again. But this time I can not solfe it anymore.
    Does anybody encounter same problem? Or has a hint?
    I really would appreciate, because I have to finish the presentation very soon and are not able to work properly with that. Furthermore, I am scared of saving and opinging the presentation again, because the lost files can not be replaced…

    I now have a hint:
    As a colleague said, he encountered some problems with usagerights on our fileserver (XServe). He said he wasn't able to read files he put on the server himself again…
    If this is true, the same could have happened to Keynote for placed images, as Keynote writes these images as separate files in the doc package…
    I will try to work on my local volume and see if it solfes the problem.
    But sooner or later I will have to place the whole thing on the server again…
    Do you think this is the reason?

  • How do I save an image (logo) transparent so that it will appear transparent on any color background such as a gradient or black?

    How do I save an image (logo) transparent so that it will appear transparent on any color background such as a gradient or black?
    I save image as "transparent" at File > New... but when I place on a gradient background it keeps appearing white. I'm trying to save transparent to be effective on any color such as a gradient or black.  How do I truly save as transparent?

    Put the logo on its own layer set the layer fill to 0 and add a layer style to the logo layer.  Here I duped one change fill to 0 added style.

  • How do I take an object from an image and save it so I can use it in other images?

    I am trying to figure out how to take an object from an image and save it so I can re-use it again in other pictures?  I know how to use the magic wand to select the object and place it in a blank image.... but I do not know how to re size the selected object or keep it for later use.  Basically I would like to use the object the same way that I use the graphics ... so I guess I am wondering if there is a way that I can make my own graphics... I have Elements Photoshop 13.  Thanks for any help/advice....  Jerrie

    Hi Jerrie,
    You can re-size using Transformation tool. Ctrl + T will give you a box to re-size the image or object.
    You can save these object as PNG file with Transparent layer. You can use these objects later in another projects.
    Regards,
    Sandeep

  • Printing Images with Transparent Background

    Sorry if this question has already been asked, but I couldn't find anything similar to it in a search through the archives.
    I have a couple of images with transparent backgrounds in a document, much like the shells and the blue flowers in the "Classic Brochure" template in Pages 2.0.1. These images show up perfect on the screen, and when printed as .pdf files, but when I print to either an hp color laserjet 4600 or an hp laserjet 1320, the images show up with a clear box around them; the colors under the box (where it should be transparent) are faded and slightly blurred. This happens both with my document, and the unaltered "Classic Brochure" template. I've tried moving various objects backwards and forwards, but it doen't seem to have any effect. It even happens with one of the text boxes I have in the document.
    Another odd thing is that the printed "box" isn't the same size as the box that appears when you click on the object in the document. It's a bit bigger. I've seen other posts about problems with hp printers, but I'm not sure it's related. Any insight would be appreciated.
    MAF
    iMac Intel Core Duo   Mac OS X (10.4.7)  

    http://indesignsecrets.com/eliminating-ydb-yucky-discolored-box-syndrome.php
    Bob

  • Framing image with transparent background png frame

    hi,
    i'm trying to find a way to frame images with transparent background png frames...
    what i'm doing now is
    1-drawing my image on a panel
    2-creating a 2nd image using the frame's filename, stretching this 'frame-image' to a size slighlty larger than that of my main image and drawing the 'frame-image'
    the problems with this method are:
    1-depending on the width of the frame, the frame sometimes hides parts of the image (thick frame), and sometimes there is a gap between the frame and the image (thin frame).
    2-if the image file containing the frame is larger than the frame (Ex: The image is 300x300, the frame is centered in this image and is 200x200; as opposed to the image is 200x200 and the frame takes up all the space), when i position the 'frame-image' near the top left corner of the image i want to frame, the frame appears at the wrong place (shifted down and to the right). This is due to the fact that i'm placing the top corner of the created 'frame-image' and not the frame, who is not in the top corner of my 'frame-image'.
    Is there a way to do what i'm trying to do???
    My ideas (which i don't know how to achieve are)
    1-To 'analyse' my transparent background png file and
         1-only keep the frame,
         2-calculate the frame's thickness
    OR
    2-Let java do the analyzing for me and tell it to frame my image with the frame in the png file
    please feel free to ask for more explanations if my description/question is confusing,
    thanks.

    Have you looked into the Border interface? If what you really want to do
    is put a custom border on a component, you may be able to do it this way.
    Anyway, here is some code that stacks and centres 2 images. It's not hard to do.
    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import java.net.*;
    import javax.imageio.*;
    import javax.swing.*;
    public class Example extends JComponent {
        private BufferedImage backgroundImage;
        private BufferedImage foregroundImage;
        public Example(BufferedImage backgroundImage, BufferedImage foregroundImage) {
            this.backgroundImage = backgroundImage;
            this.foregroundImage = foregroundImage;
        public Dimension getPreferredSize() {
            int w = backgroundImage.getWidth();
            int h = backgroundImage.getHeight();
            return new Dimension(w, h); //assuming this is bigger
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int w = getWidth();
            int h = getHeight();
            //paint both, centred
            int x0 = (w-backgroundImage.getWidth())/2, y0 = (h-backgroundImage.getHeight())/2;
            g.drawImage(backgroundImage, x0, y0, null);
            int x1 = (w-foregroundImage.getWidth())/2, y1 = (h-foregroundImage.getHeight())/2;
            g.drawImage(foregroundImage, x1, y1, null);
        public static void main(String[] args) throws IOException {
            URL url1 = new URL("http://weblogs.java.net/jag/Image54-large.jpeg");
            URL url2 = new URL("http://weblogs.java.net/jag/DukeSaltimbanqueSmall.jpeg");
            JComponent comp = new Example(ImageIO.read(url1), ImageIO.read(url2));
            final JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(comp);
            f.pack();
            SwingUtilities.invokeLater(new Runnable(){
                public void run() {
                    f.setLocationRelativeTo(null);
                    f.setVisible(true);
    }

  • Images with Transparency Cause Rendering Bug in Acrobat

    Try this at home: Export a file containing an image with transparency to PDF and open it in Acrobat (any platform, any recent version): the page containing the image will have all text rendered in what looks like bold or faux bold, depending on the font. The document looks fine in Preview and printed (even from Acrobat).
    Can anyone else reproduce this? I get this behavior consistently.

    Hi bdepaepe, I've had a look and I believe I have experienced the error you mean and solved it.
    The reason this occurs some of the time, but not always is because the source code is not the same each time, as the adverts change every refresh. The error only happens when two of the < div> tags of class 'banneritem' which are on top of each other have a height of 84px each as opposed to 80px (although even if this is the case the rendering issue doesn't always happen).
    The reason that some of the 'banneritem' < div> tags have a height of 84px is because inside them there is an <a> tag which contains an < img> tag, and the <a> tag has not been set to 'display: block;'. <a> tags are inline elements by default, so shouldn't be used to hold block level elements such as images unless the styles for the <a> tag include 'display: block;'.

  • Export a PDF to an image with transparency

    Hello,
    I'm trying to export a PDF (which contains a logo with 4 paths) as an image with transparency. In my case I use the PNG because it supports the transparency. As my logo is defined with paths it should be possible to create an image with transparency.
    Maybe this is a limitation and Acrobat cannot create image wich supports transparency.
    Anyone have an idea?
    Thanks in advance,
    Joe

    @Joe:
    That's a really pity.
    Have you found any other software which can do this job.
    I searched yesterday but didn't find anything.
    @Doc Maik
    b)
    I have a (lot) PDF containing a single image which has a transparent background.
    You can check that in Acrobat by menu: Edit - Preferences - Page Display - Show transparency grid
    I need to have a PNG or TIFF (these two formats allow transparency) of that image.
    That's all but as it seems to much for Acrobat to handle.
    "Save as ..." doesn't work in case of transparency!
    Thanks anyway.
    Best regards
    Norbert

  • Using images with Transparency in Photobooks

    I'm to a point where I would like to use images that utilize transparency in Aperture photo books. Is this possible? So far, PSD files that I've cropped with PS's lasso tools to have transparent areas surrounding their subject tend to change this area to white when dropped into a book.
    Does anyone know of a way or format to use to retain this transparency? I love the speed of Aperture's photobook tools, along with export and printing options, and would like to be able to layer images that bleed together, or have a cut-out subject on top of others, but I haven't noticed a way as of yet.
    Is this ability even available in Aperture yet?
    If not, is there a third party plugin that gives it to the user when making photo books?
    Please let me know soon, else i'm going to have to adjust my workflow back to photoshop only for page set layouts and the created PSD files on blank book pages- not fun.

    i have been trying different ways to make it work fine, and find several things to consider:
    Note: for the next ways i tried with images with transparency.
    1 searching images in the iPad with safari and saving it into Photo app
    then copying it with the popup and pasting it with the popup inside Pages or Keynote:
    work fine.
    2 using the saved images that worked fine in the first way but
    then inside Pages in my opened document, using the menu to import the image from the
    Photo app, to insert the same downloaded image used in the first case:
    does not work, the image appears with a white background.
    3 trying the first way (manually copying and pasting) but with the images i imported from
    my MacBook Pro using the normal way via iTunes(10.1.1 (4))
    does not work, some images appears with a black background, other images
    appears with a white background.
    4 trying with the images of the third way, but changing the method using the photo app
    menu from Pages
    does not work as in the third way
    i dont have problem using transparency images in the first complex way.
    my problem is when i try to use images with transparency in the other three normal ways.

Maybe you are looking for

  • Referencing a field from a resource table to a task table

    Hello, I have created a a resource field that is called "Internal Cost Rate" and I would like this field to be added to a Task table so that I can then reference it and make a simple calculation. For example in the Task Table I have the "Work Time" a

  • Use of domain administration port breaks session access?

    WLS 8.1.2;           We have a third-party app deployed in a pretty basic cluster setup (two managed servers, each on a separate machine). When accessing the main web app, it works fine. If/when we enable the domain-wide administration port (DAP)(aft

  • Oracle Module

    Hii :) I'm bit new to oracle apps, just wondering if anyone can help me to learn SCM module(i.e,PO,OM INV). Actually, i want to know how i can learn or practice INV, OM, PO module. Any help will b appreciated. Regards Royal

  • Job created in DB control

    Hi, in 10g R2 I created a daily job whose owner is SYS. In which table it would be registered ? I can not see it in DBA_jobs. thanks.

  • Elements 12 without Photomerge Panorama on MAC?

    After installing Elements 12 on the MAC (10.8.5) the menu for Photomerge Panorama is greyed out, so Photomerge Panorama cannot be started. Any other Photomerge function is available and can be used. Is the panorama one of the missing function on MACs