Reading from file for picture info

Hi Fellows,

I have this code right here, Im trying to reading the data from the binary file (It has 49,960 numbers which are between 0-255) called rosE_plot.datb.200. The last 200 in the file-name means there are 199 more files before that file to do the same procedure(translate from binary to picture file) and this one is 200th. Then I have to attach them all with each other and make a movie file...

I wanted to read the data from the file and paint the screen pixels one by one but it didnt work...

public class BinaryRead extends JComponent {

BufferedImage image;

File file;

FileInputStream input;

Graphics graph;

public BinaryRead() {

try

{

file = new File("rosE_plot.datb.200");

input = new FileInputStream(file);

int numberBytes = input.available();

byte bytearray[] = new byte[numberBytes];

input.read(bytearray);

int[] data = new int[numberBytes];

int width = getSize().width;

int height = getSize().height;

for (int i = 0; i < numberBytes; i++) {

data = ByteToInt(bytearray);

System.out.println(data);

}

input.close();

// image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

// image.setRGB(0, 0, width, height, data, 0, width);

for (int y = 0; y < height; y++) {

for (int x = 0; x < width; x++) {

paintpixel(graph, x, y);

}

}

}

catch(FileNotFoundException FNFE)

{

System.out.println(FNFE.toString());

System.out.println("Unable to open the data file.");

System.exit(0);

}

catch(IOException IOE)

{

System.out.println(IOE.toString());

System.out.println("Could not read the input.");

System.exit(0);

}

}

public static int ByteToInt(byte b) {

return (int) b & 0xFF;

}

public void paintpixel(Graphics g, int x, int y){

g.setColor(Color.BLUE); //instead of Blue I want to put the data array for each pixel

g.fillRect(x,y,1,1);

}

public static void main(String args[])

{

JFrame f = new JFrame("BinaryRead");

f.getContentPane().add(new BinaryRead());

f.setSize(400, 400);

f.setLocation(100, 100);

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

f.setVisible(true);

}

}

[2461 byte] By [ShayneTa] at [2007-11-27 0:18:45]
# 1

Your painting code is wrong.

All custom painting in swing should be done in paintComponent.

Read in the tutorial how to perform custom drawing:

http://java.sun.com/docs/books/tutorial/uiswing/painting/practice.html

Or you can draw on a BufferedImage and then place it into a JLabel component intead of extending JComponent.

See my example here:

http://forum.java.sun.com/thread.jspa?threadID=5133706&messageID=9483545

Rodney_McKaya at 2007-7-11 22:09:18 > top of Java-index,Security,Cryptography...