Hello everyone!
I was trying get answer on other forum but without any reaction so I decided to try here .
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
setScrollPosition(int hPercentage, int vPercentage)
to scroll to the center of zoomed area after zoom in/out request.
Full zoom algorithm is in function
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:
javaTest_2013_01_02.zip - Speedy Share - upload your files here
My today's fight conclusions:
I turned off function this.picViewer1.refreshImage() [which is resizing picture and JPanel] and removed this.revalidate() and repaint() everywhere [of course I will add it back later].
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 it is flickering on screen...
Please help.
I'm using this solution to scroll picture using KeyListener and it works. Only during resizing it doeasn't work...
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 }