Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Repaint immediately JPanel (with PDFRenderer)

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    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:

    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);
        }
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Repaint immediately JPanel (with PDFRenderer)

    Can you make a small simple complete program (SSCCE) that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Repaint immediately JPanel (with PDFRenderer)

    Please delete this thread

Similar Threads

  1. [SOLVED] Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...
    By snowguy13 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 31st, 2011, 03:35 PM
  2. Help with repaint
    By AraHabs in forum AWT / Java Swing
    Replies: 5
    Last Post: November 5th, 2011, 04:40 PM
  3. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  4. Repaint doesn't repaint?
    By PotataChipz in forum AWT / Java Swing
    Replies: 6
    Last Post: January 18th, 2010, 09:56 PM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM