snafu8 | Salut à tous,
je commence par préciser que je ne suis PAS en train de faire mes devoirs.
Je dois écrire un programme qui ouvre un socket TCP sur un port fixé, récupère une image par ce socket et l'affiche en plein écran sur l'écran secondaire d'une machine. Je suis très loin d'être un expert en IHM (et en java d'ailleurs). Pour l'instant j'ai un bout de code qui me permet d'ouvrir le socket et de récupérer l'image :
Code :
- public class test {
- public static void main(String[] args) {
- int listenPort = 25001;
- try {
- ServerSocket socket= new ServerSocket(listenPort);
- Socket clientSocket = null;
- while(true){
- clientSocket = socket.accept();
- InputStream input = clientSocket.getInputStream();
- BufferedInputStream input_r = new BufferedInputStream(input);
- byte[] message= new byte[5];
- byte[] width_b = new byte[4];
- byte[] length_b = new byte[4];
- input.read(message,0,5);
- input.read(length_b,0,4);
- input.read(width_b,0,4);
- int width = ByteBuffer.wrap(width_b).getInt();
- int length = ByteBuffer.wrap(length_b).getInt();
- String s = new String(message);
- System.out.println("Message:" + s + " width:" + width + " length:" + length);
- byte[][] data = new byte[length][width];
- for(int i=0;i<length;i++){
- input.read(data[i],0,width);
- }
- }
- }
- catch (IOException e) {
- System.out.println("Accept failed: 4444" );
- System.exit(-1);
- }
- }
- }
|
et j'ai récupéré sur l'interweb un snippet qui fait la deuxième partie :
Code :
- public class fullscreen extends JFrame {
- // this line is needed to avoid serialization warnings
- private static final long serialVersionUID = 1L;
- Image screenImage; // downloaded image
- int w, h; // Display height and width
- // Program entry
- public static void main(String[] args) throws Exception {
- if (args.length < 1) // by default program will load AnyExample logo
- new fullscreen("http://www.anyexample.com/i/logo.gif" );
- else
- new fullscreen(args[0]); // or first command-line argument
- }
- // Class constructor
- fullscreen(String source) throws MalformedURLException {
- // Exiting program on window close
- addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- // Exitig program on mouse click
- addMouseListener(new MouseListener() {
- public void mouseClicked(MouseEvent e) { System.exit(0); }
- public void mousePressed(MouseEvent e) {}
- public void mouseReleased(MouseEvent e) {}
- public void mouseEntered(MouseEvent e) {}
- public void mouseExited(MouseEvent e) {}
- }
- );
- // remove window frame
- this.setUndecorated(true);
- // window should be visible
- this.setVisible(true);
- // switching to fullscreen mode
- GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
- GraphicsDevice[] gs = ge.getScreenDevices();
- gs[gs.length-1].setFullScreenWindow(this);
- // getting display resolution: width and height
- w = this.getWidth();
- h = this.getHeight();
- System.out.println("Display resolution: " + String.valueOf(w) + "x" + String.valueOf(h));
- // loading image
- if (source.startsWith("http://" )) // http:// URL was specified
- screenImage = Toolkit.getDefaultToolkit().getImage(new URL(source));
- else
- screenImage = Toolkit.getDefaultToolkit().getImage(source); // otherwise - file
- }
- public void paint (Graphics g) {
- if (screenImage != null) // if screenImage is not null (image loaded and ready)
- g.drawImage(screenImage, // draw it
- w/2 - screenImage.getWidth(this) / 2, // at the center
- h/2 - screenImage.getHeight(this) / 2, // of screen
- this);
- // to draw image at the center of screen
- // we calculate X position as a half of screen width minus half of image width
- // Y position as a half of screen height minus half of image height
- }
- }
|
Ma question est assez simple, comme je peux fusionner les deux? |