Repaint immediately JPanel (with PDFRenderer)
Hello everyone!
At the beggining - I'm novice in Java so my question is quite basic.
I have a problem with repainting my JPanel. I draw an image on JPanel and it doesn't appear. Picture is read from PDF file using PDFRenderer (during application startup) so before it shows it have to be extracted. It works because when I save picture to file it is compelete. Also after resizing window picture refreshes and shows in JPanel. Only when window shows up it is white background, but with correct size. I know the problem appears because
repaint is run in other thread so if I do something like this
Code :
public synchronized void OpenFromImage(Image image)
{
this.imageOrg = image;
this.imageShown = this.imageOrg;
this.refreshImage();
Thread.sleep(2000);
}
picture appears, but if only I change, for example, sleep time to 1500 it shows only half of the picture. When I reasize window the image refreshes and appears completly.
In google there is a lot of this problems posted, but I can't get working it in my case.
I was trying to run repaint in seperate thread (How to paintImmediately() in Java? - MacRumors Forums) or using paintImediately function in place of
Thread.sleep but it doesn't work.
What I'm doing wrong? Is it possible to force paint that picture is always showing fully?
Here is the part of my code. I someone could show me how should I put repaint in other threads or using invokeLater to acheive it:
Code :
public class PicViewer extends javax.swing.JPanel
{
/**
* Creates new form PicViewer
*/
private Image imageOrg;
private Image imageShown;
private final int maxSize = 32000;
private final int minSize = 1;
private final float zoomMax = 5;
private final float zoomMin = 0.1F;
public synchronized void OpenFromImage(Image image)
{
this.imageOrg = image;
this.imageShown = this.imageOrg;
this.refreshImage();
this.paintImmediately(new Rectangle(0, 0, this.getWidth(), this.getHeight()));
this.pictureChangedEvent();
}
protected void setSizeLimits()
{
if(this.getScaleImage() || (!this.getScaleImage() && this.imageShown == null))
{
this.setMinimumSize(new Dimension(this.minSize, this.minSize));
this.setMaximumSize(new Dimension(this.maxSize, this.maxSize));
}
else if(!this.getScaleImage() && this.imageShown != null)
{
int width = this.imageShown.getWidth(this);
int height = this.imageShown.getHeight(this);
this.setMinimumSize(new Dimension(width, height));
this.setMaximumSize(new Dimension(width, height));
}
}
/**
* Resizes and scales JPanel
*/
public void resize(int x, int y, int width, int height)
{
this.setSizeLimits();
this.setPreferredSize(new Dimension(width, height));
this.setBounds(x, y, width, height);
//It doeasn't work!!!
this.paintImmediately(x, y, width, height);
}
/**
* Scales image using zoom ratio
*/
public void refreshImage()
{
this.refreshImage(0, 0);
}
public void refreshImage(int posX, int posY)
{
if(this.imageOrg != null)
{
float zoomLevel = this.getZoom();
if(!this.getScaleImage())
{
this.imageShown = this.imageOrg.getScaledInstance((int)(this.imageOrg.getWidth(this)*zoomLevel), (int)(this.imageOrg.getHeight(this)*zoomLevel), 0);
}
else if(this.getScaleImage())
{
Component parent = this.getParent();
int parW = parent.getWidth();
int parH = parent.getHeight();
Dimension newDim = this.getScaledDimension(parW, parH);
if(newDim.width > 0 && newDim.height > 0)
this.imageShown = this.imageOrg.getScaledInstance(newDim.width, newDim.height, 0);
}
this.resize(posX, posY, this.imageShown.getWidth(this), this.imageShown.getHeight(this));
}
}
protected Dimension getScaledDimension(int reqWidth, int reqHeight)
{
int newWidth = 0;
int newHeight = 0;
float zoomLevel = this.getZoom();
float localZoomLevel = 0;
if(this.imageOrg != null)
{
int imgWidth = this.imageOrg.getWidth(this);
int imgHeight = this.imageOrg.getHeight(this);
if(reqWidth > reqHeight)
{
localZoomLevel = (float)reqWidth / (float)imgWidth;
newWidth = reqWidth;
newHeight = (int)(imgHeight * localZoomLevel);
}
else
{
localZoomLevel = (float)reqHeight/(float)imgHeight;
newHeight = reqHeight;
newWidth = (int)(imgWidth * localZoomLevel);
}
}
return new Dimension((int)(newWidth * zoomLevel), (int)(newHeight * zoomLevel));
}
public void refreshVisual()
{
this.revalidate();
this.repaint();
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Dimension dim = this.getPreferredSize();
g.drawImage(this.imageShown, 0, 0, dim.width, dim.height, null);
}
@Override
public void paint(Graphics g)
{
super.paint(g);
Dimension dim = this.getPreferredSize();
g.drawImage(this.imageShown, 0, 0, dim.width, dim.height, null);
}
@Override
public void update(Graphics g)
{
super.update(g);
this.paint(g);
}
}
Re: Repaint immediately JPanel (with PDFRenderer)
Can you make a small simple complete program (SSCCE) that compiles, executes and shows the problem?
Re: Repaint immediately JPanel (with PDFRenderer)
Please delete this thread