To get Image or get graphics for any JComponent

My requirment is that I need to get the Graphics object for any JComponent.
I used createImage() method and tried and used getGraphics() method for the image, Iam getting NullPointer Exception.Since createImage() is giving a null value.
Help urgently needed.
Code looks something like this:
tSource is the Component iam passing to the method.Iam getting the component correctly.
JPanel compPnl = (JPanel)tSource;
Image tImage = compPnl.createImage(compPnl.getWidth(), compPnl.getHeight());
Graphics tGraph = tImage.getGraphics();
Iam getting Null value for tImage.Help needed very urgently.
Thanx in advance

Try this: (include rt.jar in classpath when compiling and running)
import com.sun.image.codec.jpeg.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
public class Image
     public static void main(String[] args)
          JFrame theGeneratorFrame = new JFrame("Raa");
          theGeneratorFrame.getContentPane().setLayout(new FlowLayout());
          JButton theButton = new JButton("Hi there!");          
          theGeneratorFrame.setVisible(true);
          theGeneratorFrame.setVisible(false);
          theGeneratorFrame.getContentPane().add(theButton);
          theGeneratorFrame.pack();
          BufferedImage theImage = (BufferedImage) theGeneratorFrame.createImage(theButton.getWidth(), theButton.getHeight());
          theButton.paint(theImage.createGraphics());
          JButton theSecondButton = new JButton("Bye!");
          theGeneratorFrame.getContentPane().add(theSecondButton);
          theGeneratorFrame.pack();
          BufferedImage theSecondImage = (BufferedImage) theGeneratorFrame.createImage(theSecondButton.getWidth(), theSecondButton.getHeight());
          theSecondButton.paint(theSecondImage.createGraphics());
          try
               FileOutputStream theStream = new FileOutputStream("button.jpg");
               JPEGImageEncoder theEncoder = JPEGCodec.createJPEGEncoder(theStream);
               theEncoder.encode(theImage);
               theStream.close();
               theStream = new FileOutputStream("secondbutton.jpg");
               theEncoder = JPEGCodec.createJPEGEncoder(theStream);
               theEncoder.encode(theSecondImage);
          catch (Exception e)
          {e.printStackTrace();}
}The key in this one is that I add the button to the JFrame and call pack(), because otherwise the button doesn't know its dimensions, and they will remain 0. It doesn't matter that the JFrame is invisible, since I have already shown it once, then hidden it again. (the two setVisible() ones in a row).
This approach should be usable on any component.
Just ignore the JPEG encoder bit; I just chose to save the images to file as proof of concept...as so many have already pointed out, the stuff in rt.jar shouldn't be used this way.
HTH,
F

