tloip
asked on
display image
I have a problem to display the loaded image at another client whiteboard.
For example, i load an image at Client1, the image will displayed at Client1 but also at Client2 as well.
I got appendText() and sendImage() code that help my program but it couldn't work.
>> what i missed out??? :- s
public void actionPerformed(ActionEven t e) {
if (e.getActionCommand().equa ls(lload) ) {
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTIO N) {
File file = fc.getSelectedFile();
ctrl.loadImage(file.getAbs olutePath( ));
sendImage(file);
}
}
}
public void sendImage(File file) {
//find length of file
long len = file.length();
//read file into byte array
byte[] byteArray = new byte[(int)len];
try {
FileInputStream fstream = new FileInputStream(file);
if (fstream.read(byteArray) < len) {
//error could not load file
} else {
out.println("IMAGE " + len + ",");
out.write(byteArray, 0, (int)len); //write file to stream
}
} catch(Exception e){}
}
public static void appendText(String text) {
if (text.startsWith("IMAGE ") ) {
int len = (new Integer( text.substring(6, text.indexOf(",")))).intVa lue();
//get x and y coordinates
byte[] data = new byte[ (int)len ];
int read = 0;
try {
while (read < len) {
data = text.getBytes( text.substring(0, len) );
}
} catch (Exception e) {}
Image theImage = null;
theImage = ctrl.getToolkit().createIm age(data);
ctrl.getToolkit().prepareI mage(theIm age, -1, -1, ctrl);
while ((ctrl.getToolkit().checkI mage(theIm age, -1, -1, ctrl) & ctrl.ALLBITS) == 0) {}
ctrl.drawPicture(0, 0, theImage);
}
}
} //end of appendText(String)
For example, i load an image at Client1, the image will displayed at Client1 but also at Client2 as well.
I got appendText() and sendImage() code that help my program but it couldn't work.
>> what i missed out??? :- s
public void actionPerformed(ActionEven
if (e.getActionCommand().equa
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTIO
File file = fc.getSelectedFile();
ctrl.loadImage(file.getAbs
sendImage(file);
}
}
}
public void sendImage(File file) {
//find length of file
long len = file.length();
//read file into byte array
byte[] byteArray = new byte[(int)len];
try {
FileInputStream fstream = new FileInputStream(file);
if (fstream.read(byteArray) < len) {
//error could not load file
} else {
out.println("IMAGE " + len + ",");
out.write(byteArray, 0, (int)len); //write file to stream
}
} catch(Exception e){}
}
public static void appendText(String text) {
if (text.startsWith("IMAGE ") ) {
int len = (new Integer( text.substring(6, text.indexOf(",")))).intVa
//get x and y coordinates
byte[] data = new byte[ (int)len ];
int read = 0;
try {
while (read < len) {
data = text.getBytes( text.substring(0, len) );
}
} catch (Exception e) {}
Image theImage = null;
theImage = ctrl.getToolkit().createIm
ctrl.getToolkit().prepareI
while ((ctrl.getToolkit().checkI
ctrl.drawPicture(0, 0, theImage);
}
}
} //end of appendText(String)
ASKER
The code i post is done in gui.java
The code u gave, where should i put?in my gui.java or ctrl.java???
>>BufferedImage newImage = ImageIO.read(new ByteArrayInputStream(image Bytes));
>>public class MyImageObserver implements ImageObserver {
public boolean imageUpdate(Image img, int infoflags, int x, int y,
int width, int height) {
//System.out.println("Imag e Displayed");
return true;
}
}
also, you guide me to delete my posted code or add your code to mine as well??
hope to get more your guidance ...thx in advance ;-)
The code u gave, where should i put?in my gui.java or ctrl.java???
>>BufferedImage newImage = ImageIO.read(new ByteArrayInputStream(image
>>public class MyImageObserver implements ImageObserver {
public boolean imageUpdate(Image img, int infoflags, int x, int y,
int width, int height) {
//System.out.println("Imag
return true;
}
}
also, you guide me to delete my posted code or add your code to mine as well??
hope to get more your guidance ...thx in advance ;-)
MyImageObserver is a completely new class....
In your class which handles repainting the frame or other visual just implement a method to update the image information like
public void setMyImage(byte[] imageBytes) {
BufferedImage newImage = ImageIO.read(new ByteArrayInputStream(image Bytes));
//now call repaint() method
repaint();
}
Now add something like this in your paint(Graphics g) method....
public void paint(Graphics g) {
g.drawImage(newImage,0,0,n ew MyImageObserver());
}
In your class which handles repainting the frame or other visual just implement a method to update the image information like
public void setMyImage(byte[] imageBytes) {
BufferedImage newImage = ImageIO.read(new ByteArrayInputStream(image
//now call repaint() method
repaint();
}
Now add something like this in your paint(Graphics g) method....
public void paint(Graphics g) {
g.drawImage(newImage,0,0,n
}
ASKER
So do i still need to include the code i posted?
I tried to put in gui.java file...
Error occured:
cannot resolve symbol
>class BufferedImage
BufferedImage newImage = ImageIO.read(new ByteArrayInputStream(image Bytes));
cannot resolve symbol
>variable ImageIO
BufferedImage newImage = ImageIO.read(new ByteArrayInputStream(image Bytes));
cannot resolve symbol
>variable newImage
g.drawImage(newImage,0,0,n ew MyImageObserver());
cannot resolve symbol
>class MyImageObserver
g.drawImage(newImage,0,0,n ew MyImageObserver());
Anything missed out?or i have placed in wrong file?
I tried to put in gui.java file...
Error occured:
cannot resolve symbol
>class BufferedImage
BufferedImage newImage = ImageIO.read(new ByteArrayInputStream(image
cannot resolve symbol
>variable ImageIO
BufferedImage newImage = ImageIO.read(new ByteArrayInputStream(image
cannot resolve symbol
>variable newImage
g.drawImage(newImage,0,0,n
cannot resolve symbol
>class MyImageObserver
g.drawImage(newImage,0,0,n
Anything missed out?or i have placed in wrong file?
You need to import java.awt.image.BufferedIma ge
also import javax.imageio.ImageIO
need to change this method as the following
public void setMyImage(byte[] imageBytes) {
newImage = ImageIO.read(new ByteArrayInputStream(image Bytes));
//now call repaint() method
repaint();
}
and declare newImage as a variable available in both the methods repaint and setMyImage
You can do this by declaring it in the begining of the class as follows
private BufferedImage newImage = null;
declare a new class MyImageObserver in the same folder or in a place and make it avialable to you code.....
But its diffucult to do it this way....... better is either you post your code or give a link to your code......
also import javax.imageio.ImageIO
need to change this method as the following
public void setMyImage(byte[] imageBytes) {
newImage = ImageIO.read(new ByteArrayInputStream(image
//now call repaint() method
repaint();
}
and declare newImage as a variable available in both the methods repaint and setMyImage
You can do this by declaring it in the begining of the class as follows
private BufferedImage newImage = null;
declare a new class MyImageObserver in the same folder or in a place and make it avialable to you code.....
But its diffucult to do it this way....... better is either you post your code or give a link to your code......
ASKER
u mean this?put in the same file?or save it as another file?
class MyImageObserver{
private BufferedImage newImage = null;
public void setMyImage(byte[] imageBytes) {
newImage = ImageIO.read(new ByteArrayInputStream(image
//now call repaint() method
repaint();
}
}
ASKER
i have 4 files in my program..
too big size to post.
and i don't know how to make a link for this..:-s
too big size to post.
and i don't know how to make a link for this..:-s
ASKER
can u specify clearly for me?
I'm really in doubt on where i need to put the code..
I'm really in doubt on where i need to put the code..
ASKER
error occured: cannot resolve Buffered and newImage
>>>>>>>>>
class MyImageObserver{
private BufferedImage newImage = null;
public void setMyImage(byte[] imageBytes) {
newImage = ImageIO.read(new ByteArrayInputStream(image Bytes));
//now call repaint() method
repaint();
}
}
>>>>>>>>>
class MyImageObserver{
private BufferedImage newImage = null;
public void setMyImage(byte[] imageBytes) {
newImage = ImageIO.read(new ByteArrayInputStream(image
//now call repaint() method
repaint();
}
}
Atleast try to post the client side code...........
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
BufferedImage newImage = ImageIO.read(new ByteArrayInputStream(image
where imageBytes is the byte[] that I received from other client..
in paint method of frame or what ever just say
g.drawImage(newImage,.....
For the image observer required in the above method just write a dummy image observer like the one below and use its instance..
public class MyImageObserver implements ImageObserver {
public boolean imageUpdate(Image img, int infoflags, int x, int y,
int width, int height) {
//System.out.println("Imag
return true;
}
}