A question about rendering BufferedImage In JLabel
I just got a problem regarding display bufferedImage with scrollbar in JLabel.
The display routine worked fine on my machine.
But with all the other machines in the office, the bufferedImage could just be displayed to line 8190 within JLabel regardless the width of the input image.
I tried to change monitor's settings.
With any monitor settings, if I run the routine, only the first 8190 lines in the image could be displayed. From line 8191, nothing is displayed. There is a blank area. Then following this blank area is pure white area which is the background color I set in the code. But at this moment, if I change any settings of the monitor, I'll get the whole bufferedImage displayed properly. Now even I get another input image and display it, the display is complete and correct as long as I do not exit the running of the routine.
If I exit the application(routine) and re-run it, it'll has display problem again. Still just the first 8190 lines in the image could be displayed properly with the rest missing.
But the routine runs well on my machine.
Could any one give me some suggestions on that problem? Thank you so much.
Message was edited by:
wwz
[1229 byte] By [
wwza] at [2007-11-27 0:05:50]

> Post a small demo code that is generally compilable,
> runnable and could reproduce your problem. See:
> http://homepage1.nifty.com/algafield/sscce.html and
> http://www.yoda.arachsys.com/java/newsgroups.html
Here is the code. It takes .pgm image file as input
import java.util.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.lang.*;
import java.applet.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
public class Display extends JFrame
{
Image image = null;
MyCanvas panel;
Graphics gr;
String fileName;
JScrollPane myScrollPane;
int currentScale;
public Display(String inName) throws Exception
{
this.setSize(500,1000);
fileName = inName;
panel = new MyCanvas(image, 500, 1000);
Dimension dimension = new Dimension(500, 1000);
panel.setPreferredSize(dimension);
panel.setSize(dimension);
myScrollPane = new JScrollPane(panel);
this.getContentPane().add(myScrollPane);
this.setBackground(Color.white);
this.setVisible(true);
this.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent evt){
System.exit(0);
}
}
);
display(1, fileName);
}
public void display(int scale, String s) throws Exception
{
currentScale = scale;
FileInputStream fis = new FileInputStream(s);
byte[] readIn = new byte[1];
boolean startPixel = false;
int nextByte = 0;
int newLineCount=0;
int spaceCount = 0;
LinkedList<Integer> widthCharArray = new LinkedList<Integer>();
LinkedList<Integer> heightCharArray = new LinkedList<Integer>();
while(!startPixel)
{
nextByte = fis.read(readIn);
Byte firstB = readIn[0];
int separatorValue = new Byte(Character.DIRECTIONALITY_PARAGRAPH_SEPARATOR).intValue();
if(firstB.intValue() == separatorValue)
{
newLineCount++;
}
if(newLineCount == 2)
{
String st = new String(readIn);
Character firstC = st.charAt(0);
if(Character.isSpaceChar(firstC))
{
spaceCount++;
}else
{
if(firstB.intValue() != separatorValue)
{
if(spaceCount == 0)
{
int temp = (int)(new Integer(st));
widthCharArray.add(new Integer(new String(readIn)));
}
if(spaceCount == 1)
{
int temp = (int)(new Integer(st));
heightCharArray.add(new Integer(new String(readIn)));
}
}
}
}
if(newLineCount == 4)
{
startPixel = true;
}
}
int tempWidth = decodeIntArray(widthCharArray);
int tempHeight = decodeIntArray(heightCharArray);
int newWidth = tempWidth/scale;
int newHeight = tempHeight/scale;
Image tempImage = new java.awt.image.BufferedImage(newWidth, newHeight, java.awt.image.BufferedImage.TYPE_3BYTE_BGR);
Graphics2D tempgr = (Graphics2D)tempImage.getGraphics();
Graphics2D g2 = (Graphics2D) tempgr;//generate an instance of Graphics2D and set the background color to be white
g2.setBackground(Color.white);
g2.clearRect(0,0,newWidth, newHeight);
if(scale < 10 )
{
readIn = new byte[tempWidth];
int lineCount = 0;
int index = 0;
while(fis.read(readIn) != -1)
{
while(index < readIn.length)
{
int newX = index/scale;
int newY = lineCount/scale;
if(index%scale == 0 && lineCount%scale == 0)
{
int tempInt = (int)(readIn[index]&0xff);
if(tempInt == 0)
{
tempgr.setColor(Color.black);
tempgr.drawLine(newX, newY, newX, newY);
}
if(tempInt == 180)
{
tempgr.setColor(Color.red);
tempgr.drawLine(newX, newY, newX, newY);
}
}
index++;
}
index = 0;
lineCount++;
}
}
fis.close();
Dimension dimension = new Dimension(newWidth, newHeight);
panel.setPreferredSize(dimension);
panel.setSize(newWidth, newHeight);
panel.setImage(tempImage);
}
public int decodeIntArray(LinkedList<Integer> inputArray)//compose an int value from an array with elements of one digit
{
int value = 0;
int i = 0;
while(i < inputArray.size())
{
value = (int) (inputArray.get(i)*Math.pow(10,inputArray.size()-i-1) + value);
i++;
}
return value;
}
public class MyCanvas extends JLabel
{
Image image = null;
int width;
int height;
public MyCanvas(Image im, int h, int w)
{
image = im;
width = w;
height = h;
}
public void setImage(Image im)
{
this.image = im;
repaint();
}
public void reSize(int w, int h)
{
this.setSize(w, h);
}
public void paint(Graphics gr)
{
super.paint(gr);
if(image != null)
{
gr.drawImage(image, 0, 0, this);
}
}
}
public static void main(String args[]) throws Exception{
Display test = new Display(args[0]);
}
}
wwza at 2007-7-11 16:02:33 >
