Black background of saved image file.

Hello All,
My code is set up to create an image then save the image into a file in JPEG format. Everything works well except the background of the saved image is balck. How can I get a white background on the saved file?
I have posted my code below. I have allocated 10 duke dollars for the person solves who this? Thank You!!!!
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;
import java.io.*;
import java.awt.image.BufferedImage;
public class Test2 extends JApplet {
public void init() {
//Initialize drawing colors
setBackground(Color.white);
setForeground(Color.black);
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Dimension d = getSize();
int gridWidth = d.width / 6;
int gridHeight = d.height / 2;
// fill Rectangle2D.Double (red)
g2.setPaint(Color.red);
g2.fill(new Rectangle2D.Double(100, 100, 100, 100));
g2.setPaint(Color.black);
g2.drawString("My Rectangle", 125, 85);
public static void main(String s[]) {
JFrame f = new JFrame("Test2");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
JApplet applet = new Test2();
f.getContentPane().add("Center", applet);
applet.init();
f.pack();
f.setSize(new Dimension(400,300));
f.show();
saveAsJPEG(applet, 1.0F, "myJPEG.jpg");
     public static void saveAsJPEG(Component component, float quality, String filename)     {
          try          {
               Dimension d = component.getSize();
               BufferedImage bimage = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB);
               component.paint(bimage.createGraphics());
               Graphics2D g = (Graphics2D)bimage.getGraphics();
               g.setPaint(Color.white);
               if(!filename.endsWith(".jpg"))
                    filename += ".jpg";
               OutputStream out = new FileOutputStream(filename);
               JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
               // Create JPEG encoder
               JPEGEncodeParam jpegParams = encoder.getDefaultJPEGEncodeParam(bimage);
               jpegParams.setQuality(quality, false);
               // Set quality to (quality*100)% for JPEG
               encoder.setJPEGEncodeParam(jpegParams);
               encoder.encode(bimage);
               // Encode image to JPEG and send to browser
               out.close();
          catch (Exception e)          {
               System.out.println(e);
}

I've added 2 lines to your paint, it works now.
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Dimension d = getSize();
int gridWidth = d.width / 6;
int gridHeight = d.height / 2;
// make a white background
g2.setPaint(Color.white);
g2.fill(new Rectangle2D.Double(0, 0, d.width, d.height));
// fill Rectangle2D.Double (red)
g2.setPaint(Color.red);
g2.fill(new Rectangle2D.Double(100, 100, 100, 100));
g2.setPaint(Color.black);
g2.drawString("My Rectangle", 125, 85);
}

Similar Messages

  • Grey background while saving pdf file as a ZIP in IE browser

    I am getting grey background while saving pdf file as a ZIP file in IE browser like below screen shot
    The pdf open properly but  when I click on save button on below screen it become grey.
    The file is getting saved properly.
    But the same thing working fine in firefox(I will not become grey while saving)
    we are using adobe 11 version.

    I am getting grey background while saving pdf file as a ZIP file in IE browser like below screen shot
    The pdf open properly but  when I click on save button on below screen it become grey.
    The file is getting saved properly.
    But the same thing working fine in firefox(I will not become grey while saving)
    we are using adobe 11 version.

  • Black background on imported images

    I have imported a few images that have blank backgrounds (.png images for instance), and iPhoto automatically puts them on a black background. This is fine except for when the image is black, and in that case, you can't see the image. Can I change the background color?

    hotwls:
    Welcome to the Apple Discussions. Those png files have transparency layers and which show up as black when displayed in iPhoto. When added to other documents or printed the background of the paper or other documents will show thru. To change them export them to the desktop changing the format to jpg and the transparent layer will change to white.
    The other formats that can contain transparency layers are gifs and Photoshop.
    TIP: For insurance against the iPhoto database corruption that many users have experienced I recommend making a backup copy of the Library6.iPhoto database file and keep it current. If problems crop up where iPhoto suddenly can't see any photos or thinks there are no photos in the library, replacing the working Library6.iPhoto file with the backup will often get the library back. By keeping it current I mean backup after each import and/or any serious editing or work on books, slideshows, calendars, cards, etc. That insures that if a problem pops up and you do need to replace the database file, you'll retain all those efforts. It doesn't take long to make the backup and it's good insurance.
    I've created an Automator workflow application (requires Tiger), iPhoto dB File Backup, that will copy the selected Library6.iPhoto file from your iPhoto Library folder to the Pictures folder, replacing any previous version of it. It's compatible with iPhoto 08 libraries and Leopard. iPhoto does not have to be closed to run the application, just idle. You can download it at Toad's Cellar. Be sure to read the Read Me pdf file.

  • Question: Saving Image Files In The Repository

    Good Morning all,
    (Running CR2008)
    I have a few image files that I have saved in the Crystal Repository. It works nicely and I've tested it pretty thoroughly to make sure that connected images update across the board. It's a convenient little feature.
    Here's my question ... when you save an image file to the repository, it automatically converts the image file to a Bitmap file type. Bitmaps greatly increase the size of whatever .rpt file uses the iimage. My standard .rpt files go from about 240K to 3.4 megs. This isn't really the biggest deal in the world, but I was wondering if there is a setting that I can check that will have the repository files automatically save in JPEG format, or just keep thier original format (JPEG in this case).
    Any thoughts? Or are we stuck with the Bitmap format? If so, this might be a good subject for a 'feature request' post.
    Thanx in advance for any help.
    -TS
    Edited to add: Sorry about the double-post. I have no idea why that keeps happening. -TS
    Edited by: Tom Salvato on Jan 19, 2010 2:45 PM

    Stuck with BMP

  • Black background when scaling images with transparent background

    I am implementing Zooming functionality for reports which have some icons. I am trying to scale icon by scaling its image. But the problem I am facing is that the icons have transparent background and the scaling puts a black background in place of transparent. Is there any way to maintain its transparency or better yet, Is there anyway to put a white backgound instead of the black one that is created by default ? Here is my code
         public static Icon getScaledIcon(Icon icon){
             if(icon instanceof ImageIcon){
                  Image outImage = ((ImageIcon)icon).getImage();
                      return new ImageIcon(getScaledImage(outImage));
             }else{
                  Image outImage = new BufferedImage(icon.getIconWidth(),
                            icon.getIconHeight(),
                            BufferedImage.TYPE_INT_ARGB);
                    JComponent comp = new JComponent(){};
                    icon.paintIcon(comp, outImage.getGraphics(), 0, 0);
                    return new ImageIcon(getScaledImage(outImage));
        public static Image getScaledImage(Image image){
             image = image.getScaledInstance((int)(image.getWidth(null) * getScale()),
                           (int)(image.getHeight(null) * getScale()), Image.SCALE_SMOOTH);
            return image;
        }I haven't done much image manipulation , so I would really appreciate any help or advice.

    I have also not done much of image work. But I guess you could achieve your functionality of showing scaled image in the screen by not scaling the image and show it, instead scale the image in paint method by calling g.drawImage giving the height and width. But use high resolution images so that scaling is good.
    If you do in this manner, you could give any colour you want as background to fill the transparent area or leave it as such.
    Regards,
    techece.

  • Saving image files - TIFF file extension wasn't acceptable

    Hello! 
    When I save an image on my iMac the default situation appears to be that the file has a TIFF file extension.
    Please review the post I recently made here:     http://www.wilderssecurity.com/threads/why-are-tagged-image-file-format-tiff-fil es-unacceptable-here.370185/#post-2426799
    I should be grateful if someone can shed some light on my conundrum/confusion.  It should be self-explanatory!
    Any help will be gratefully received!
    David B.

    Hello again   So sorry for the delay in replying to folk for the helpful responses.
    If one re-visits my original link one can view a response - in essence, look here:-   https://answers.yahoo.com/question/index?qid=20090125135844AAwqjkB
    After some reflection, I decided to do nothing at all!  However, today I looked under 'Grab' preferences and found that there is no way to change the image format of a screen capture. BUT ..... I then looked under 'Grab Help' and found this ....
    Save screenshots:  After taking a screenshot, choose File > Save, enter a name, add a tag (optional), then choose a folder. Screenshots are saved as TIFF files.
    Convert a screenshot to another format:  Open your screenshot in the Preview app. Choose File > Export, click the Format pop-up menu and choose a format, then click Save. For more information, see Convert graphics file types.
    So I now know what to do if/when I need to switch to another format! 
    Many thanks to rkaufmann87  and Frank Miller2 - your guidance is much appreciated,
    David B.

  • Saving Image Files on Disk after drawing lines on the Image

    Hi guys,
    I am presently working on a swing project in which I need to draw lines on a image and then store the modified image in .gif, .jpg or .bmp file format.
    I can't seem to find an documentation dealing with this situation.
    Any help will be highly appreciated
    Abhi

    Ive just been looking for the same. Theres an article (and a class) on javaworld on subject of saving bitmap-files, and in another thread in this forum, a guy described how to make jpg. with the jdk1.4 .... Like this:
    int w = comp.getWidth();
    int h = comp.getHeight();
    BufferedImage im = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = im.createGraphics();
    comp.paint(g2);
    g2.dispose();
    ImageIO.write(im, "jpeg", new File("foo.jpeg"));
    im.flush();
    Im just referring, havent tried it yet. Good luck.
    Stig.

  • Saving/publishing site fails - Error in saving image files

    A friend has created a simple portfolio site for his artwork (about 80 jpegs) but when he goes to save (or publish) iWeb says it can't save some of the images and shows a list of the ones it can't save and stops. I believe that the problem is related to image file name length. Is there a restriction on file length? If not has anyone seen error this before?
    If it is file length the he will have to rename the files he moved in iWeb from iPhoto. I understand that it's impossible to change file names in iPhoto. Is this true?
    Thanks
    He is running iPhoto 6 on an iMac PPC 20" 10.5.4

    Hi Rob,
    Coming to your error, it’s really strange. You can able to access/use your WCF service for a while and after sometime you get error. Few things which I would recheck are as follows:
    Make sure it’s not due to your active directory. I would check if there are any issue with AD after an elapsed period of time to valid my app pool credentials.
    In your Application-pool, under process model, try by increasing the “Idle Time-out (minutes)”. This time decides the amount of time worker process will remain idle before its shutdown.
    I personally never required to change it from default value-20 mins, but looking at your issue, it’s worth trying this.
    In your Application-pool, under CPU, Check the setting “Limit Action” to “KillW3WP”. Default is “NoAction”. “KillW3WP” setting potential could have impact like yours. If so change it to
    “NoAction”.
     When you create a WCF service using wizard, I expect to have default values; check any setting at IIS level is having an impact.
    Few things I would try:
    Create a separate App pool for this WCF service and check the impact
    Reapply the credentials of you app pool. Just simply go to the “Identity” section and reapply the same valid credentials. This could clear any cache related things to do
    with the credentials/AD validation.
    I understand you’re overwriting the service/virtual directory while creating the SCF service in Wizard. Try manually deleting the virtual directory folder and create the
    WCF server as fresh using Wizard, without using the overwrite checkbox.
    Hope the above suggest helps.
    If this answers your question please mark it accordingly. If this post is helpful, please vote as helpful by clicking the upward arrow mark next to my reply.

  • Black background in transparant images

    Let me 1st explain what is happening.
    1/ Place a Photoshop image with transparent background in Illustrator.
    2/ Save a 1.5 pdf (or higher).
    3/ Open the pdf in Acrobat, select the image, copy it.
    4/ Open Photoshop, make new image and past the image you copied b4 from Acrobat.
    You will see that the transparency is filled in with a black color.
    Why is this?
    And can this be changed, so i can have a whit background?
    Thanks

    I don't imagine you will be able to copy and paste an image with
    transparency. Maybe using the Object Touch-up Tool in Acrobat Pro to
    open the image directly in Acrobat will preserve the transparency, but
    not if it was done with a clipping path.
    Aandi Inston

  • Saving image file as binary into database + pulling it out + displaying

    Hi guys, this is exactly I have to do, and have no ideal how to do.
    It will be greatly appreciated if you can offer help on this.
    I don't have a problem putting it into and pulling it out fo the database, it is really the jpg-> binary, and binary->display part that I need help on.
    Btw, I am currently using "draw Flatten PixMap.VI" to display a picture in labview, from a jpg file. Can I still use this VI to achieve what I intend to do?

    Have you tried this:
    Saving a binary file to a database with the Database Connectivity Toolset
    Adnan Zafar
    Certified LabVIEW Architect
    Coleman Technologies

  • Mass background upload of image files

    Hi! We have more than 100000 images to upload into the repository. Is there an easier way to do mass upload through the Data Manager?
    Cheers!
    SF

    Hi,
    I am trying to assign multiple images to a single catalog item through MDM. Cannot get it working for multiple images. Though I can assign the default image to a catalog item but cannot assign multiple images at once. My excel looks like this
    Catalog Item ID
    Node Id
    Image
    ProdA
    NodeA
    CMLProdImg.bmp
    ProdB
    NodeA
    ResProdImg.bmp
    ProdC
    NodeA
    product_pic_2.bmp
    ProdD
    NodeA
    24XXXX_img.bmp
    I am matching records based on catalog item id and node id. But in maching record selection I can only see catalog item id as available. Node ID is from a hierarchy table I think.
    Any pointers would help much

  • WORD doc x files show lettering changed from black to white with black background, colors have black background

    WORD doc x files received open with black lettering changed to white with black backgrounds. Color images have black backgrounds. There is no issue for non MAC users. How can I open the document as sent? Thank you.

    Pages (any version) is not a word clone, and may not digest Word documents as accurately as their native Microsoft application. Pages v5 is also far from feature/functionality complete, and that only compounds this issue.
    Suggest you download/install the free LibreOffice and open the Word documents with it. Do the artifacts persist in LibreOffice?

  • How to remove black border around image files?

    I'm something of a beginner with Photoshop, so forgive me if I've missed something entirely obvious: for some strange reason I'm now getting a black border around all image files that I save through the "Save for Web & Devices" panel. Does anyone know how I can save without creating this border? It looks rather unsightly when I load it into a client site. The file in question is a .png, but I seem to be getting the same effect with all other image files as well.
    Thanks in advance,
    Shane

    One cause could be having the matte color set to something other than none.
    Is that a png-8?
    MTSTUNER

  • Problem: Light color shadow on top of black background when printing

    Using InDesign CS3, PC.
    I have a book cover design that looks perfect on screen. There is a section with a black background and colored letters on top of it. Also on top of this black background is an image of a dinosaur (made in illustrator CS3). The image was placed into the InDesign file and does not have any color to the background. But when it is printed (Xerox Phaser), there is a colored shadow where the image overlaps the black background section.
    There are also colored areas over other black background sections where drop shadows (35%) occur on top of the black background.
    Could anyone tell me what I am doing wrong? I have never dealt with 100% black in my designs before and I must be doing something wrong.
    Again, on the screen you cannot see any of these colored areas on top of the black background. It's only when I print that they show up.
    Thank you,
    Cathy

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

  • How to convert a Image to an image file...

    Hi,
    I load an image file in my application and I am trying to run it through a filter and save it without modification.
    So this leaves me with an Image object, which I would generally assume is easy to convert to a saved image file... But its not. It seems I need a RenderedImage to do this so I created this function
    public static RenderedImage convertToRenderedImage(Image img,
                  int wid, int ht, int type){
    BufferedImage bi = new BufferedImage(wid, ht, type);
    Graphics2D g2d = bi.createGraphics();
    g2d.drawImage(img, 0,0,wid, ht, null);
    return bi;
    }which actually leaves me with an image without content... it appears to be all background color, and no foreground text.
    any ideas on how to make an Image into a saved Image file?

    ImageIO.write?

Maybe you are looking for