Dear all,
I am a newbie in Java GUI...
My goal is that, I have a IPCam, and I want to write a Java GUI
application that refreshing the picture periodically, let say in a
simplier case - simply trigger by a button clicking.
The picture is a jpg image, let say "http://xxxx.com/imagegrab", it
will return the byte of JPG to u.
When I implement my work, I find the image cannot refresh.. It just
keep to be the very first image when I construct the GUI object.
Here is my code:
package testajay;
import java.*;
import java.io.*;
import java.util.*;
import java.net.*;
import java.lang.*;
public class IPCam {
private int n;
private String url_link = new
String("http://192.168.1.253/SnapshotJPEG?Resolution=320x240&Quality=Standard&Interval=30");
private int temp;
int counter = 0;
public byte[] buffer = new byte[100000];
public IPCam(String link) {
this.download_URL(url_link, "./", link);
}
private byte[] downloadByteCodesFromURL(DataInputStream in) {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while (true) {
try {
outStream.write(in.readByte());
}
catch (IOException e) {
break;
}
}
return outStream.toByteArray();
}
public void download_URL(String url_string, byte[] buf){
try{
URL u = new URL(url_string);
DataInputStream data = new DataInputStream(u.openStream());
// Download byte codes from URL Stream
buf = downloadByteCodesFromURL(data);
System.out.println("Downloaded! length = "+buf.length);
temp = buf.length;
}catch (Exception e){
e.printStackTrace();
}
}
public void download_URL(String url_string, String file_directory,
String filename){
try{
File saveFile = new File(file_directory, filename);
FileOutputStream saveFileStream = new FileOutputStream(saveFile);
URL u = new URL(url_string);
DataInputStream data = new DataInputStream(u.openStream());
// Download byte codes from URL Stream
byte classBytes[] = downloadByteCodesFromURL(data);
saveFileStream.write(classBytes, 0, classBytes.length);
saveFileStream.flush();
saveFileStream.close();
System.out.println("Written! length = "+classBytes.length);
}catch (Exception e){
e.printStackTrace();
}
}
}
package testajay;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestAjay extends JFrame{
private JPanel picPanel;
private JButton refresh;
private Image image;
JScrollPane jsp;
private JLabel test_label;
private int counter = 0;
public TestAjay(){
IPCam test = new IPCam("test.jpg");
counter++;
Container c = getContentPane();
c.setLayout(new BorderLayout());
refresh = new JButton("refresh");
image = Toolkit.getDefaultToolkit().getImage("test.jpg");
refresh.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getActionCommand() == "refresh"){
String baby;
if (counter%2==1){
baby = new String("test1.jpg");
}else{
baby = new String("test.jpg");
}
// I write this routine to test...
// everytime it will display intial test.jpg(the very first jpg I grab
when I construct the GUI), when I first click refresh, it will return
test1.jpg.... but from that on, I will only see test1.jpg no matter I
am requsting test.jpg or test1.jpg or not...
// I am very sure of that test1.jpg and test.jpg is changed and written
to a file correctly when I call IPCam test = new IPCam(baby);
IPCam test = new IPCam(baby);
ImageComponent ico = new ImageComponent(baby);
jsp.setViewportView(ico);
}
}
});
picPanel = new JPanel();
picPanel.setLayout(new BorderLayout());
ImageComponent ic = new ImageComponent(image);
jsp = new JScrollPane(ic);
picPanel.add(jsp, BorderLayout.CENTER);
c.add(refresh,BorderLayout.NORTH);
c.add(picPanel,BorderLayout.CENTER);
setSize(400,400);
setVisible(true);
}
class ImageComponent extends JLabel {
Image img;
ImageIcon ii;
String imgFname;
public ImageComponent(Image m){
super();
img = m;
ii = new ImageIcon(img);
setIcon(ii);
}
public ImageComponent(String fnam){
super();
imgFname = fnam;
ii = new ImageIcon(imgFname);
setIcon(ii);
}
}
public static void main(String[] args){
TestAjay app = new TestAjay();
app.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
faiyeah - 31 Mar 2006 02:34 GMT
Sorry that I forget to thank you all~