JFrame _ JPanel _ DrawImage

Discussion in 'Java' started by papachris79@hotmail.com, May 15, 2006.

  1. Guest

    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
     
    , May 15, 2006
    #1
    1. Advertising

  2. wrote:
    > 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);
    }
    }

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, May 15, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green
    Replies:
    0
    Views:
    3,062
    Roedy Green
    Jul 30, 2003
  2. Frédéric Colombero
    Replies:
    0
    Views:
    1,096
    Frédéric Colombero
    Nov 5, 2003
  3. Andreas Beresko

    Add a JPanel to a JPanel...

    Andreas Beresko, Jul 14, 2004, in forum: Java
    Replies:
    6
    Views:
    27,028
  4. Raphael Jolivet

    [JPanel] Drop Down JPanel

    Raphael Jolivet, Jul 18, 2008, in forum: Java
    Replies:
    1
    Views:
    1,543
    John B. Matthews
    Jul 18, 2008
  5. t1m1976
    Replies:
    1
    Views:
    2,444
    t1m1976
    Nov 7, 2010
Loading...

Share This Page