PDA

View Full Version : Getting Permission To Play With a Picture




bobber205
Aug 1, 2006, 07:59 PM
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
Aug 1, 2006, 08:57 PM
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
Aug 1, 2006, 09:08 PM
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.

bobber205
Aug 1, 2006, 09:12 PM
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
Aug 1, 2006, 09:14 PM
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
Aug 1, 2006, 09:21 PM
Any suggestions on how to easily convert my applet so far into a standalone app?

gekko513
Aug 1, 2006, 09:29 PM
It might work to just add it in a frame and go from there...



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
Aug 1, 2006, 10:29 PM
Brilliance! It's much too bad I already rewrote it though... thanks!

bobber205
Aug 1, 2006, 10:43 PM
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
Aug 1, 2006, 10:59 PM
URL url = new URL("http://www.klamath.net/main2.jpg");
Image img = Toolkit.getDefaultToolkit().getImage(url);
might do the trick

bobber205
Aug 2, 2006, 04:08 PM
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.

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
Aug 4, 2006, 12:01 AM
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...

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

gekko513
Aug 4, 2006, 12:23 AM
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:

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
Aug 4, 2006, 12:30 AM
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
Aug 4, 2006, 12:43 PM
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
Aug 4, 2006, 01:20 PM
Code is below.

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
Aug 4, 2006, 08:20 PM
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.

bobber205
Aug 4, 2006, 08:24 PM
No wonder it wasn't working! Aarrgh! Thanks for helping me so much.