Java.awt.Image getWidth() and getHeight() not working!

Right- this is want I want to do.
I have a background image which is added as a background image to a third party API class which extends Java.awt.Container. Such container is then added to the Content Pane of MyClass (which extends JFrame)
I want to read the width and height of the image and resize MyClass accordingly.
Problem is calling theImage.getWidth(MyClass.this) or theImage.getWidth(theThirdPartyAPIContainer) always return -1 and -1 denoting that no information is available.
What do I need to do to obtain the height and width?

Post here, cos don't know your email.
Anyway
- Can't use setPreferredSize because the Conatiner which is added to the ContentPane is a Awt Component not a Swing JComponent!
- Also, the background is added AFTER the this.pack()and this.show().
-I figured out that I could access Insets.top, Insets.left, Insets.right, Insets.bottom.
I have basically the following
|JMenuBar |
L____________________________________________|
| |
| |
| |
| Third Party Class extending java.awt.Container |
| containing an image of known size |
| |
L____________________________________________|
I am fine for the width I do
Insets frameInsets = this.getInsets();
width = image.getWidth() + frameInsets.left + frameInsets.right;
Height is the problem
After some Trial an error it LOOKS on Solaris as if the following is right-
height = image.getHeight()+frameInsets.top+ myJMenuBar.getSize()+getHeight+ myJMenuBar.getInsets().top + myJMenuBar.getInsets().bottom
I cannot comprehend whether this is just a coincidence or not as I don't know why frameInsets.bottom should in any case be excluded.
Here are the dimensions-
frameInsets.left = 6
frameInsets.right= 6
frameInsets.top =28
frameInsets.bottom = 6
JMenuBar height = 23
JMenuBar's Insets.top = 1
JMenuBar's Insets.bottom = 1
What should be the 'correct' calculations and does the Insets for the JMenuBar and the JFrame overlaps?

