Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

JFrame _ JPanel _ DrawImage

Thread view: 
papachris79@hotmail.com - 15 May 2006 20:26 GMT
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
Knute Johnson - 15 May 2006 23:25 GMT
> Hello to everyone!!
>
[quoted text clipped - 36 lines]
>
> 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);
    }
}

Signature

Knute Johnson
email s/nospam/knute/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.