Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
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?
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
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?
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1
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 http://www.klamath.net to get access to http://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.
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
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?
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1
bobber205 said:
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.
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
Any suggestions on how to easily convert my applet so far into a standalone app?
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1
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.
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
Now that I have a connection going, how do I download the image w/o having to process individual bytes?

Because that'll suck. ;)
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1
Code:
URL url = new URL("http://www.klamath.net/main2.jpg");
Image img = Toolkit.getDefaultToolkit().getImage(url);
might do the trick
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
gekko513 said:
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! :D

Whew. I was so close to that method too. The main thing I left out was the "getDefaultToolKit" part....
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
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
 

Attachments

  • KlamathWebCam.class.zip
    1.8 KB · Views: 121

gekko513

macrumors 603
Oct 16, 2003
6,301
1
bobber205 said:
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
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
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!
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
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.
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
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
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.