Results 1 to 5 of 5
  1. #1
    hanifa is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Post ScrollPane Issues..

    Hi,
    When i scale the image, the scrollbars dont pop out to cover the whole image. The only time, the scrollbars do work is when I drag the JFrame and even then , it still doesnt show the whole image. It seems like a simple question But I am still unable to solve it. :confused:
    I know the code seems a bit long but I just cut out all the remaining code from an existing application to just narrow down to scaling only.

    Thanks in advance.
    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class ContainerPanel extends JPanel{
    	private  ImagePanel imagePanel;
    	private JSlider slider;
    
    	public ContainerPanel(BufferedImage buffImage){
    		setLayout(new BorderLayout());
    		imagePanel = new ImagePanel(buffImage);
    		JScrollPane scrollPane = new JScrollPane(imagePanel);
    		add(scrollPane,BorderLayout.CENTER);
    //		TODO: scrollPane doesnt work properly	
    		createSlider();
    		add(slider,BorderLayout.PAGE_END);
    	}
    	private JSlider createSlider() {
    		slider = new JSlider(25, 200, 100);
    		slider.addChangeListener(new ChangeListener() {
    			public void stateChanged(ChangeEvent e) {
    				int value = ((JSlider)e.getSource()).getValue();
    				imagePanel.scale = value/100.0;
    				imagePanel.scaleImage();
    			}
    		});
    	  return slider;
    	}
    	public static void main(String[] args) {
        	JFrame f = new JFrame();
    	    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            BufferedImage buffImage=null;
    	    String fileName = "your.jpg";
            try{
            buffImage = ImageIO.read(new File(fileName));
            }catch(IOException e){}        
            ContainerPanel one =new ContainerPanel(buffImage);
    	    f.add(one);
            f.setSize(500,500);
            f.setLocation(200,200);
            f.setVisible(true);
            f.pack();
    
        }
    	
    	class ImagePanel extends JPanel {
    		private BufferedImage buffImage;
    		public BufferedImage scaledImage;
    		public double scale = 1.0;
    
    		public ImagePanel(BufferedImage buffImage){
    			this.buffImage=buffImage;
    			//scaledImage=buffImage;
                            scaleImage();
    			this.setPreferredSize( new Dimension(buffImage.getHeight(),buffImage.getWidth() ) );
    
    		}
    		public void scaleImage() {
    	        int w = (int)(scale*buffImage.getWidth());
    	        int h = (int)(scale*buffImage.getHeight());
    	        int type = BufferedImage.TYPE_INT_RGB;
    	        scaledImage = new BufferedImage(w, h, type);
    	        Graphics2D g2 = scaledImage.createGraphics();
    	        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
    	                            RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    	        g2.drawImage(buffImage, 0, 0, w, h, this);
    	        g2.dispose();
    	        revalidate();
    	        repaint();
    	    }
    		protected void paintComponent(Graphics g){
    			super.paintComponent(g);
    	        g.drawImage(this.scaledImage,0,0,this);
    		}
    	}
    }
    Last edited by hanifa; 09-11-2008 at 05:15 AM. Reason: ImagePanel() constructor was changed.

  2. #2
    hanifa is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default

    Another issue , I would like to raise is that scaling does seem to result in lagging. Is there something , I could change to make it more robust.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class ContainerPanelRx extends JPanel{
        private ImagePanel imagePanel;
    
        public ContainerPanelRx(BufferedImage buffImage){
            setLayout(new BorderLayout());
            imagePanel = new ImagePanel(buffImage);
            JScrollPane scrollPane = new JScrollPane(imagePanel);
            add(scrollPane,BorderLayout.CENTER);
            //TODO: scrollPane doesnt work properly
            // It has no idea what size ImagePanel is.
            // When it queries ImagePanel for its size
            // requirements it will get the preferredSize
            // or, if none has been set or offered, the
            // default size (10,10). It makes use of the
            // preferredSize to position its scrollBars.
            add(createSlider(), BorderLayout.PAGE_END);
        }
        private JSlider createSlider() {
            JSlider slider = new JSlider(25, 200, 100);
            slider.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                    int value = ((JSlider)e.getSource()).getValue();
                    imagePanel.scale = value/100.0;
                    imagePanel.scaleImage();
                }
            });
            return slider;
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            BufferedImage buffImage=null;
            String fileName = //"your.jpg";
                              "images/blackBear.jpg";
            try{
                buffImage = ImageIO.read(new File(fileName));
            }catch(IOException e){
                System.out.println("read error: " + e.getMessage());
            }
            ContainerPanelRx one = new ContainerPanelRx(buffImage);
            f.add(one);
    //        f.setSize(500,500);
            f.pack();
            f.setLocation(200,200);
            f.setVisible(true);
        }
    
        class ImagePanel extends JPanel {
            private BufferedImage buffImage;
            public BufferedImage scaledImage;
            public double scale = 1.0;
    
            public ImagePanel(BufferedImage buffImage){
                this.buffImage=buffImage;
                //scaledImage=buffImage;
                scaleImage();
                // Using this method can get tedious in a
                // dynamic component. Try overriding the
                // [i]getPreferredSize[/i] method instead.
                // These Dimension arguments are reversed:
    //            this.setPreferredSize( new Dimension(buffImage.getHeight(),
    //                                                 buffImage.getWidth() ) );
            }
    
            public void scaleImage() {
                int w = (int)(scale*buffImage.getWidth());
                int h = (int)(scale*buffImage.getHeight());
                int type = BufferedImage.TYPE_INT_RGB;
                scaledImage = new BufferedImage(w, h, type);
                Graphics2D g2 = scaledImage.createGraphics();
                g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                    RenderingHints.VALUE_INTERPOLATION_BICUBIC);
                g2.drawImage(buffImage, 0, 0, w, h, this);
                g2.dispose();
                revalidate();
                repaint();
            }
    
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(this.scaledImage,0,0,this);
            }
    
            /**
             * Here's how you inform the parent JScrollPane
             * of the size requirements of this dynamic
             * component. The updated information is ready
             * and waiting when the scrollPane calls for it.
             */
            public Dimension getPreferredSize() {
                int w = (int)(scale*buffImage.getWidth());
                int h = (int)(scale*buffImage.getHeight());
                return new Dimension(w, h);
            }
        }
    }

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    scaling does seem to result in lagging. Is there something...to make it more robust.
    Possibly. Some image types don't perform well on some computers. Try using a compatibleImage and see if it helps.
    Java Code:
        class ImagePanel extends JPanel {
            ...
            public void scaleImage() {
                ...
                scaledImage = getCompatibleImage(w, h);
                System.out.println("scaledImage type = " +
                                    scaledImage.getType());
                Graphics2D g2 = scaledImage.createGraphics();
                ...
            }
    
            private BufferedImage getCompatibleImage(int w, int h) {
                GraphicsEnvironment ge =
                    GraphicsEnvironment.getLocalGraphicsEnvironment();
                GraphicsDevice gd = ge.getDefaultScreenDevice();
                GraphicsConfiguration gc = gd.getDefaultConfiguration();
                return gc.createCompatibleImage(w, h);
            }

  5. #5
    hanifa is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default

    Your first solution was great.. Solved the issue..
    For the second one, the result wasnt much significant (the lag was somewhat lesser though). So cud consider it, a significant improvement.

    and Thanks Once Again... I would have never guessed it to be the answer ever . :D

    and just fyi, my prev. scaling code was also ripped from one of your posts.

    Cheers and keep up the good work.

Similar Threads

  1. JList issues
    By aneesahamedaa in forum AWT / Swing
    Replies: 7
    Last Post: 12-15-2009, 04:16 PM
  2. iterator issues
    By orchid in forum New To Java
    Replies: 2
    Last Post: 08-12-2008, 01:43 PM
  3. scrollpane
    By Newb in forum AWT / Swing
    Replies: 0
    Last Post: 06-09-2008, 12:14 AM
  4. Issues with Jva I.O
    By Annatar01 in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 01:16 AM
  5. Tree with Listener not working when added to ScrollPane
    By praveen.kb in forum AWT / Swing
    Replies: 2
    Last Post: 01-09-2008, 07:06 AM

Posting Permissions

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