Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    speedbump164 is offline Member
    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0

    Exclamation Loading and Scaling Images is not working

    I am taking a Java class through VTC.com and I'm trying to follow along but I ran into obstacles when we started working with Images. The instructor uses command prompt to write his code but I'm running NetBeans. I have 2 classes...a default ImageLoader class for loading images from file or url and then I have a DisplayScaledImages class to display and scale the images and then draw them onto Canvas using Paint. I watched this code work for the instructor but it will not load the images for me, just the blank window with a white background. The course i'm taking is pretty old so I started thinking that there is a modern way to do this and maybe that's why it's not working. Can somebody please help me to figure this out?

    Java Code:
    package displayscaledimages;
    
    import java.awt.Component;
    import java.awt.Toolkit;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.net.URL;
    
    public class ImageLoader extends Component {
        public Image loadImageFromFile(String filename) {
            Image image = null;
            Toolkit tk = Toolkit.getDefaultToolkit();
            image = tk.getImage(filename);
            waitForImage(image);
            return(image);
        }
        public void waitForImage(Image image) {
            MediaTracker mt = new MediaTracker(this);
            mt.addImage(image,1);
            try {
                mt.waitForAll();
            } catch(Exception e) {
                e.printStackTrace();
                System.exit(0);
            }
        }
        public Image loadImageFromInternet(String urlString) {
            Image image = null;
            URL url = null;
            try {
                url = new URL(urlString);
            } catch(Exception e) {
                e.printStackTrace();
                System.exit(0);
            }
            Toolkit tk = Toolkit.getDefaultToolkit();
            image = tk.getImage(url);
            waitForImage(image);
            return(image);
        }
    }
    Here is the other class...

    Java Code:
    package displayscaledimages;
    import java.awt.Frame;
    import java.awt.Canvas;
    import java.awt.Graphics;
    import java.awt.AWTEvent;
    import java.awt.Image;
    import java.awt.Color;
    import java.awt.event.WindowEvent;
    
    public class DisplayScaledImages extends Frame {
        public static void main(String arg[]) {
            new DisplayScaledImages();
        }
        DisplayScaledImages() {
            enableEvents(AWTEvent.WINDOW_EVENT_MASK);
            DisplayScaleCanvas dcanvas = new DisplayScaleCanvas();
            add(dcanvas);
            pack();
            show();
        }
        public void processWindowEvent(WindowEvent event) {
            if(event.getID() == WindowEvent.WINDOW_CLOSING)
                System.exit(0);
        }
    }
    
    class DisplayScaleCanvas extends Canvas {
        Image littlered;
        Image littleblue;
        DisplayScaleCanvas() {
            setSize(256,128);
            ImageLoader il = new ImageLoader();
            Image bluemarble = il.loadImageFromFile("bluemarble.png");
            littleblue = bluemarble.getScaledInstance(128,128,
                    Image.SCALE_AREA_AVERAGING);
            il.waitForImage(littleblue);
            Image redmarble = il.loadImageFromFile("redmarble.jpeg");
            littlered = redmarble.getScaledInstance(128,128,
                    Image.SCALE_AREA_AVERAGING);
            il.waitForImage(littlered);
        }
        public void paint(Graphics g) {
            g.drawImage(littlered,0,0,this);
            g.drawImage(littleblue,128,0,this);
        }
    }
    Attached Thumbnails Attached Thumbnails Loading and Scaling Images is not working-bluemarble.png   Loading and Scaling Images is not working-redmarble.jpeg  

  2. #2
    speedbump164 is offline Member
    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0

    Default Re: Loading and Scaling Images is not working

    I threw in an "if" statement to ensure the image is loading and it is. The problem is with drawing the image. Can somebody please give me some suggestions? I've been searching the net for days and I'm just plain stuck. Please help.

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,416
    Rep Power
    5

    Default Re: Loading and Scaling Images is not working

    You are probably not specifying the complete pathname location for the images. Also, check out the tutorial in my signature. Loading and displaying images is much easier than you are making it. Including scaling.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    speedbump164 is offline Member
    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0

    Default Re: Loading and Scaling Images is not working

    Quote Originally Posted by jim829 View Post
    You are probably not specifying the complete pathname location for the images. Also, check out the tutorial in my signature. Loading and displaying images is much easier than you are making it. Including scaling.

    Regards,
    Jim
    Yeah I found the Java Tutorial site and tried their suggestion of using ImageIO
    Java Code:
    try{
            bluemarble = ImageIO.read(new File("bluemarble.png"));
            }catch(IOException e){}
    By doing this I bypassed the ImageLoader class completely. I was desperate for anything. I also tried ImageIcon
    Java Code:
    Image bluemarble = new ImageIcon("bluemarble.png").getImage();
    I do not believe it is the location of the file for 2 reasons:
    First, the image file is located in the same folder as the java files. I copied and pasted directly into NetBeans and then verified that it was actually in the project folder.
    Second, I threw in an "if" statement to verify whether or not the image was null and it's coming back that the image is not null.
    Java Code:
    if(bluemarble == null)
            {
                System.out.println("Image not found");
            }else
                System.out.println("Image Found");
    I'm just trying to get an image to load onto Canvas at this point. Most of my recent attempts are bypassing the ImageLoader class completely. Any other suggestions? I'll keep poking around the java tutorials. I can get these images to load using JLabel but that's not the route I can take. I'm trying to follow along with the instructor but this code works for him in the demonstrations which is why i'm thinking some component is outdated in the code he gave us. It's not a live lecture so I cannot ask him. It's an old video archive class on VTC.com. I've even submitted the same question on JustAnswer.com for $80 and still no luck. I'm just plain stuck. I'm very very new to Java; maybe about 4 weeks into learning it, and I have no other programming experience.

    Thanks for the help.

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,416
    Rep Power
    5

    Default Re: Loading and Scaling Images is not working

    I noticed you are ignoring your IOExceptions. That is a bad idea. At least print out the stack trace so you can see if it is loading properly.

    Java Code:
    catch (IOException e) {
       e.printStackTrace();
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,416
    Rep Power
    5

    Default Re: Loading and Scaling Images is not working

    And even in Eclipse if I don't specify a proper pathname (e.g. "f:/filename.jpg" vs just "filename.jpg") it complains.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    speedbump164 is offline Member
    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0

    Default Re: Loading and Scaling Images is not working

    Quote Originally Posted by jim829 View Post
    And even in Eclipse if I don't specify a proper pathname (e.g. "f:/filename.jpg" vs just "filename.jpg") it complains.

    Regards,
    Jim
    I really appreciate all your help. I put the file at the root of my drive and specified the location and it didn't do anything different. As for the printStackTrace(), I tried adding that in as well and it did nothing. The example on Java Tutorials leaves it out but I still threw it in to see if anything changed. I've also tried:
    Java Code:
    this.getClass().getResource("e:/bluemarble.png").getFile();
    and
    Java Code:
    getClass().getResource("e:/bluemarble.png");
    Have you by chance loaded my code into your IDE to see what happens on your end? Please don't give up on me yet. Thanks, Jason

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,416
    Rep Power
    5

    Default Re: Loading and Scaling Images is not working

    Yes, I loaded the exact code you specified above. I used your images but specified the full pathname (i.e. prefixed with F:/ on my system). And it worked.

    Edit: Any particular reason the instructor is using the AWT packages vs Swing?

    Regards,
    Jim
    Last edited by jim829; 06-12-2013 at 08:53 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    speedbump164 is offline Member
    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0

    Default Re: Loading and Scaling Images is not working

    I don't know why it's working for you and not for me. So you used both the image loader class and the display scaled class with
    Java Code:
     il.loadImageFromFile("f:/bluemarble.png") or did you use ImageIO?
    It's a really old course so that's probably why he used awt instead of swing. All of his lessons use the awt window and canvas.

    None of the awt image display code has worked for me and there are quite a few lessons. I see it work for him in the lessons but he uses command prompt to teach. I can't use swing because all the source code that comes with the course will not work. I'm totally stuck. I even moved the file to the root of my e:/ drive.

  10. #10
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,416
    Rep Power
    5

    Default Re: Loading and Scaling Images is not working

    Well, just in case there are differences in the code, here is my version of what you pasted. The only differences should be the file declarations and the package name I use.

    Java Code:
        package examples.images;
    
        import java.awt.AWTEvent;
        import java.awt.Canvas;
        import java.awt.Frame;
        import java.awt.Graphics;
        import java.awt.Image;
        import java.awt.event.WindowEvent;
        public class DisplayScaledImages extends Frame {
            public static void main(String arg[]) {
                new DisplayScaledImages();
            }
            DisplayScaledImages() {
                enableEvents(AWTEvent.WINDOW_EVENT_MASK);
                DisplayScaleCanvas dcanvas = new DisplayScaleCanvas();
                add(dcanvas);
                pack();
                show();
            }
            public void processWindowEvent(WindowEvent event) {
                if(event.getID() == WindowEvent.WINDOW_CLOSING)
                    System.exit(0);
            }
        }
         
        class DisplayScaleCanvas extends Canvas {
            Image littlered;
            Image littleblue;
            DisplayScaleCanvas() {
                setSize(256,128);
                ImageLoader il = new ImageLoader();
                Image bluemarble = il.loadImageFromFile("f:/bluemarble.png");
                littleblue = bluemarble.getScaledInstance(128,128,
                        Image.SCALE_AREA_AVERAGING);
                il.waitForImage(littleblue);
                Image redmarble = il.loadImageFromFile("f:/redmarble.jpeg");
                littlered = redmarble.getScaledInstance(128,128,
                        Image.SCALE_AREA_AVERAGING);
                il.waitForImage(littlered);
            }
            public void paint(Graphics g) {
                g.drawImage(littlered,0,0,this);
                g.drawImage(littleblue,128,0,this);
            }
        }
    
    package examples.images;
    
    import java.awt.Component;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.Toolkit;
    import java.net.URL;
    
    public class ImageLoader extends Component {
        public Image loadImageFromFile(String filename) {
            Image image = null;
            Toolkit tk = Toolkit.getDefaultToolkit();
            image = tk.getImage(filename);
            waitForImage(image);
            return(image);
        }
        public void waitForImage(Image image) {
            MediaTracker mt = new MediaTracker(this);
            mt.addImage(image,1);
            try {
                mt.waitForAll();
            } catch(Exception e) {
                e.printStackTrace();
                System.exit(0);
            }
        }
        public Image loadImageFromInternet(String urlString) {
            Image image = null;
            URL url = null;
            try {
                url = new URL(urlString);
            } catch(Exception e) {
                e.printStackTrace();
                System.exit(0);
            }
            Toolkit tk = Toolkit.getDefaultToolkit();
            image = tk.getImage(url);
            waitForImage(image);
            return(image);
        }
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    speedbump164 is offline Member
    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0

    Default Re: Loading and Scaling Images is not working

    Quote Originally Posted by jim829 View Post
    Well, just in case there are differences in the code, here is my version of what you pasted. The only differences should be the file declarations and the package name I use.
    Regards,
    Jim
    Thanks Jim. Unfortunately it will not run for me in netbeans. I think I'll try Eclipse like you and see if that makes a difference. I'll keep you posted on how it works out. Thanks, Jason

  12. #12
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,416
    Rep Power
    5

    Default Re: Loading and Scaling Images is not working

    OK. First I'd like you to try something. Copy your two classes to some folder somewhere of your choosing. Also make certain the two images are in the same folder.

    Edit each java program and remove the package declaration.

    Then compile both programs using javac Filename.java. Ignore any warnings (you will get one regarding show() as deprecated).

    Then use java to run the program:

    java DisplayScaledImages

    You my need to modify your executable path to reflect the location of the Java compiler and runtime programs.

    Let me know if that works.

    Also, here is a version I wrote. See if this works in NetBeans. You may add a package declaration of your choosing.


    Java Code:
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class ScaleImages extends JPanel {
       private JFrame frame;
       private Image  redPlanet;
       private Image  bluePlanet;
       private boolean imagesLoaded = false;
    
       public ScaleImages() {
          frame = new JFrame("Scaled Images");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.add(this);
          setLayout(null);
       }
    
       public static void main(String[] args) {
          ScaleImages si = new ScaleImages();
          si.loadImages();
          si.resize_and_show();
          si.repaint();
       }
    
       public void loadImages() {
          try {
             redPlanet = ImageIO.read(new File("f:/redmarble.jpeg"));
             bluePlanet = ImageIO.read(new File("f:/bluemarble.png"));
             bluePlanet = bluePlanet.getScaledInstance(200,
                   -1,
                   Image.SCALE_AREA_AVERAGING);
             redPlanet = redPlanet.getScaledInstance(-1,
                   200,
                   Image.SCALE_AREA_AVERAGING);
             setImageStatus(bluePlanet != null && redPlanet != null);
          }
          catch (IOException ioe) {
             ioe.printStackTrace();
          }
       }
    
       public void resize_and_show() {
          if (imagesLoaded()) {
             int totalWidth = bluePlanet.getWidth(null) + redPlanet.getWidth(null);
             setPreferredSize(new Dimension(totalWidth, 200));
             frame.pack();
             frame.setLocationRelativeTo(null);
             frame.setVisible(true);
          }
       }
    
       public void paintComponent(Graphics g) {
          if (imagesLoaded()) {
             super.paintComponent(g);
             g.drawImage(bluePlanet, 0, 0, null);
             g.drawImage(redPlanet, 200, 0, null);
          }
       }
       
       public boolean imagesLoaded() {
          return imagesLoaded;
       }
       
       public void setImageStatus(boolean imageStatus) {
          imagesLoaded = imageStatus;
       }
    
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    speedbump164 is offline Member
    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0

    Default Re: Loading and Scaling Images is not working

    Ok. I'm on it. BTW...I tried Eclipse and built a project using JRE7 and it didn't load the images. My images are located at the root of my e:/ drive so i'm setting the file location in the code to "e:/bluemarble.png"

  14. #14
    speedbump164 is offline Member
    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0

    Default Re: Loading and Scaling Images is not working

    The new code you provided me worked. I cannot get javac to be recognized as a command to compile the classes and when i type just java it gives me a help screen with a few commands that can be used but none of them are what i need.
    Last edited by speedbump164; 06-13-2013 at 07:43 PM. Reason: additional info

  15. #15
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,416
    Rep Power
    5

    Default Re: Loading and Scaling Images is not working

    Apparently your JRE is in the path name but not the JAVA_HOME. You may want to modify your PATH environment variable to include the pathname of the javac binary in the search path. This pathname should be where Java is installed, suffixed with a /bin. You may want to check to see if the file is there before you add it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  16. #16
    speedbump164 is offline Member
    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0

    Default Re: Loading and Scaling Images is not working

    Quote Originally Posted by jim829 View Post
    Apparently your JRE is in the path name but not the JAVA_HOME. You may want to modify your PATH environment variable to include the pathname of the javac binary in the search path. This pathname should be where Java is installed, suffixed with a /bin. You may want to check to see if the file is there before you add it.

    Regards,
    Jim
    Ok, I found out the JRE is installed in 2 places. Its's under Program Files and Program Files x86. I found the javac command under the sdk folder at C:\Program Files\Java\jdk1.7.0_21\bin. Where is JAVA_HOME and how do I change the PATH environment variable? I'm pretty tech savvy but I know nothing of Java really and I don't believe that in all my years of computers that I've never had to change the PATH variable. Thanks for the help Jim.....Jason

  17. #17
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,416
    Rep Power
    5

    Default Re: Loading and Scaling Images is not working

    The path environment variable is only necessary if you want the command console in Windows and the shell in Unix or Linux to find your program without having to type the complete pathname along with the command. Here is how you do it on Windows. I am using XP but other versions should be similar

    Go to My Computer -> View System Information -> Advanced -> Environment Variables.

    Then under System variables, find path, highlight it, and edit it. Then at the end simply past the pathname of the JDK including the bin folder. The pathnames are separated by a semi-colon.

    Then back out of the menus by clicking OK. New command consoles should now know how to find the javac command.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  18. #18
    speedbump164 is offline Member
    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0

    Default Re: Loading and Scaling Images is not working

    Quote Originally Posted by jim829 View Post
    The path environment variable is only necessary if you want the command console in Windows and the shell in Unix or Linux to find your program without having to type the complete pathname along with the command. Here is how you do it on Windows. I am using XP but other versions should be similar

    Go to My Computer -> View System Information -> Advanced -> Environment Variables.

    Then under System variables, find path, highlight it, and edit it. Then at the end simply past the pathname of the JDK including the bin folder. The pathnames are separated by a semi-colon.

    Then back out of the menus by clicking OK. New command consoles should now know how to find the javac command.

    Regards,
    Jim
    Ok Jim,
    I got both classes to compile with 2 errors in DisplayScale.java about a deprecated api and recompile with Xlint for deprecation details; however, when i try and run it using the java command i get this error message saying could not find or load main class. I'm not sure what that is all about. I know I have the path right because I compiled it successfully. It's weird. Thanks, Jason

  19. #19
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,416
    Rep Power
    5

    Default Re: Loading and Scaling Images is not working

    1. Did you remove the package names from both classes?
    2. Did you invoke the one with the public static void main(String[] args) entry point via java:
    e.g java DisplayScaledImages

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  20. #20
    speedbump164 is offline Member
    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0

    Default Re: Loading and Scaling Images is not working

    Yep. Did both.

Page 1 of 2 12 LastLast

Similar Threads

  1. Loading Images
    By wired-in=p in forum New To Java
    Replies: 9
    Last Post: 02-08-2012, 11:53 PM
  2. The problem with scaling images
    By LianaN in forum AWT / Swing
    Replies: 3
    Last Post: 01-18-2011, 08:52 PM
  3. scaling images
    By willemjav in forum Java Applets
    Replies: 7
    Last Post: 06-19-2008, 09:54 AM
  4. Loading Images - Imp
    By Thulasiraman in forum Advanced Java
    Replies: 0
    Last Post: 01-28-2008, 09:33 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •