Table des matières

Java Tips

Plein de choses interressantes dans le livre “Killer Game Programming in Java”. Des extraits de chapitres et de codes: http://fivedots.coe.psu.ac.th/~ad/jg/index.html.

http://filthyrichclients.org/ Le bouquin de Romain Guy & Chet Haase. In Filthy Rich Clients, we explain how to create filthy rich effects in your applications, from the fundamental graphics, GUI, and animation technology up through sample code and algorithms for the effects themselves. Des exemples de code.

Java 2D games tutorial

Swing

CVS Format

Read input from console

Read console input using java.io.Console instance - Don't work in Eclipse IDE.

private static void usingConsoleReader()
{
  Console  console = System.console();
  if (console != null)
  {
     String inputString = console.readLine("Name: ");
     System.out.println("Name entered : " + inputString);
  }
}

Read console input using BufferedReader instance

private static void usingBufferedReader()
{
  BufferedReader bufferRead = new BufferedReader( new InputStreamReader( System.in ) );
  String inputString = bufferRead.readLine();
  System.out.println( inputString );
}

Read console input using Scanner instance

private static void usingScanner()
{
  Scanner scanIn = new Scanner( System.in );
  String inputString = scanIn.nextLine();
  scanIn.close();           
  System.out.println(inputString);
}

Process Input Stream Reader

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.io.*;
 
public class Traceroute {
 public static void main(String[] args){
  Runtime t = Runtime.getRuntime();
  try {		
   Process p = t.exec("traceroute  "+args[0]);
   //Process p = t.exec("ping -w 1 "+args[0]);		
   InputStream i = p.getInputStream();		
   InputStreamReader r = new InputStreamReader(i);
   BufferedReader re = new BufferedReader(r);
   int m =0;
   while(true){
    String content = re.readLine();		
    System.out.println(content);
    if(content == null){
     break;
    }			
   }		
   re.close();
   r.close();
  } catch (IOException e) {
    e.printStackTrace();
  } 				
 }	
}

Convert a Java OutputStream to an InputStream

http://ostermiller.org/convert_java_outputstream_inputstream.html

Method 1: Buffer the data using a byte array

  ByteArrayOutputStream out = new ByteArrayOutputStream();
  class1.putDataOnOutputStream(out);
  class2.processDataFromInputStream(
    new ByteArrayInputStream(out.toByteArray())
  );

Method 2: Use pipes

PipedInputStream in = new PipedInputStream();
  PipedOutputStream out = new PipedOutputStream(in);
  new Thread(
    new Runnable(){
      public void run(){
        class1.putDataOnOutputStream(out);
      }
    }
  ).start();
  class2.processDataFromInputStream(in);

Method 3: Use Circular Buffers

  // Multiple Threaded Example of a Circular Buffer
 
  CircularByteBuffer cbb = new CircularByteBuffer();
  new Thread(
    new Runnable(){
      public void run(){
        class1.putDataOnOutputStream(cbb.getOutputStream());
      }
    }
  ).start();
  class2.processDataFromInputStream(cbb.getInputStream());
  // Single Threaded Example of a Circular Buffer
 
  // buffer all data in a circular buffer of infinite size
  CircularByteBuffer cbb = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);
  class1.putDataOnOutputStream(cbb.getOutputStream());
  class2.processDataFromInputStream(cbb.getInputStream());

Serial communication

Communication série avec Java