Similar Messages

  • We are not getting email for any of the two new profiles in Production environment

    We are not getting email for any of the two new profiles.
    The new rules for sending e-mail work when using the initial profile.
    Only works for F9 and choose manually the profile is being sent.
    But siebel writes the log of both the \ BIN \ sent as if it had sent.
    operation:
    Standards (Policies) were created in the Activities (S_EVT_ACT) and Contacts table (S_CONTACT). When Standards (Policies) are reached trigger actions (Actions). These actions are linked to sending emails.

    The SMTP Adapter don't just "through away" messages. If your Tracked Service Instances shows that a message was sent through the Port, I am pretty sure that a mail was indeed sent. 
    -Are you using Static- or Dynamic Port?
    Can you try to set the Send Port in "Stopped" state, and then send a message to it. Examine the message suspended in BizTalk, and if everything looks ok, then start the Port again and check that the message has disappeared from both suspended and running
    instances in the Group Hub.
    If so, quickly check the mail box, make sure that no spam-filters etc. are messing up your test.
    Morten la Cour

  • Do I get charged for any uploading (possibly even from my web host) when people download a podcast that I have created?

    Do I get charged for any uploading (possibly even from my web host) when people download a podcast that I have created?

    Not as far as iTunes is concerned: hosting a podcast there is free. However the media files are kept on your server - your ISP or a hosting service - and there may be a limit to bandwidth. If you podcast turned out to be extremely popular then the number of downloads might exceed the allowable bandwidth from your server in a given period and lead to an extra charge or possibly blocking until the period was over: this is something you would have to check with them.

  • FM to get Deliveries for any Shipment

    Hallo,
    Is there any function module to get all the delivery no's of a particular shipment?
    Regards,
    Bhaskar

    Hi Bhaskar,
    Use FM RV_SHIPMENT_VIEW
    Here Set the values of OPTION_ITEMS as X
    And you will get all deliveries for that shipment as VBELN in table F_VTTP.
    Hope this will help.
    Regards,
    Nitin.

  • IPod isn't getting notifications for any apps

    So before you all suggest I check that the notifications are on, and the badges, sounds and such are on, they are all. My problem is that they are all on, and I don't get any notifications. Which is riduculous. I've deleted apps, and redownloaded them. I've synced my ipod, restarted it, restored it, everything. This only starting happening a week ago, and I wonder what cause this. I really need help because it is seriously irritating me as I don't receive notifications for incoming messags/ posts on facebook, replies from twitter, game notifications and when I'm using ebuddy, it doesn't notify me of incoming messages. Express lane isn't working right now either. HELP ! Desperate !

    chances are in the bios it is set to allow booting from a USB/firewire port.
    Soooo when the IPOD is connected on start up it tries to boot from it.
    Go into the start up bios and disable booting from firewire/usb.

  • I have two iPod videos and one iPod touch, 1st generation. The internal battery died in the iPod videos, and the memory in the iPod touch is too little to put all my songs on it. Can I get credit for any of these iPods or possible get a reconditioned iPod

    ?

    The only "credit" from Apple for used iPods is the Recycling Program, which gives you 10% off the purchase of a new iPod (at an Apple Store).
    There are third-party businesses, such as Gazelle.com. that pays for used iPods.
    Apple offers "certified" refurbs, with full warranty, at a reduced price
    http://store.apple.com/us/browse/home/specialdeals/ipod

  • Getting path for any software while installing runtime

    Hi,
    I'm trying to install software using java program
    (using Runtime.getRuntime().exec()). I want to capture the path where it is installing. Anybody have any ideas regarding this pls welcome. Thank you. Bye.
    JK

    Hi
    I have been involved in project which is dependent on Runtime.getRuntime().exec().My learning from that experience is,as soon as u execute Runtime.getRuntime().exec() Java looses control.It will have control only on the current thread processes which it had started,but not over the excecuted process.
    regards

  • How to get notification of new post  for any discussion topic

    hi,
    i ve started a discussion in portal and i want to get notification for any new post to that discussion topic in my uwl....can anybody help me
    thanks in advance
    Message was edited by: Amit Pandey

    Have a look at this:
    http://help.sap.com/saphelp_nw04/helpdata/en/fc/b03a381dc88d49b1f509cde90a27d1/frameset.htm
    This should help solve it!

  • Setting a fixed desktop background for ANY data projector

    Any ideas on how I may set the desktop of ANY randomly selected and attached data projector (or external monitor/HDTV) to a specific image (containing my my corporate logo) without resorting to turning on mirroring to display the main desktop on my MBP?
    I run strategy workshops for my clients at venues ranging from client boardrooms to hotels and convention centers.  In each instance, I am provided a data projector which I simply connect to my MBPT, fire up Keynote and it's show time!  Never had a failure to present.
    However, in the odd moment when I am doing things at the desktop level, I want the image displayed on the projected screen the audience sees to be that of my corporate logo.  They do not need to see what I am doing on my MacBook.
    So how can I set a default image to be used for any attached display device ONCE as a single setting for all future (unknown and randomly selected) data projectors that may get attached to my MBP?
    Currently, on every occasion, I have to remember to get the pointer way across to the destop of the attached projector then right-click and manually “Change Destop Background...” etc.  It is just one niggle I would like to take out of my ‘seting up for showtime’ routine.

    Hi coocooforcocoapuffs
    I am hoping there is a way around this...
    Otherwise this still remains as two steps on my 'Presentation Check-list' which I would rather see changed from:
    Select “Change Destop Background...” on presentation screen and set to mylogoimage.jpg
    Ensure mylogoimage.jpg is displayed on screen - fix if not
    To:
    Ensure mylogoimage.jpg is displayed on screen - fix if not
    It would just take one more thing off the critical path at the very moment when time is of the essence.
    Isn’t there a plist or something I can set?

  • Inserting dynamic image doesn't work for range of records

    Hi,
    I'm using BI Publisher 5.6.3 and E-business Suite 12.1.3.
    I have set up my RTF template so that it brings in a logo image dynamically onto an invoice.
    The code in the "alt text" field behind my dummy image is: url:{concat('${OA_MEDIA}/',//CF_LOGO)} 
    The image is within a repeating loop that loops around the invoices chosen.
    It works fine if I run the concurrent request for a single invoice - the correct logo image is shown.
    However if I run the concurrent request for a range of invoices which should contain a mixture of both logos it doesn't work and prints the same logo for each invoice regardless of what it should be displaying.
    For testing purposes I also output the name of the image, field CF_LOGO (which is calculated in a formula in the RDF file) just before the actual image, and this always displays the correct value even for a range of invoices. As you can see above this is the field that I include in the code to obtain the image.
    Does anyone have any idea why a range of invoices will only display one logo image?
    Many thanks for any suggestions.
    Regards,
    Hazel

    It's a bug in MDM 5.5 SP04.  The fix is in MDM 5.5 SP05.

  • I am having problems with the Preview application.  When I try to use it to open a pdf document it gets hung-up and I have to select force quit to close it.   Any ideas on how to resolve this problem?  Thanks for any help

    I am having problems with the Preview application.  When I try to use it to open a pdf document it gets hung-up and I have to select force quit to close it.   Any ideas on how to resolve this problem?  Thanks for any help

    Can you open the Preview program without loading a file, like by itself?
    If it doesn't load then I suspect a corrupt Preview preference file.
    Deleting the System Preference or other .plist file
    Can you open other files with Preview, like jpg's and images?
    How about other PDFs? or is it just that one you have downloaded?
    Run through this list of fixes
    Step by Step to fix your Mac

  • Please help me!!!! my macbook pro 15 running on mountain lion is hanging a lot...sometimes when i use any app than it get freeze for some seconds...what should i do?

    please help me!!!! my macbook pro 15 running on mountain lion is hanging a lot...sometimes when i use any app than it get freeze for some seconds...what should i do?

    Hardware Information:
              MacBook Pro - model: MacBookPro8,2
              1 2.2 GHz Intel Core i7 CPU: 4 cores
              4 GB RAM
    System Software:
              OS X 10.8.2 (12C60) - Uptime: 0 days 1:17
    Disk Information:
              TOSHIBA MK5065GSXF disk0 : (500.11 GB)
                        disk0s1 (disk0s1) <not mounted>: 209.7 MB
                        Macintosh HD (disk0s2) /: 499.25 GB (424.92 GB free)
                        Recovery HD (disk0s3) <not mounted>: 650 MB
              HL-DT-ST DVDRW  GS31N 
    USB Information:
              Apple Inc. BRCM2070 Hub
                        Apple Inc. Bluetooth USB Host Controller
              Apple Inc. Apple Internal Keyboard / Trackpad
              Apple Inc. FaceTime HD Camera (Built-in)
              Apple Computer, Inc. IR Receiver
    FireWire Information:
    Kernel Extensions:
    Problem System Launch Daemons:
                     [failed] com.apple.coresymbolicationd.plist
    Problem System Launch Agents:
                     [failed] com.apple.afpstat.plist
                     [failed] com.apple.AirPlayUIAgent.plist
                     [failed] com.apple.coreservices.appleid.authentication.plist
                     [failed] com.apple.rcd.plist
    Launch Daemons:
                     [loaded] com.adobe.fpsaud.plist
                     [loaded] com.adobe.SwitchBoard.plist
                     [loaded] com.google.keystone.daemon.plist
                     [loaded] com.microsoft.office.licensing.helper.plist
    Launch Agents:
                     [loaded] com.adobe.CS5ServiceManager.plist
                     [loaded] com.google.keystone.agent.plist
    User Launch Agents:
                     [loaded] com.adobe.ARM.202f4087f2bbde52e3ac2df389f53a4f123223c9cc56a8fd83a6f7ae.plist
                     [loaded] com.facebook.videochat.Akshay.plist
                     [loaded] com.microsoft.LaunchAgent.SyncServicesAgent.plist
    User Login Items:
              Sony Ericsson Bridge Helper
              BitTorrent
              uTorrent
    3rd Party Preference Panes:
              Flash Player
              Java
    Internet Plug-ins:
              AdobePDFViewer.plugin
              AdobePDFViewerNPAPI.plugin
              Flash Player.plugin
              FlashPlayer-10.6.plugin
              googletalkbrowserplugin.plugin
              JavaAppletPlugin.plugin
              npgtpo3dautoplugin.plugin
              QuickTime Plugin.plugin
              SharePointBrowserPlugin.plugin
    User Internet Plug-ins:
              Google Earth Web Plug-in.plugin
              Picasa.plugin
    Bad Fonts:
              None
    Top Processes:
              14.6  %          WindowServer
              6.9   %          coreaudiod
              6.4   %          uTorrent
              1.9   %          WebProcess
              1.4   %          iTunes
              1.2   %          EtreCheck
              0.5   %          fontd
              0.2   %          NotificationCenter
              0.1   %          Dock
              0.0   %          Microsoft

  • I am able to get color images in the continues shot but when i try to snap a color image which i need for my processing i get only a monocrome image

    I have been using lv_vfw.llb VI's for grabbing images from a CREATIVE WEB CAM ,i am able to get color images in the continues shot but when i try to snap a color image which i need for my processing i get only a monocrome image ,i have been trying to play around with the RGB weightings but i am unable to get a color iamge,it shall be of great help if you could help me in doing this as it is urgent for me.Thanks for your help.

    vicky,
    I am unfamiliar with the lv_vfw.llb, and have not used it before. However, I noticed that you stated that you are able to grab in color, just not snap in color. If this is the case, why not just perform a grab and then extract a single buffer from this grabbed data, which would likely be in color? This seems like a possible solution to the issue that you are seeing.
    Other than that, I really don't know enough about the lv_vfw.llb to be of much help on this issue. Hopefully another member of this forum will be able to assist you with this software.
    Regards,
    Jed R.
    Applications Engineer
    National Instruments

  • I keep getting a message saying my protection engine has corrupted and that i should try re-installing live update. What is live update and how do i run it? - be grateful for any help

    Hi,
    I turn my i mac on and when it has loaded onto the home screen I get a message popping up saying my protection engine has corrupted and that i should try reinstalling live update and my computer will no longer be protected until I do so. Would anyone be able to tell me what live update is and how I would run/reinstall it? - be grateful for any help!!!
    Thankyou

    It would be a great idea to uninstall Norton - you don't need it and it has a pretty poor reputation for mucking up otherwise healthy Macs.
    Have a look at this User Tip from klaus1: https://discussions.apple.com/docs/DOC-2435
    To uninstall Norton:
    https://support.norton.com/sp/en/us/home/current/solutions/kb20080427024142EN_En dUserProfile_en_us
    Edit: To see whether you actually have Norton installed launch Activity Monitor in the Utilities folder. If it's installed you'll likely see some Norton entries there.
    Also, in System Preferences>Users & Groups or Accounts, depending on OS)>Login Items, it might be listed.

  • Hello, so today I realized that my iPhone with iOS 6 is frozen on the music. I can get back to the home screen but I can't get the music player to work because its frozen on the w's and will not respond to touch. I would be grateful for any help on this.

    Hello, so today I realized that my iPhone with iOS 6 is frozen on the music. I can get back to the home screen but I can't get the music player to work because its frozen on the w's and will not respond to touch. I would be grateful for any help on this.

    Hi deamayfield,
    Thanks for visiting Apple Support Communities.
    If your Music app is unresponsive, try restarting/resetting your iPhone:
    iOS: Turning off and on (restarting) and resetting
    http://support.apple.com/kb/ht1430
    Best,
    Jeremy

Maybe you are looking for