in Programming

Handwritten numbers viewer (Developed in Java, version 2)

In this example we can see a small Java-code that loads a corpus of handwritten numbers and draw it on a panel.

public class Graficador extends javax.swing.JFrame{

	private final String RUTA="numeros.data";
	
	private final int KTE_FILAS=16;
	
	private final int KTE_COLS=16;
	
	private java.util.ArrayList numeros;
	
	private Lienzo lienzo;

	public Graficador(){
		super("Graficador Numeros Proyecto Integrador - IA");
		setSize(833,833);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		numeros=new java.util.ArrayList();
		lienzo=new Lienzo();
		setLayout(new java.awt.BorderLayout());
		add(createControls(),java.awt.BorderLayout.NORTH);
		add(lienzo,java.awt.BorderLayout.CENTER);
	}
	
	
	private javax.swing.JPanel createControls(){
		javax.swing.JButton cargar=new javax.swing.JButton("Cargar data");
		final javax.swing.JComboBox lista=new javax.swing.JComboBox();
		javax.swing.JPanel panel=new javax.swing.JPanel(new java.awt.FlowLayout());
		panel.add(cargar);
		panel.add(new javax.swing.JLabel("Entradas de Datos/CORPUS: "));
		panel.add(lista);
		cargar.addActionListener(ae->{
			cargarDataBase(lista);
		});
		lista.addItemListener(ae->{
			int i=lista.getSelectedIndex();
			Numero n=numeros.get(i);
			lienzo.setMatrix(n.matrix);
		});
		return panel;
	}
	
	private class Lienzo extends javax.swing.JPanel{
	
		private int matrix[][];
	
		public Lienzo(){
			setBackground(java.awt.Color.WHITE);
			matrix=null;
		}
		
		public void setMatrix(int m[][]){
			matrix=m;
			repaint();
		}
		
		public void paint(java.awt.Graphics g){
			super.paint(g);
			java.awt.Dimension d=getSize();
			int stepX=((int)d.getWidth())/KTE_COLS;
			int stepY=((int)d.getHeight())/KTE_FILAS;
			g.setColor(java.awt.Color.BLUE);
			for(int i=0;i<(int)d.getWidth();i+=stepX){
				g.drawLine(i,0,i,(int)d.getHeight());
			}
				
			for(int i=0;i<((int)d.getHeight());i+=stepY){
				g.drawLine(0,i,(int)d.getWidth(),i);
			}
			if(matrix!=null){
				g.setColor(java.awt.Color.YELLOW);
				for(int i=0;i<matrix.length;i++){
					for(int j=0;j<matrix[0].length;j++){ if(matrix[j][i]!=0) g.fillRect(i*stepX,j*stepY,stepX,stepY); } } } } } private void cargarDataBase(javax.swing.JComboBox lista){ try{ java.io.BufferedReader buffer=new java.io.BufferedReader(new java.io.FileReader(RUTA)); String linea=""; Numero num=null; int cont=1; while((linea=buffer.readLine())!=null){ if(linea.trim().length()>3){
					num=string2Numero(linea.trim());
					numeros.add(num);
					lista.addItem((cont++)+" => "+num.numero);
				}
			}
			buffer.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private Numero string2Numero(String cad){
		Numero num=new Numero(cad);
		return num;
	}
	
	private class Numero{
		public int matrix[][];
		public String numero;
		
		public Numero(String linea){
			String data[]=linea.trim().split("(\\s+)");
			matrix=new int[KTE_FILAS][KTE_COLS];
			int k=0;
			for (int i=0;i<KTE_FILAS;i++){
				for(int j=0;j<KTE_COLS;j++){
					matrix[i][j]=new Float(data[k++]).intValue();
				}
			}
			
			numero="";
			for (int i=KTE_FILAS*KTE_COLS;i<data.length;i++){
				numero+=data[i]+"   ";
			}
			numero=numero.trim();
		}
	}
	
	public static void main(String[]args){
		Graficador g=new Graficador();
		g.setVisible(true);
	}
	
}

Write a Comment

Comment