Similar Messages

  • Java pulgin is enablesed and still not working with firefox but is working with my explorer browser

    Java 1.7.0_10 from Oracle Corp is working with my Explorer but when I go to a sight with Firefox, it shows that its not installed. When I go into Firefox's plug-ins, it shows that its enabled....How do I fix this?

    Your plugins list only shows the Java Deployment plugin and not the Java plugin.<br />
    A possible cause is that the Java installer failed to create the necessary registry keys that Firefox uses to scan for plugins.
    See this page about a possible JavaFX installation causing this.
    *[[/questions/944191?page=4]]
    See also:
    *http://kb.mozillazine.org/Java

  • Image object getWidth() and getHeight() returning -1

    Hello all,
    I'm currently unit testing a pretty simple utility that calculates a new image position, given a handful of parameters (including image width and height).
    The problem I'm running into is this:
    The image width/height don't always load immediately. My only clue so far from searching the web is this:
    http://download-llnw.oracle.com/javase/6/docs/api/java/awt/Image.html#getWidth%28java.awt.image.ImageObserver%29
    which states Determines the width of the image. If the width is not yet known, this method returns -1 and the specified ImageObserver object is notified later. In my JUnit test harness, I have the following code to load my images, using Toolkit:
    public static final String TEST_IMG_1_PATH = "images/apple_logo.png";
    public static final String TEST_IMG_2_PATH = "images/dude.png";
    private Image testImage1, testImage2;
    @Before
    public void setUp() throws Exception {
         testImage1 = Toolkit.getDefaultToolkit().getImage(TEST_IMG_1_PATH);
         testImage2 = Toolkit.getDefaultToolkit().getImage(TEST_IMG_2_PATH);
         // check the image size to see if they are fully loaded:
         System.out.println(testImage1.getWidth(null));
         System.out.println(testImage1.getHeight(null));
         System.out.println(testImage2.getWidth(null));
         System.out.println(testImage2.getHeight(null));
    }Because the utility method I'm testing heavily depends on the image size, my tests intermittently fail. When I run this, I typically get -1 for all of the image dimension checks (see the above println()s).
    -1
    -1
    -1
    -1Even with these "-1" results, my test still passes... which means at some point before the test assert() statement executes, the images do become fully loaded.
    My question is: What can I do to make sure my image is loaded fully before I do anything with it? Is there any elegant way to handle this? Am I doing something wrong?
    Thanks for your time.
    -Thok

    [http://forums.sun.com/thread.jspa?threadID=5446922]
    db

  • I am having trouble printing a PDF document. Whenever I click print nothing happens but when I am printing documents from Word, Excel etc they print without a problem. I've tried printing as image but that did not work. I uninstalled Adobe Reader XI and d

    I am having trouble printing a PDF document. Whenever I click print nothing happens but when I am printing documents from Word, Excel etc they print without a problem. I've tried printing as image but that did not work. I uninstalled Adobe Reader XI and downloaded Adobe Reader X instead and that still doesn't work. I have Windows 7.

    Hello,
    Please follow the link to troubleshoot the printing issues with Adobe Reader:- Troubleshoot PDF printing | Acrobat, Reader.
    Regards,
    Nakul

  • Java applet works in windows and does not work in linux

    Hello, guys!
    I'm working with site which is located in vpn network at work.
    I use browsers firefox 3 and opera.
    When I want to open java applet using firefox or opera at first everything is fine then on the last stage of loading(judging by progress bar) applet hangs(progress bar hangs) and when I reload page firefox shows in the bottom bar: applet <applet's name> bail.
    In windows everything works both in ie6 and firefox 3.
    On both environments I have the jre 6 installed.
    please help!

    Ok, I installed ies4linux and jre 1.5.16. I can work with that but when I start applet screen goes black and window frames dissapear.
    To solve this problem(feature?) I disabled DirectX-based acceleration for Java 2D but that works for javacpl.exe and does not work for my applet.

  • JAVA ISN'T WORKING. I've tried software update and also reinstalling java. But it's still not working. How can I fix it? OS X 10.5.8

    I've tried software update and also reinstalling java. But it's still not working. How can I fix it? OS X 10.5.8

    Bad juju not having a bootable backup that you can revert back to when you run into these kinds of issues. Get a bootable, external HD, and make a bootable backup/clone before updating/upgrading and ensure that it's bootable and works like the original. See these for details:
    http://docs.info.apple.com/article.html?artnum=106941
    http://www.macmaps.com/upgradefaq.html
    http://www.macmaps.com/backup.html
    http://www.thexlab.com/faqs/installswupdates.html
    http://www.thexlab.com/faqs/backuprecovery.html
    Back to your issue. Boot with the Leopard install disk, select your language, select Disk Utility from the Utilities menu, and repair the disk and permissions. Report back.

  • Useless code in java.awt.image.SampleModel.java?

    Hey there,
    i just looked up the sourcecode of java.awt.image.SampleModel.java in JDK 6
    I discovered two issues i'd like to discuss.
    1) on lines 736 to 739 this code is stated:
    if (iArray != null)
    pixels = iArray;
    else
    pixels = new int[numBands * w * h];
    I asked myself, why does this code exist? while the getPixels() method is overwritten twice by double[] getPixels() and float[] getPixels, it is impossible to reach the part of the java code that initializes the pixels-array. One could only step into that line if "null" is given for the i/d/fArray-parameter. but if one would do so, the Java parser couldn't determine, which method is to use. so this part of code is just useless IMHO.
    the java developers could get a little more performance out of this method if the if statement would be cut out - especially when reading a lot of very small rasters
    or, on the other hand, they could replace this piece of code by an explicit bounds check.
    When somebody touches this code, i would appreciate it if the errormessage "index out of bounds!" could be rewritten to be a little more verbose, like: Index out of bounds(x=123; y=456, maxX=100, maxY=400)!(numbers are just examples)
    I hope i didn't miss something very basic and could help improving this class a little bit.
    2) the local variable Offset(line 734) is coded against code conventions which say, variables shall start with a lowercase letter. Offset obviously doesn't fit that convention.
    best regards
    kdot

    One could only step into that line if "null" is given for the i/d/fArray-parameter. but if one would do so, the Java parser couldn't determine, which method is to use. so this part of code is just useless IMHO. You can have
    sampleModel.getPixels(x,y,w,h,(int[]) null, dataBuffer);No ambiguity on which method to use.
    the local variable Offset(line 734) is coded against code conventions which say, variables shall start with a lowercase letter. Offset obviously doesn't fit that convention. You're correct, offset is against coding conventions. So are many other examples scattered throughout the jdk source code. For example, Hashtable should be called HashTable. In some cases the coding conventions might not have been established when the original code was written. In other cases it might have been human error. In yet other cases the conventions were probably ignored. The person who wrote the SampleModel class did so some 10+ years ago (Java 1.2). Who knows what he/she was thinking at the time, and in all honesty - does it really matter in this case?
    Did you know there are some classes that declare unused local variables (ahem ColorConvertOp)? Some also have unused imports ( *** cough *** BufferedImage *** cough *** ). In essence, the jdk source code is not the epidemy of code correctness. But it's still pretty good.

  • OutOfMemory error in java.awt.image.DataBufferInt. init

    We have an applet application that performs Print Preview of the images in the canvas. The images are like a network of entities (it has pictures of the entities involve (let's say Person) and how it links to other entities). We are using IE to launch the applet.
    We set min heap space to 128MB, JVM max heap space to 256MB, java plugin max heap space to 256MB using the Control Panel > Java.
    When the canvas width is about 54860 and height is 1644 and perform Print Preview, it thows an OutOfMemoryError in java.awt.image.DataBufferInt.<int>, hence, the Print Preview page is not shown. The complete stack trace (and logs) is as follows:
    Width: 54860 H: 1644
    Max heap: 254 # using Runtime.getRuntime().maxMemory()
    javaplugin.maxHeapSize: 256M # using System.getProperties("javaplugin.maxHeapSize")
    n page x n page : 1x1
    Exception in thread "AWT-EventQueue-2" java.lang.OutOfMemoryError: Java heap space
         at java.awt.image.DataBufferInt.<init>(Unknown Source)
         at java.awt.image.Raster.createPackedRaster(Unknown Source)
         at java.awt.image.DirectColorModel.createCompatibleWritableRaster(Unknown Source)
         at java.awt.image.BufferedImage.<init>(Unknown Source)
         at com.azeus.gdi.chart.GDIChart.preparePreview(GDIChart.java:731)
         at com.azeus.gdi.chart.GDIChart.getPreview(GDIChart.java:893)
         at com.azeus.gdi.ui.GDIUserInterface.printPreviewOp(GDIUserInterface.java:1526)
         at com.azeus.gdi.ui.GDIUserInterface$21.actionPerformed(GDIUserInterface.java:1438)
         at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
         at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
         at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
         at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
         at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
         at java.awt.Component.processMouseEvent(Unknown Source)
         at javax.swing.JComponent.processMouseEvent(Unknown Source)
         at java.awt.Component.processEvent(Unknown Source)
         at java.awt.Container.processEvent(Unknown Source)
         at java.awt.Component.dispatchEventImpl(Unknown Source)
         at java.awt.Container.dispatchEventImpl(Unknown Source)
         at java.awt.Component.dispatchEvent(Unknown Source)
         at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
         at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
         at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
         at java.awt.Container.dispatchEventImpl(Unknown Source)
         at java.awt.Component.dispatchEvent(Unknown Source)
         at java.awt.EventQueue.dispatchEvent(Unknown Source)
         at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
         at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
         at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
         at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
         at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
         at java.awt.EventDispatchThread.run(Unknown Source)
    Drilling down the cause of the problem. The OutOfMemory occurred in the constructor of DataBufferInt when it tried to create an int array:
    public DataBufferInt(int size) {
    super(STABLE, TYPE_INT, size);
    data = new int[size]; # this part produce out of memory error when size = width X height
    bankdata = new int[1][];
    bankdata[0] = data;
    The OutOfMemory error occurred when size is width * height (54860 X 1644) which is 90,189,840 bytes (~86MB).
    I can replicate the OutOfMemory error when initiating an int array using a test class when it uses the default max heap space but if I increase the heap space to 256MB, it cannot be replicated in the test class.
    Using a smaller width and height with product not exceeding 64MB, the applet can perform Print Preview successfully.
    Given this, I think the java applet is not using the value assigned in javaplugin.maxHeapSize to set the max heap space, hence, it still uses the default max heap size and throws OutOfMemory in int array when size exceeds the default max heap space which is 64MB.
    For additional information, below is some of the java properties (when press S in java applet console):
    browser = sun.plugin
    browser.vendor = Sun Microsystems, Inc.
    browser.version = 1.1
    java.awt.graphicsenv = sun.awt.Win32GraphicsEnvironment
    java.awt.printerjob = sun.awt.windows.WPrinterJob
    java.class.path = C:\PROGRA~1\Java\jre6\classes
    java.class.version = 50.0
    java.class.version.applet = true
    java.runtime.name = Java(TM) SE Runtime Environment
    java.runtime.version = 1.6.0_17-b04
    java.specification.version = 1.6
    java.vendor.applet = true
    java.version = 1.6.0_17
    java.version.applet = true
    javaplugin.maxHeapSpace = 256M
    javaplugin.nodotversion = 160_17
    javaplugin.version = 1.6.0_17
    javaplugin.vm.options = -Xms128M -Djavaplugin.maxHeapSpace=256M -Xmx256m -Xms128M
    javawebstart.version = javaws-1.6.0_17
    Kindly advise if this is a bug in JRE or wrong setting. If wrong setting, please advise on the proper way to set the heap space to prevent OutOfMemory in initializing int array.
    Thanks a lot.
    Edited by: rei_xanther on Jun 28, 2010 12:01 AM
    Edited by: rei_xanther on Jun 28, 2010 12:37 AM

    rei_xanther wrote:
    ..But the maximum value of the int data type is 2,147,483,647. That is the maximum positive integer value that can be stored in (the 4 bytes of) a signed int, but..
    ..The value that I passed in the int array size is only 90,189,840...its only connection with RAM is that each int requires 4 bytes of memory to hold it.
    new int[size] -- size is 90,189,840Sure. So the number of bytes required to hold those 90,189,840 ints is 360,759,360.
    I assumed that one element in the int array is 1 byte. ..Your assumption is wrong. How could it be possible to store 32 bits (4 bytes) in 8 bits (1 byte)? (a)
    a) Short of some clever compression algorithm applied to the data.

  • How can I create a java.awt.Image from ...

    Hi all,
    How can I create a java.awt.Image from a drawing on a JPanel?
    Thanks.

    JPanel p;
    BufferedImage image =
        new BufferedImage(p.getWidth(), p.getHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = image.createGraphics();
    p.paint(g);
    g.dispose();

  • Problems with getWidth() and getHeight() in class Canvas

    Hi,
    i am developing MIDlets with Sun Studio4 One Update1 (Mobile Edition). Here is a simple example Code for my Problem:
    class MyCanvas extends Canvas
        public void paint(Graphics g)
            //Clear the Display
            g.setColor(0,0,0); //black
            g.fillRect(0,0,getWidth(),getHeight());
    }If i try to execute the Program with the Default Color Phone emulator included with Suns WTK2.0, everything works fine. (Ive tried to print out the results of getWidth() and getHeight() on the console, everything ok, the Displays width and height is displayed.).
    The problem is, that any other emulator from WTK1.0.4_01 (included with Sun Studio4 ME) compiles and preverifies fine, but while running the application, the emulator quits with the following error:
    ALERT: No such method getWidth.()I
    Execution completed successfully
    Anyone has an idea?
    Thanks!!

    Hello,
    I'm having the exact same problem and I'd like to know what changes you made to your code to get it working. My class is not an inner class and is very much like yours and so follows the form:
    class MyCanvas extends Canvas{ 
    int element = getWidth()/10;
    public void paint(Graphics g) {        //Clear the Display  
    g.setColor(0,0,0); //black
    g.fillRect(0,0,getWidth(),getHeight());
    I am also using WTK104. I did not have this problem until I renamed my file. I carried the renaming throughout the rest of the file in a consistent fashion and I don't see any problems with it. Any help appreciated.

  • After installing Firefox 6 it disables java 6.0.26 and says "not compatible...". The last version ov java is 1.6.0.26 which is what most users use. What is the solution?

    This morning I installed Firefox 6 and it says that it is not compatible with Java Console 6.0.26 and it is disabled. I am using Windows 7 Professional 32 bit. Is this normal?

    Not quite true certain web pages need java and are NOT working correctly at the moment .

  • I have mac os x 10.6.8 version and my java is version 6 and it deosnt work since yesterday and it gives error?

    i have mac os x 10.6.8 version and my java is version 6 and it deosnt work since yesterday and it gives error?
    i really hope someone can help me with it because i really need java

    I followed that and Java .jnlp files still do not work after downloading them manualy and double-click. What's next?
    Java -version show the right version.
    I need to get this fixed as it is thrid day when after applying OsX updates things got broken.  I would prefer to have Java functional and be exposed to security risk than not having working at all.
    How do I fix my Java .jnlp when I have all updates for 10.6.8 and running Safari 5.1.10 ?
    I do not want to upgrade as I am scared that other things will break.
    Please help.

  • S10-3t bluetooth and wireless not working with windows 8

    S10-3t bluetooth and wireless not working with windows 8. I tried everything but doesn't work at all. I don't know where to find the new drivers that are comatible with windows 8 ???!!! please help me to solve this problem because i tried everything....

    Do you still have your windows7 install files ? 
    ( installation image is usually in LENOVO_PART OKRBackup/Factory ) ? I lost them :\
    if yes I fear you have to enable it in win7 and then use it in win8
    http://rzr.online.fr/q/lenovo# g470 s103t (Please Contact me if your s10-3T is booting win7 or support bluetooth, 3g)
    Lenovo S10-3t | Model Name : 0651 | Mfg Date: 2010/06/08
    Lenovo G470 | Model Name : 20078 | M fg Date: 11/03/23 | BIOS: 40CN23WW(V2.09) 06/20/2011 | CPU: i5-2410M | Linux version 3.3.4lenovog470+ (root@lap) (gcc version 4.6.3 (Debian 4.6.3-4) ) #8 SMP Tue May 1 10:23:48 CEST 2012
    OS:GNU/Linux/Debian

  • Hi all, Since I updated to Mavericks I am having trouble with Safari showing all the buttons/clickable options, but they are gray and will not work. Specifically, the Trash Can/Delete and the "Move to" folder button simply do not work. Any ideas?

    Hi all, Since I updated to Mavericks I am having trouble with Safari showing all the buttons/clickable options, but they are gray and will not work. Specifically, the Trash Can/Delete and the "Move to" folder button simply do not work. Any ideas?

    Please post a screenshot that shows what you mean. Be careful not to include any private information.
    Start a reply to this message. Click the camera icon in the toolbar of the editing window and select the image file to upload it. You can also include text in the reply.

  • Microphone and Webcam not working/We​bcam not recognized by YouCam

    Hello,
          I have a dv7-6c95dx and my YouCam  program tells me that it does not recognize my integrated webcam. I have read all of the webcam forums on this site and clicked on all of the links people have posted in the forums. I have uninstalled the CyberLink YouCam program and it still comes up with the same messages. The webcam also does not show up in my device manager. The microphone is malfunction as well, it will not work any any programs including skype. 
         I have heard that the webcam can come unplugged internally, will that also cause the microphone to stop working? I have run out of options and I do not want to pay anyone to fix it. I would appreciate any advice on this subject. 

    Hi DillOrNoDill,
    Thank you for visiting the HP Support Forums and Welcome. I have read your thread on your HP Pavilion dv7-6c95dx Notebook and sound and microphone issues and Webcam not working correctly. I would be happy to assist if needed. Try these steps first.
    1. Right click speaker icon (right hand corner)
    2. Select Playback devices
    3. Right click Speakers
    4. Enable it and set as Default
    5. Click Apply/Ok
    You could also try Microsoft troubleshooter located on your Notebook.
    Here is a document on WebCam troubleshooting. The Webcam will be listed in your device manager under Imaging devices. You should be able to click the action button and scan for new hardware.
    Hope this helps.
    Thanks.
    Please click “Accept as Solution ” if you feel my post solved your issue, it will help others find the solution.
    Click the “Kudos, Thumbs Up" on the bottom to say “Thanks” for helping!

Maybe you are looking for

  • IPod nano locked with folder and exclamation

    I have received today a new iPod nano and it has worked just for an hour. In the middle of a transfer from my pc it got locked. I tried everything but to no avail. It doesn't reset pressing select and menu and it doesn't restore with iPod updater, be

  • Image Gallery picture orientation

    Is there a way to have a gallery where all of the pictures are rotated 90°?   I have pictures that are taller than they are wide, so I would like them to be display sideways.

  • Error loading the vi

    Hi, There's error loading the main page vi and can be solve when i open the subvi first and then reopen the main vi. Any reason for this problem?? regards, Attachments: error9.JPG ‏78 KB

  • Spry Pager & Filters issue

    Hi Kin or Don, I am still not sure why I am getting this error when I Filter my dataset in the paged version of the project I am working on. here is the sample page I created that shows the issue I seem to be having. http://www.bardenhomes.com/pcs/sg

  • Cannot get VPN connection working

    I set up my company laptop (running Ubuntu Linux) to connect to the company VPN in about 2 minutes, but using the same settings in "Internet Connect" on my iMac G5, it times out, telling me it cannot get a response from the server. I notice also that