Results 1 to 3 of 3
  1. #1
    brajan1984 is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default JScrollPane and programatic scrolling

    Hello everyone!
    I'm new in JAVA and I'm migrating from C#. I make a simple picture viewer with possibility of zooming using JPanel and JScrollPane. I made a zoom component (buttons + slider) and with events, program is zooming in and out picture which is loaded in JPanel (in code called picViewer1). Everything works, but everytime when I make zoom operation it scrolls to position 0,0. I tried few options and it will be visible in presented code.

    Below I present You a my class which is Swing component iherited from JScrollPane. My idea is to run function
    Java Code:
    setScrollPosition(int hPercentage, int vPercentage)
    to scroll to the center of zoomed area after zoom in/out request.
    Full zoom algorithm is in function
    Java Code:
    public void setZoom(float zoom)
    Can anybody show me how can I achieve scrolling in program? I'm out of ideas...

    I'm adding my NetBeansProject with source. Please put file pic.jpg in C:/a/pic.jpg:
    http://speedy.sh/fukwt/javaTest-2013-01-02.zip


    Java Code:
    public class ScrolledPicViewer extends javax.swing.JScrollPane
    {
        /**
         * Creates new form ScrolledPicViewer
         */
        public ScrolledPicViewer()
        {
            super();
            
            initComponents();
            
            this.horizontalScrollBarPolicy = HORIZONTAL_SCROLLBAR_ALWAYS;
            this.verticalScrollBarPolicy = VERTICAL_SCROLLBAR_ALWAYS;
            
            this.getViewport().add(this.picViewer1, BorderLayout.CENTER);
        }
        
        public final void setScaleImage(boolean scaleImage)
        {
            this.picViewer1.setScaleImage(scaleImage);
        }
        
        public final boolean getScaleImage()
        {
            return this.picViewer1.getScaleImage();
        }
        
        /*
         * Gets horizontal scroll position in percentage
         */
        public final int getHorizontalScrollPosition()
        {
            int picWidth = this.picViewer1.getWidth();
            int scrollPos = this.getHorizontalScrollBar().getValue();
            
            int percentage = 0;
            
            if(picWidth > 0)
            {
                percentage = (int)(((float)scrollPos / (float)picWidth)*100);
            }
            
            return percentage;
        }
        
        /*
         * Gets vertical scroll position in percentage
         */
        public final int getVerticalScrollPosition()
        {
            int picHeight = this.picViewer1.getHeight();
            int scrollPos = this.getVerticalScrollBar().getValue();
            
            int percentage = 0;
            
            if(picHeight > 0)
            {
                percentage = (int)(((float)scrollPos / (float)picHeight)*100);
            }
            
            return percentage;
        }
        
        public final void setScrollPosition(int hPercentage, int vPercentage)
        {
            float convFactorH = 0;
            float convFactorV = 0;
            
            int scrollPosH = 0;
            int scrollPosV = 0;
            
            JScrollBar vbar = this.getVerticalScrollBar();
            int maxPositionV = vbar.getMaximum () - vbar.getVisibleAmount();
            
            JScrollBar hbar = this.getHorizontalScrollBar();
            int maxPositionH = hbar.getMaximum () - hbar.getVisibleAmount();
            
            if(hPercentage >= 0 && hPercentage <= 100 && vPercentage >= 0 && vPercentage <= 100)
            {
                convFactorH = (float)hPercentage/100;
                convFactorV = (float)vPercentage/100;
                
                scrollPosH = (int)(convFactorH * maxPositionH);
                scrollPosV = (int)(convFactorV * maxPositionV);
            }
            
            //hbar.setValue(scrollPosH);
            //vbar.setValue(scrollPosV);
            
            this.getViewport().setViewPosition(new Point(-200, -200));
            
            this.repaint();
            this.revalidate();
        }
        
        
        public void setZoom(float zoom)
        {
            int horizontalScrollPosition = this.getHorizontalScrollPosition();
            int verticalScrollPosition = this.getVerticalScrollPosition();
            
            this.picViewer1.setZoom(zoom);
            this.picViewer1.refreshImage();
            
            this.setScrollPosition(horizontalScrollPosition, verticalScrollPosition);
        }
        
        public float getZoom()
        {
            return this.picViewer1.getZoom();
        }
        
        public float getZoomMax()
        {
            return this.picViewer1.getZoomMax();
        }
        
        public float getZoomMin()
        {
            return this.picViewer1.getZoomMin();
        }
        
        public void zoomIn()
        {
            this.picViewer1.zoomIn(0.1F);
            this.picViewer1.refreshImage();
        }
        
        public void zoomOut()
        {
            this.picViewer1.zoomOut(0.1F);
            this.picViewer1.refreshImage();
        }
        
        public void resetZoom()
        {
            this.picViewer1.setZoom(1);
            this.picViewer1.refreshImage();
        }
        
        public void resize()
        {
            this.picViewer1.refreshImage();
        }
        
        public void OpenFromFile(String fileName) throws IOException
        {
            this.picViewer1.OpenFromFile(fileName);
            
            this.refreshImage();
        }
        
        public void refreshImage()
        {
            this.picViewer1.refreshImage();
            
            this.revalidate();  
            this.validate();
        }
        
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            picViewer1 = new javatest.PicViewer();
    
            addComponentListener(new java.awt.event.ComponentAdapter() {
                public void componentResized(java.awt.event.ComponentEvent evt) {
                    formComponentResized(evt);
                }
            });
    
            javax.swing.GroupLayout picViewer1Layout = new javax.swing.GroupLayout(picViewer1);
            picViewer1.setLayout(picViewer1Layout);
            picViewer1Layout.setHorizontalGroup(
                picViewer1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 100, Short.MAX_VALUE)
            );
            picViewer1Layout.setVerticalGroup(
                picViewer1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 100, Short.MAX_VALUE)
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getViewport());
            getViewport().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(picViewer1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 494, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(picViewer1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 408, Short.MAX_VALUE))
            );
        }// </editor-fold>                        
    
        private void formComponentResized(java.awt.event.ComponentEvent evt) {                                      
            // TODO add your handling code here:
            
            this.picViewer1.refreshImage();
            
            //this.validate();
            //this.repaint();
        }                                     
    
        // Variables declaration - do not modify                     
        private javatest.PicViewer picViewer1;
        // End of variables declaration                   
    }
    Last edited by brajan1984; 01-02-2013 at 02:12 PM. Reason: Add link with source code

  2. #2
    brajan1984 is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Re: JScrollPane and programatic scrolling

    I still play with this. I turned off function this.picViewer1.refreshImage() and removed this.revalidate() and repaint() everywhere.

    Using code
    Java Code:
    hbar.setValue(scrollPosH);
    vbar.setValue(scrollPosV);
    image scrolls to desired location. So my question is what is happening in function refreshImage that scroll is always reseting to zero position?

    PS. It seems that after clicking zoom in image is scrolling but right after is reset to zero position. I know because is flickering on screen...

    Please help.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

Similar Threads

  1. JScrollPane is Cutting off my Image upon Scrolling
    By Ryan.Vincent in forum AWT / Swing
    Replies: 6
    Last Post: 04-19-2012, 05:16 AM
  2. Automatic scrolling of JScrollPane
    By chayan in forum AWT / Swing
    Replies: 2
    Last Post: 07-03-2010, 05:51 AM
  3. MouseMotionListener 'scrolling'
    By Thez in forum Java 2D
    Replies: 3
    Last Post: 03-12-2009, 11:48 AM
  4. Getting JScrollPane to stop auto-scrolling
    By Shadow in forum AWT / Swing
    Replies: 8
    Last Post: 10-21-2008, 08:52 PM
  5. JScrollPane not scrolling
    By Riftwalker in forum Advanced Java
    Replies: 2
    Last Post: 07-17-2007, 08:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •