Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 1, 2006, 07:59 PM   #1
bobber205
macrumors 68020
 
bobber205's Avatar
 
Join Date: Nov 2005
Location: Oregon
Send a message via AIM to bobber205 Send a message via Yahoo to bobber205 Send a message via Skype™ to bobber205
Getting Permission To Play With a Picture

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?
__________________
Gyaboo!!!!
bobber205 is offline   0 Reply With Quote
Old Aug 1, 2006, 08:57 PM   #2
bobber205
Thread Starter
macrumors 68020
 
bobber205's Avatar
 
Join Date: Nov 2005
Location: Oregon
Send a message via AIM to bobber205 Send a message via Yahoo to bobber205 Send a message via Skype™ to bobber205
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?
__________________
Gyaboo!!!!
bobber205 is offline   0 Reply With Quote
Old Aug 1, 2006, 09:08 PM   #3
gekko513
macrumors 601
 
gekko513's Avatar
 
Join Date: Oct 2003
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.
__________________
Someone get Nekkid™, now!

Last edited by gekko513; Aug 1, 2006 at 09:13 PM.
gekko513 is offline   0 Reply With Quote
Old Aug 1, 2006, 09:12 PM   #4
bobber205
Thread Starter
macrumors 68020
 
bobber205's Avatar
 
Join Date: Nov 2005
Location: Oregon
Send a message via AIM to bobber205 Send a message via Yahoo to bobber205 Send a message via Skype™ to bobber205
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?
__________________
Gyaboo!!!!
bobber205 is offline   0 Reply With Quote
Old Aug 1, 2006, 09:14 PM   #5
gekko513
macrumors 601
 
gekko513's Avatar
 
Join Date: Oct 2003
Quote:
Originally Posted by bobber205
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?
Yes, there are no such limitations on a standalone app.
__________________
Someone get Nekkid™, now!
gekko513 is offline   0 Reply With Quote
Old Aug 1, 2006, 09:21 PM   #6
bobber205
Thread Starter
macrumors 68020
 
bobber205's Avatar
 
Join Date: Nov 2005
Location: Oregon
Send a message via AIM to bobber205 Send a message via Yahoo to bobber205 Send a message via Skype™ to bobber205
Any suggestions on how to easily convert my applet so far into a standalone app?
__________________
Gyaboo!!!!
bobber205 is offline   0 Reply With Quote
Old Aug 1, 2006, 09:29 PM   #7
gekko513
macrumors 601
 
gekko513's Avatar
 
Join Date: Oct 2003
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.
__________________
Someone get Nekkid™, now!

Last edited by gekko513; Aug 1, 2006 at 09:34 PM.
gekko513 is offline   0 Reply With Quote
Old Aug 1, 2006, 10:29 PM   #8
bobber205
Thread Starter
macrumors 68020
 
bobber205's Avatar
 
Join Date: Nov 2005
Location: Oregon
Send a message via AIM to bobber205 Send a message via Yahoo to bobber205 Send a message via Skype™ to bobber205
Brilliance! It's much too bad I already rewrote it though... thanks!
__________________
Gyaboo!!!!
bobber205 is offline   0 Reply With Quote
Old Aug 1, 2006, 10:43 PM   #9
bobber205
Thread Starter
macrumors 68020
 
bobber205's Avatar
 
Join Date: Nov 2005
Location: Oregon
Send a message via AIM to bobber205 Send a message via Yahoo to bobber205 Send a message via Skype™ to bobber205
Now that I have a connection going, how do I download the image w/o having to process individual bytes?

Because that'll suck.
__________________
Gyaboo!!!!
bobber205 is offline   0 Reply With Quote
Old Aug 1, 2006, 10:59 PM   #10
gekko513
macrumors 601
 
gekko513's Avatar
 
Join Date: Oct 2003
Code:
URL url = new URL("http://www.klamath.net/main2.jpg");
Image img = Toolkit.getDefaultToolkit().getImage(url);
might do the trick
__________________
Someone get Nekkid™, now!
gekko513 is offline   0 Reply With Quote
Old Aug 2, 2006, 04:08 PM   #11
bobber205
Thread Starter
macrumors 68020
 
bobber205's Avatar
 
Join Date: Nov 2005
Location: Oregon
Send a message via AIM to bobber205 Send a message via Yahoo to bobber205 Send a message via Skype™ to bobber205
Quote:
Originally Posted by gekko513
Code:
URL url = new URL("http://www.klamath.net/main2.jpg");
Image img = Toolkit.getDefaultToolkit().getImage(url);
might do the trick

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!

Whew. I was so close to that method too. The main thing I left out was the "getDefaultToolKit" part....
__________________
Gyaboo!!!!
bobber205 is offline   0 Reply With Quote
Old Aug 4, 2006, 12:01 AM   #12
bobber205
Thread Starter
macrumors 68020
 
bobber205's Avatar
 
Join Date: Nov 2005
Location: Oregon
Send a message via AIM to bobber205 Send a message via Yahoo to bobber205 Send a message via Skype™ to bobber205
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.
Attached Files
File Type: zip KlamathWebCam.class.zip (1.8 KB, 4 views)
__________________
Gyaboo!!!!
bobber205 is offline   0 Reply With Quote
Old Aug 4, 2006, 12:23 AM   #13
gekko513
macrumors 601
 
gekko513's Avatar
 
Join Date: Oct 2003
Quote:
Originally Posted by bobber205
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...
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
__________________
Someone get Nekkid™, now!
gekko513 is offline   0 Reply With Quote
Old Aug 4, 2006, 12:30 AM   #14
bobber205
Thread Starter
macrumors 68020
 
bobber205's Avatar
 
Join Date: Nov 2005
Location: Oregon
Send a message via AIM to bobber205 Send a message via Yahoo to bobber205 Send a message via Skype™ to bobber205
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!
__________________
Gyaboo!!!!
bobber205 is offline   0 Reply With Quote
Old Aug 4, 2006, 12:43 PM   #15
bobber205
Thread Starter
macrumors 68020
 
bobber205's Avatar
 
Join Date: Nov 2005
Location: Oregon
Send a message via AIM to bobber205 Send a message via Yahoo to bobber205 Send a message via Skype™ to bobber205
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.
__________________
Gyaboo!!!!
bobber205 is offline   0 Reply With Quote
Old Aug 4, 2006, 01:20 PM   #16
bobber205
Thread Starter
macrumors 68020
 
bobber205's Avatar
 
Join Date: Nov 2005
Location: Oregon
Send a message via AIM to bobber205 Send a message via Yahoo to bobber205 Send a message via Skype™ to bobber205
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
__________________
Gyaboo!!!!
bobber205 is offline   0 Reply With Quote
Old Aug 4, 2006, 08:20 PM   #17
gekko513
macrumors 601
 
gekko513's Avatar
 
Join Date: Oct 2003
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.
__________________
Someone get Nekkid™, now!
gekko513 is offline   0 Reply With Quote
Old Aug 4, 2006, 08:24 PM   #18
bobber205
Thread Starter
macrumors 68020
 
bobber205's Avatar
 
Join Date: Nov 2005
Location: Oregon
Send a message via AIM to bobber205 Send a message via Yahoo to bobber205 Send a message via Skype™ to bobber205
No wonder it wasn't working! Aarrgh! Thanks for helping me so much.
__________________
Gyaboo!!!!
bobber205 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
OS X: Star Wars Empire at War - Picture-in-picture GFX Glitch swwack91 Mac and PC Games 7 May 15, 2014 07:04 AM
iMovie 'picture in picture' image size question shuffles Mac Basics and Help 6 Jan 18, 2013 11:48 AM
Solve picture puzzles and play along with First Sight on iOS BombApp iPhone and iPod touch Apps 0 Oct 30, 2012 01:05 PM
Getting a movie to play inside a picture of an iPad Greencardman Digital Video 7 Sep 13, 2012 12:22 PM
How to make multiple Picture-In-Picture with iMovie VinegarTasters Digital Video 7 Sep 2, 2012 11:33 PM

Forum Jump

All times are GMT -5. The time now is 04:08 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC