Handwritten Java visualizer example (Corpus Semeion)

This post shows a simple Java-code to visualize the handwritten digits of semeion dataset.

 

/**
	@author vlarobbyk
	Clase en Java que permite generar una imagen del digito que se ha escrito.
*/

public class Visualizador extends javax.swing.JFrame{
	
	private String fichero;
	private final int TAM=16;
	private int digito[][]=null;

	public Visualizador(String fichero){
		super("Visualizador de Digitos");
		this.fichero=fichero;
		setSize(510,510);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		getContentPane().setBackground(java.awt.Color.WHITE);
		javax.swing.JMenuBar barra=new javax.swing.JMenuBar();
		javax.swing.JMenu archivo=new javax.swing.JMenu("Archivo");
		javax.swing.JMenuItem sel=new javax.swing.JMenuItem("Seleccionar Fila");
		sel.addActionListener(new java.awt.event.ActionListener(){
			public void actionPerformed(java.awt.event.ActionEvent e){
				String valor=javax.swing.JOptionPane.showInputDialog(Visualizador.this,"Ingrese la fila");
				try{
					Integer val=new Integer(valor);
					if(!(val>=0 && val<1593)){
						javax.swing.JOptionPane.showMessageDialog(Visualizador.this,"El rango valido de filas es [0,1592]");
					}else{
						digito=visualizar(val);
						System.out.println("Visualizando fila: "+val);
						repaint();
					}
				}catch(Exception ex){
					javax.swing.JOptionPane.showMessageDialog(Visualizador.this,"No es un numero valido...");
				}
			}
		});
		archivo.add(sel);
		barra.add(archivo);
		setJMenuBar(barra);
	}
	
	public void paint(java.awt.Graphics g){
		super.paint(g);
		java.awt.Graphics2D g2d=(java.awt.Graphics2D) g;
		java.awt.Dimension d=getSize();
		if(digito!=null){
			System.out.println(d.getWidth()+"!!"+d.getHeight());
			double dy=d.getWidth()/TAM;
			double dx=d.getHeight()/TAM;
			double paso1=dx;
			double paso2=dy;
			g2d.setColor(java.awt.Color.BLUE);
			java.awt.geom.Line2D.Double linea=null;
			java.awt.geom.Rectangle2D.Double rect=null;
			for(int i=0;i<TAM;i++){
				linea=new java.awt.geom.Line2D.Double(0.d,paso1,d.getWidth(),paso1);
				g2d.draw(linea);
				linea=new java.awt.geom.Line2D.Double(paso2,0.d,paso2,d.getHeight());
				g2d.draw(linea);
				paso1+=dx;
				paso2+=dy;
			}
			paso1=dx;
			paso2=dy;
			for(int i=0;i<TAM;i++){
				for(int j=0;j<TAM;j++){
					if(digito[i][j]==1){
						paso1=dx*j;
						paso2=dy*i;
						rect=new java.awt.geom.Rectangle2D.Double(paso1,paso2,dx,dy);
						g2d.fill(rect);
					}
				}
			}
		}
	}
	
	public int[][] visualizar(int fila){
		try{
			int matriz[][]=new int[TAM][TAM];
			java.io.BufferedReader buffer=new java.io.BufferedReader(new java.io.FileReader(fichero));
			int cont=0;
			String data=null;
			while((cont<fila)){
				data=buffer.readLine();
				cont++;
			}
			data=buffer.readLine();
			String datos[]=data.trim().split("(\\s)+");
			for(int i=0;i<datos.length;i++){
				System.out.print(datos[i]+"|");
			}
			for(int i=0,k=0;i<matriz.length;i++){
				for(int j=0;j<matriz[0].length;j++,k++){
					matriz[i][j]=new Double(datos[k]).intValue();
				}
			}
			return matriz;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public static void main(String[]args){
		new Visualizador("corpus_digitos.data").setVisible(true);
	}

}

 

Loading 3D objects in Java

/**
	Example that shows load 3D models in Java
	@author vlarob
*/
public class LoaderObj extends javax.swing.JFrame{

	private javax.media.j3d.Canvas3D canvas3D;
	private javax.media.j3d.BranchGroup bg;
	private javax.media.j3d.TransformGroup tg;
	private com.sun.j3d.utils.universe.SimpleUniverse simpleUniverse;
	/**
		Class constructor: generates the instance of canvas3D and simple universe.
		@param path - Specifies the name and path of 3D object model
	*/
	public LoaderObj(String path){
		super("Ejemplo de carga de objetos 3D");
		setSize(400,400);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		canvas3D=new javax.media.j3d.Canvas3D(com.sun.j3d.utils.universe.SimpleUniverse.getPreferredConfiguration());
		add(canvas3D);
		
		bg=new javax.media.j3d.BranchGroup();
		tg=new javax.media.j3d.TransformGroup();
		tg.setCapability(javax.media.j3d.TransformGroup.ALLOW_TRANSFORM_READ);
		tg.setCapability(javax.media.j3d.TransformGroup.ALLOW_TRANSFORM_WRITE);

		
		javax.media.j3d.BoundingSphere bs=new javax.media.j3d.BoundingSphere();
		com.sun.j3d.utils.behaviors.mouse.MouseRotate mr=new com.sun.j3d.utils.behaviors.mouse.MouseRotate(tg);
		mr.setSchedulingBounds(bs);
		com.sun.j3d.utils.behaviors.mouse.MouseZoom mz=new com.sun.j3d.utils.behaviors.mouse.MouseZoom(tg);
		mz.setSchedulingBounds(bs);
		com.sun.j3d.utils.behaviors.mouse.MouseTranslate mt=new com.sun.j3d.utils.behaviors.mouse.MouseTranslate(tg);
		mt.setSchedulingBounds(bs);

		javax.media.j3d.AmbientLight ambientLight=new javax.media.j3d.AmbientLight();
		ambientLight.setColor(new javax.vecmath.Color3f(1.f,0.f,0.0f));
		ambientLight.setInfluencingBounds(bs);
		tg.addChild(ambientLight);
		
		try{
			com.sun.j3d.loaders.objectfile.ObjectFile objectFile=new com.sun.j3d.loaders.objectfile.ObjectFile(com.sun.j3d.loaders.objectfile.ObjectFile.RESIZE);
			com.sun.j3d.loaders.Scene scene=objectFile.load(ClassLoader.getSystemResource("galleon.obj"));
			tg.addChild(scene.getSceneGroup());
			System.out.println(scene.getNamedObjects());
			System.out.println("Cargado...");
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		javax.media.j3d.TransformGroup tgEscala=new javax.media.j3d.TransformGroup();
		javax.media.j3d.Transform3D t3d=new javax.media.j3d.Transform3D();
// 		t3d.setScale(0.2f);
		tgEscala.setTransform(t3d);
		tgEscala.addChild(tg);

		bg.addChild(tgEscala);
		bg.addChild(mr);
		bg.addChild(mz);
		bg.addChild(mt);
		
		bg.compile();
		simpleUniverse=new com.sun.j3d.utils.universe.SimpleUniverse(canvas3D);
		simpleUniverse.addBranchGraph(bg);
		simpleUniverse.getViewingPlatform().setNominalViewingTransform();
	}
	
	public static void main(String []args){
		new LoaderObj("").setVisible(true);
	}
}

Free 3D models: http://tf3dm.com/3d-models/file/1/obj