JFrame _ JPanel _ DrawImage

P

papachris79

Hello to everyone!!

I'm working on a program that downloads photos (jpeg) from a server's
camera and that will show each photo on a frame as soon as it's
downloaded. Kinda like a motion jpeg (mjpeg).
I've managed to get the photos but i'm having trouble displaying them.
From what i understand, i must create a JFrame, then a JPanel to work
with the frame and last DrawImage will draw the images on the frame.
Can someone please show me how i can do this?

This is pretty much how my program looks like....
******************************************************************************
byte [] hostIP = {...};
InetAddress hostAddress = InetAddress.getByAddress(hostIP);
byte [] rxbuffer = new byte[1024];
DatagramSocket r = new DatagramSocket(clientPort);
DatagramPacket q = new DatagramPacket(rxbuffer,rxbuffer.length);
byte [] txbuffer = imageInfo.getBytes();
DatagramSocket s = new DatagramSocket();
DatagramPacket p = new DatagramPacket
(txbuffer,txbuffer.length,hostAddress,serverPort);
r.setSoTimeout(200);

newPhoto:
for (int j = 0; j<10; j++) {
File ImageFile = new File("Image"+String.valueOf(j+1)+".jpeg");
FileOutputStream Image = new FileOutputStream(ImageFile);
s.send(p);
for(;;){
try {
r.receive(q);
Image.write(rxbuffer,0,q.getLength());
} catch (Exception x1) {continue newPhoto;}
}
}
************************************************************************************

Thanks

Christos Papageorgiou
 
K

Knute Johnson

Hello to everyone!!

I'm working on a program that downloads photos (jpeg) from a server's
camera and that will show each photo on a frame as soon as it's
downloaded. Kinda like a motion jpeg (mjpeg).
I've managed to get the photos but i'm having trouble displaying them.
From what i understand, i must create a JFrame, then a JPanel to work
with the frame and last DrawImage will draw the images on the frame.
Can someone please show me how i can do this?

This is pretty much how my program looks like....
******************************************************************************
byte [] hostIP = {...};
InetAddress hostAddress = InetAddress.getByAddress(hostIP);
byte [] rxbuffer = new byte[1024];
DatagramSocket r = new DatagramSocket(clientPort);
DatagramPacket q = new DatagramPacket(rxbuffer,rxbuffer.length);
byte [] txbuffer = imageInfo.getBytes();
DatagramSocket s = new DatagramSocket();
DatagramPacket p = new DatagramPacket
(txbuffer,txbuffer.length,hostAddress,serverPort);
r.setSoTimeout(200);

newPhoto:
for (int j = 0; j<10; j++) {
File ImageFile = new File("Image"+String.valueOf(j+1)+".jpeg");
FileOutputStream Image = new FileOutputStream(ImageFile);
s.send(p);
for(;;){
try {
r.receive(q);
Image.write(rxbuffer,0,q.getLength());
} catch (Exception x1) {continue newPhoto;}
}
}
************************************************************************************

Thanks

Christos Papageorgiou

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.*;

public class ImagePanel extends JPanel {
BufferedImage bi;

public ImagePanel(String fname) {
try {
bi = ImageIO.read(new File(fname));
setPreferredSize(new Dimension(bi.getWidth(),bi.getHeight()));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

public void paintComponent(Graphics g) {
if (bi == null)
g.drawString("No Image",10,40);
else
g.drawImage(bi,0,0,null);
}

public static void createGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImagePanel ip = new ImagePanel("saturn.jpg");
JScrollPane sp = new JScrollPane(ip);
f.add(sp);
f.setSize(320,240);
f.setVisible(true);
}

public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
createGUI();
}
};
EventQueue.invokeLater(r);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top