DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

FileToImageGenerator

08.30.2006
| 1533 views |
  • submit to reddit
        // creates a simple colored image out of a text file

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class FileToImageGenerator {

	private Display display;
	private Shell shell;
	private File imgFile;
	private Text textFile;
	private Canvas canvas;
	private Color clearColor;

	public FileToImageGenerator() {
		display = new Display();
		shell = new Shell(display);
		
//		shell.setSize(500, 300);
		shell.setText("FileToImageGenerator");

		createGUI();
		
		shell.open();
		
		while(!shell.isDisposed()) {
			if(!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
	
	private void createGUI() {
		shell.setLayout(new GridLayout(3, false));
		
		Label label = new Label(shell, SWT.NONE);
		label.setText("File:");
		label.setLayoutData(new GridData());
		
		textFile = new Text(shell, SWT.BORDER | SWT.READ_ONLY);
		textFile.setText("");
		textFile.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Button b = new Button(shell, SWT.BORDER);
		b.setText("Open...");
		b.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				FileDialog dlg = new FileDialog(shell);
				dlg.open();
				
				imgFile = new File(dlg.getFilterPath() + File.separator + dlg.getFileName());
				
				if(imgFile.isDirectory()) {
					imgFile = null;
				} else {
					generateImageFromFile(imgFile);
				}
			}
		});
		b.setLayoutData(new GridData());
		b.setFocus();
		
		canvas = new Canvas(shell, SWT.BORDER);
		GridData gd = new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL);
		gd.horizontalSpan = 3;
		canvas.setLayoutData(gd);
		clearColor = canvas.getBackground();
	}

	private void generateImageFromFile(File i) {
		textFile.setText(i.getAbsolutePath());

		Rectangle bounds = canvas.getBounds();
		GC gc = new GC(canvas);
		canvas.setBackground(clearColor);
		gc.fillRectangle(0, 0, bounds.width, bounds.height);
		
		Random r = new Random(System.nanoTime());
		
		try {
			BufferedReader fr = new BufferedReader(new FileReader(i.getAbsoluteFile()));
			int b = -1;
			int x = 0;
			int y = 0;
			while((b = fr.read()) != -1) {
//				System.err.print((char)b);
//				gc.drawString("" + (char)b, x, y);
				gc.setBackground(new Color(display, r.nextInt(255), r.nextInt(255), r.nextInt(255)));
				gc.fillRectangle(x, y, 10, 15);
				
				x += 10;
//				if(b == '\n') {
				if(x >= bounds.width) {
					x = 0;
					y += 15;
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public static void main(String[] args) {
		new FileToImageGenerator();
	}

}