Actualiser une image à partir d'un bouton
Bonjour,
Je suis débutant en Java et dans le cadre d'un projet, je voudrais pouvoir envoyer une image d'un serveur à un client puis que ce client modifie l'image avant de la renvoyer à ce même serveur.
Malheureusement je ne suis pas très doué, j'ai réussi à m'en sortir pour la 1ère partie mais je n'arrive pas à appliquer la modification de l'image sur la panneau d'affichage...
Merci de votre aide!
Code serveur:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| public class SendImage extends JFrame {
BufferedImage bi;
JButton btn;
ServerSocket serverSocket;
Socket socket;
public SendImage() {
super("SendImage");
bi = createImage();
this.add(btn = new JButton("Send"), BorderLayout.NORTH);
this.add(new JLabel(new ImageIcon(bi)), BorderLayout.CENTER);
this.setSize(511, 512);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
try{
serverSocket = new ServerSocket(8000);
socket = serverSocket.accept();
}
catch(IOException ex){
System.err.println(ex);
}
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
ImageIO.write(bi, "PNG", socket.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public BufferedImage createImage() {
BufferedImage bi = null;
try {
bi = ImageIO.read(new File("java.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bi;
}
public static void main(String[] args) {
new SendImage();
}
} |
Code Client:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| public class ReceiveImage extends JFrame {
JButton btn1;
JButton btn2;
BufferedImage image;
public ReceiveImage() {
super("ReceiveImage");
this.setSize(511, 522);
this.add(btn1 = new JButton("Modify"),BorderLayout.NORTH);
this.add(btn2= new JButton("Re Send"),BorderLayout.SOUTH);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
try
{
Socket socket = new Socket("127.0.0.1", 8000);
BufferedImage image = ImageIO.read(socket.getInputStream());
this.image = image;
this.add(new JLabel(new ImageIcon(image)), BorderLayout.CENTER);
}
catch (IOException e)
{
// TODO Auto-generated catch block
System.err.println(e);
}
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
BufferedImage imageFlou = new BufferedImage(image.getWidth(),image.getHeight(), image.getType());
float[ ] masqueFlou =
{
0.1f, 0.1f, 0.1f,
0.1f, 0.2f, 0.1f,
0.1f, 0.1f, 0.1f
};
Kernel masque = new Kernel(3, 3, masqueFlou);
ConvolveOp opération = new ConvolveOp(masque);
opération.filter(image, imageFlou);
image = imageFlou;
repaint();
}
});
revalidate();
}
public static void main(String[] args) {
new ReceiveImage();
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
} |