Getting Permission To Play With a Picture

Discussion in 'Mac Programming' started by bobber205, Aug 1, 2006.

  1. macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Location:
    Oregon
    #1
    Code:
     public void start()
        {
            
            //makes a connection to the actual image
            ///
            ///
            URL imageLocation = null;
            URLConnection connection = null;
            try {
            
                imageLocation = new URL("http", "www.klamath.net", "/main2.jpg");  
    
            }
    
            catch (MalformedURLException ex) {
                
                System.out.println("URL did not form properly");
                
            }
            
           System.out.println(imageLocation);
           
    ///////////////////////////////////////////////
    //Now connect to the url
    
           try {
                   
               connection = imageLocation.openConnection();
               System.out.println(connection + " CONNECTION");
               System.out.println(connection.getContent());
               
            }
            
            catch (IOException ex) {}
            
            catch (java.security.AccessControlException ex) { 
                
                System.out.println("It won't let me in!"); 
                ex.printStackTrace();
            
            }
          
            
        
        
        }
    Here why I'm doing what I'm doing.
    I wanted to get back into java before school started and I thought a good place to start would be internet issues with java (was wrong ;)). jokes aside, I want to be able to get a URL from a website every 5 seconds.

    To do that, I need to be able to successfully get the image I need.

    (Eventually I'll want to add functions so that my applet checks to see when the next image will be uploaded (it's every 5 seconds) and then gets the new pic when it really is new, maybe just one second behind.)

    When the above code runs, I am denied access. How do I get around this?
     
  2. thread starter macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Location:
    Oregon
    #2
    Just thought I should add that I have dmz enabled on my computer, opening all ports. Though that shouldn't make a difference should it?
     
  3. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #3
    I can't find details in the docs on what that URL class expects for the third "file" argument in the constructor you use. Maybe it doesn't want that preceeding '/' you've got there.

    Or you could try just new URL("http://www.klamath.net/main2.jpg");

    Edit: Oh, since this is an applet you need to run it from www.klamath.net to get access to www.klamath.net. If you're running it locally you can only get stuff from localhost.

    If you want an applet to access resources from other places than the host it is loaded from, you need to sign it with a digital certificate.
     
  4. thread starter macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Location:
    Oregon
    #4
    Ah! I see.

    Well. I'll need to rethink things.

    Note: If I make this a standalone app, will I be able to get to it?
     
  5. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #5
    Yes, there are no such limitations on a standalone app.
     
  6. thread starter macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Location:
    Oregon
    #6
    Any suggestions on how to easily convert my applet so far into a standalone app?
     
  7. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #7
    It might work to just add it in a frame and go from there...


    Code:
        public static void main(String[] args) {
            Component applet = new MyApplet();
            applet.init();
            JFrame frame = new JFrame();
            frame.getContentPane().add(applet);
            frame.pack();
            frame.setVisible(true);
            applet.start();
        }
    If that doesn't work, rewrite the current applet to extend JPanel, Panel, Container or JComponent or whatever you see fit instead of (J)Applet.
     
  8. thread starter macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Location:
    Oregon
    #8
    Brilliance! It's much too bad I already rewrote it though... thanks!
     
  9. thread starter macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Location:
    Oregon
    #9
    Now that I have a connection going, how do I download the image w/o having to process individual bytes?

    Because that'll suck. ;)
     
  10. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #10
    Code:
    URL url = new URL("http://www.klamath.net/main2.jpg");
    Image img = Toolkit.getDefaultToolkit().getImage(url);
    might do the trick
     
  11. thread starter macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Location:
    Oregon
    #11

    Whoo! You're a lifesaver. ;)

    I did what you suggested and I then printed the actual img object I got.
    This is what was printed.

    Code:
    apple.awt.OSXImage@8814e9
    That's right! :D

    Whew. I was so close to that method too. The main thing I left out was the "getDefaultToolKit" part....
     
  12. thread starter macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Location:
    Oregon
    #12
    Ok. I learned that I couldn't get access to my image because it was an unsigned applet. I wasn't too far so I went ahead and made it an app instead. I've got the image download, the canvas displayed.

    Code is below. When I run this java code, the background color flashes for a second then goes away, giving away to what's supposed to be the image... but I'm not getting anything. I thought at first it was because my image hadn't been loaded before the canvas was constructed, but I moved the makeConnection() method up before the canvas is made...

    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.net.*;
    import java.io.*;
    import java.awt.Image.*;
    
    
    public class KlamathWebCam extends JFrame
    {
    
    ///---> Variables:
    URL imageLocation = null;
    URLConnection connection = null;
    Image mainJPEG = null;
    Image mainImg = null;
    JPanel mainPanel = null;
    JButton refreshButton = null;
    MainCanvas imgCanvas = null;
        
        
        KlamathWebCam(String title) 
        {
            super(title);
            
            //---->
            makeConnection();
            //<----
            
            mainPanel = new JPanel();
            refreshButton = new JButton("Refresh");
            imgCanvas = new MainCanvas();
            
            imgCanvas.setSize(new Dimension(640,480));
            imgCanvas.setBackground(new Color(20,200,55));
            
            mainPanel.add(imgCanvas);
            mainPanel.add(refreshButton);
            
            getContentPane().add(mainPanel);
            setSize(700,700);
            setVisible(true);
            
            
            
            
    
        }
        
        
        
    
        public static void main(String[] args) {
            
            KlamathWebCam app = new KlamathWebCam("Klamath.net WebCam");
            
           }
            
    /////////////////////////////////////////////////////////////////////////////////////////////      
           public void makeConnection() {
               
               try {
                   
                imageLocation = new URL("http", "www.klamath.net", "/main2.jpg");  
                System.out.println(mainPanel + " <-----MainPanel");
    
            }
    
            catch (MalformedURLException ex) {
                
                System.out.println("URL did not form properly");
                
            }
            
           System.out.println(imageLocation);
           
    ///////////////////////////////////////////////
    //Now connect to the url
    
           try {
                   
               connection = imageLocation.openConnection();
               System.out.println(connection + " CONNECTION");
               
               System.out.println("IMAGE OBTAINED!");
               
    
               System.out.println(connection.getContent() + " TYPE");
               
               Image mainImg = Toolkit.getDefaultToolkit().getImage(imageLocation);
               System.out.println(mainImg + " <------Image");
    
    
               
               
               
            }
            
            catch (IOException ex) { ex.printStackTrace();}
            
            catch (java.security.AccessControlException ex) { 
                
                System.out.println("It won't let me in!"); 
                ex.printStackTrace();
            }
            
            
               
               
           }
    ////////////////////////////////////////////////////////////////////////////////////////////////
    
    class MainCanvas extends Canvas {
    
        MainCanvas() { System.out.println("MainCanvas was created!!"); }
        
        public void paint(Graphics g) {
    
            System.out.println(">>>>>>>>>PAINT!<<<<<<<<");
            g.drawImage(mainImg,0,0,this);
            
            
            
            
            
        }
    
    
    
    }
        
        
        
    }
    
    I thought it might be good to go ahead and give you guys the class file so you can see for yourself. :D
     

    Attached Files:

  13. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #13
    You're probably right that that the image isn't loaded by the time your draw it. The reason is that the getImage(url) method doesn't wait for the image to load before continuing, it just creates the image object and starts an asynchronous loading of the image content.

    To make sure that the image is loaded, you can wait for it using MediaTracker. The simplest solution:
    Code:
    Image mainImg = Toolkit.getDefaultToolkit().getImage(imageLocation);
    MediaTracker tracker = new MediaTracker(this);
    tracker.addImage(mainImg, 0);
    tracker.waitForAll(10*1000); // wait 10 seconds before giving up
    
     
  14. thread starter macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Location:
    Oregon
    #14
    Aww. That's the class I needed! I was initially looking for a method with an image to tell if it was loaded, but I'll use that instead!

    You're such great help!
     
  15. thread starter macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Location:
    Oregon
    #15
    I added the MediaTracker code right after the image is loaded in my code.

    Especially the first time, the class took a couple seconds longer to load, but still no dice.
     
  16. thread starter macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Location:
    Oregon
    #16
    Code is below.

    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.net.*;
    import java.io.*;
    import java.awt.Image.*;
    
    
    
    public class KlamathWebCam extends JFrame
    {
    
    ///---> Variables:
    URL imageLocation = null;
    URLConnection connection = null;
    Image mainJPEG = null;
    Image mainImg = null;
    JPanel mainPanel = null;
    JButton refreshButton = null;
    MainCanvas imgCanvas = null;
    MediaTracker tracker = null;
        
        
        KlamathWebCam(String title) 
        {
            super(title);
            
            //---->
            makeConnection();
            //<----
            
            mainPanel = new JPanel();
            refreshButton = new JButton("Refresh");
            imgCanvas = new MainCanvas();
            
            imgCanvas.setSize(new Dimension(640,480));
            //imgCanvas.setBackground(new Color(20,200,55));
            
            refreshButton.addActionListener(new refreshAction());
            
            mainPanel.add(imgCanvas);
            mainPanel.add(refreshButton);
            
            getContentPane().add(mainPanel);
            setSize(700,700);
            setVisible(true);
            
            
            
            
    
        }
        
        
        
    
        public static void main(String[] args) {
            
            KlamathWebCam app = new KlamathWebCam("Klamath.net WebCam");
            
           }
            
    /////////////////////////////////////////////////////////////////////////////////////////////      
           public void makeConnection() {
               
               try {
                   
                imageLocation = new URL("http", "www.klamath.net", "/main2.jpg");  
                System.out.println(mainPanel + " <-----MainPanel");
    
            }
    
            catch (MalformedURLException ex) {
                
                System.out.println("URL did not form properly");
                
            }
            
           System.out.println(imageLocation);
           
        
           
    ///////////////////////////////////////////////
    //Now connect to the url
    
           try {
                   
               connection = imageLocation.openConnection();
               System.out.println(connection + " CONNECTION");
               
               System.out.println("IMAGE OBTAINED!");
               
    
               System.out.println(connection.getContent() + " TYPE");
               
               Image mainImg = Toolkit.getDefaultToolkit().getImage(imageLocation);
               tracker = new MediaTracker(this);
               tracker.addImage(mainImg,0);
               System.out.println(tracker.checkAll());
               tracker.waitForAll(10*1000); //wait for 10 seconds before giving up :)
               System.out.println(mainImg + " <------Image");
    
    
               
               
               
            }
            
            catch (IOException ex) { ex.printStackTrace();}
            
            catch (InterruptedException ex) { ex.printStackTrace(); }
            
            catch (java.security.AccessControlException ex) { 
                
                System.out.println("It won't let me in!"); 
                ex.printStackTrace();
            }
            
            
               
               
           }
    ////////////////////////////////////////////////////////////////////////////////////////////////
    
    
     class MainCanvas extends Canvas {
    
        MainCanvas() { System.out.println("MainCanvas was created!!"); }
        
        public void paint(Graphics g) {
    
            System.out.println(">>>>>>>>>PAINT!<<<<<<<<");
            g.drawImage(mainImg,10,10,this);
            //this.setBackground(new Color(20,200,55));
       
            
        }
    }//<--End of MainCanvas
    
         class refreshAction implements ActionListener {
            
            public void actionPerformed (ActionEvent e) {
                System.out.println("Action Performed.");
                System.out.println("Was Image Loaded??? " + tracker.checkAll());
                imgCanvas.repaint();
    
            }
            
        }//<--End of refreshAction
    
    }//<--End of KlamathWebCam
    
       
    
    
     
  17. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #17
    I found the problem, but I had to play around a bit before I noticed it.

    You define mainImg twice, as a class variable and then as a local variable in the makeConnection method. The image is loaded into the local reference and then discarded, while the class variable remains a null-reference.
     
  18. thread starter macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Location:
    Oregon
    #18
    No wonder it wasn't working! Aarrgh! Thanks for helping me so much.
     

Share This Page