Zooming an image and scrolling it using a JScrollPane

Hi all, I know this is one of the most common problems in this forum but i cant get any of the replys to work in my code.
The problem:
I create an image with varying pixel colors depending on the value obtained from an AbstractTableModel and display it to the screen.
I then wish to be able to zoom in on the image and make it scrollable as required.
At the minute the scrolling method is working but only when i resize or un-focus and re-focus the JInternalFrame. Ive tried calling revalidate (and various other options) on the JScrollPane within the paintComponents(Graphics g) method but all to no avail.
Has anyone out there any ideas cause this is melting my head!
Heres the code im using (instance is called and added to a JDesktopPane):
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.AffineTransform;
import uk.ac.qub.mvda.gui.MVDATableModel;
import uk.ac.qub.mvda.utils.MVDAConstants;
public class HCLSpectrumPlot extends JInternalFrame implements MVDAConstants
  AbstractAction zoomInAction = new ZoomInAction();
  AbstractAction zoomOutAction = new ZoomOutAction();
  double zoomFactorX = 1.0;
  double zoomFactorY = 1.0;
  private AffineTransform theTransform;
  private ImagePanel imageViewerPanel;
  private JScrollPane imageViewerScroller;
  public HCLSpectrumPlot(String title, MVDATableModel model)
    super(title, true, true, true, true);
    int imageHeight_numOfRows = model.getRowCount();
    int imageWidth_numOfCols = model.getColumnCount();
    int numberOfColourBands = 3;
    double maxValueInTable = 0;
    double[][] ValueAtTablePosition =
        new double[imageHeight_numOfRows][imageWidth_numOfCols];
    for(int i=0; i<imageHeight_numOfRows; i++)
      for(int j=0; j<imageWidth_numOfCols; j++)
     ValueAtTablePosition[i][j] = ((Double)model.getValueAt
             (i,j)).doubleValue();
    for(int i=0; i<imageHeight_numOfRows; i++)
      for(int j=0; j<imageWidth_numOfCols; j++)
     if ( ValueAtTablePosition[i][j] > maxValueInTable)
       maxValueInTable = ValueAtTablePosition[i][j];
    BufferedImage newImage = new BufferedImage(imageWidth_numOfCols,
          imageHeight_numOfRows, BufferedImage.TYPE_3BYTE_BGR);
    WritableRaster newWritableImage = newImage.getRaster();
    int colourB;
    double pixelValue, cellValue, newPixelValue;
    for (int x = 0; x < imageHeight_numOfRows; x++)
      for (int y = 0; y < imageWidth_numOfCols; y++)
     colourB = 0;
     cellValue = ValueAtTablePosition[x][y];
     pixelValue = (1 - (cellValue / maxValueInTable)) * 767;
     pixelValue = pixelValue - 256;
     while (colourB < numberOfColourBands)
       if ( pixelValue < 0 )
         newPixelValue = 256 + pixelValue;
         newWritableImage.setSample(x, y, colourB, newPixelValue);
         colourB++;
            while ( colourB < numberOfColourBands )
           newWritableImage.setSample(x, y, colourB, 0);
           colourB++;
     else
       newWritableImage.setSample(x, y, colourB, 255);
     colourB++;
     pixelValue = pixelValue - 256;
      }//while
     }//for-y
    }//for-x
    imageViewerPanel = new ImagePanel(this, newImage);
    imageViewerScroller =     new JScrollPane(imageViewerPanel,
                        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
               JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(imageViewerScroller, BorderLayout.CENTER);
    JToolBar editTools = new JToolBar();
    editTools.setOrientation(JToolBar.VERTICAL);
    editTools.add(zoomInAction);
    editTools.add(zoomOutAction);
    this.getContentPane().add(editTools, BorderLayout.WEST);
    this.setVisible(true);
  class ImagePanel extends JPanel
    private int iWidth, iHeight;
    private int i=0;
    private BufferedImage bufferedImageToDisplay;
    private JInternalFrame parentFrame;
    public ImagePanel(JInternalFrame parent, BufferedImage image)
      super();
      parentFrame = parent;
      bufferedImageToDisplay = image;
      iWidth = bufferedImageToDisplay.getWidth();
      iHeight = bufferedImageToDisplay.getHeight();
      theTransform = new AffineTransform();
      //theTransform.setToScale(parent.getContentPane().getWidth(),
                                parent.getContentPane().getHeight());
      this.setPreferredSize(new Dimension(iWidth, iHeight));
    }//Constructor
    public void paintComponent(Graphics g)
      super.paintComponent(g);
      ((Graphics2D)g).drawRenderedImage(bufferedImageToDisplay,
                                         theTransform);
      this.setPreferredSize(new Dimension((int)(iWidth*zoomFactorX),
                                      (int)(iHeight*zoomFactorY)));
    }//paintComponent
  }// end class ImagePanel
   * Class to handle a zoom in event
   * @author Ross McCaughrain
   * @version 1.0
  class ZoomInAction extends AbstractAction
   * Default Constructor.
  public ZoomInAction()
    super(null, new ImageIcon(HCLSpectrumPlot.class.getResource("../"+
              MVDAConstants.PATH_TO_IMAGES + "ZoomIn24.gif")));
    this.putValue(Action.SHORT_DESCRIPTION,"Zooms In on the Image");
    this.setEnabled(true);
  public void actionPerformed(ActionEvent e)
    zoomFactorX += 0.5;
    zoomFactorY += 0.5;
    theTransform = AffineTransform.getScaleInstance(zoomFactorX,
                                                zoomFactorY);
    repaint();
  // ZoomOut to be implemented
}// end class HCLSpectrumPlotAll/any help greatly appreciated! thanks for your time.
RossMcC

Small mistake, the revalidate must be called on the panel not on the jsp.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class UsaZ extends JFrame 
     IPanel      panel = new IPanel();
     JScrollPane jsp   = new JScrollPane(panel);
public UsaZ() 
     addWindowListener(new WindowAdapter()
     addWindowListener(new WindowAdapter()
     {     public void windowClosing(WindowEvent ev)
          {     dispose();
               System.exit(0);
     setBackground(Color.lightGray);
     getContentPane().add("Center",jsp);
     setBounds(1,1,400,320);
     setVisible(true);
public class IPanel extends JComponent
     Image  map;
     double zoom = 1;
     double iw;
     double ih;
public IPanel()
     map = getToolkit().createImage("us.gif");
     MediaTracker tracker = new MediaTracker(this);
     tracker.addImage(map,0);
     try {tracker.waitForID(0);}
     catch (InterruptedException e){}
     iw = map.getWidth(this);
     ih = map.getHeight(this);
     zoom(0);     
     addMouseListener(new MouseAdapter()     
     {     public void mouseReleased(MouseEvent m)
               zoom(0.04);               
               repaint();      
               revalidate();
public void zoom(double z)
     zoom = zoom + z;
     setPreferredSize(new Dimension((int)(iw*zoom)+2,(int)(ih*zoom)+2));
public void paintComponent(Graphics g)
     super.paintComponent(g);
     Graphics2D g2 = (Graphics2D)g;
     g2.scale(zoom,zoom);
     g2.drawImage(map,1,1,null);
     g.drawRect(0,0,map.getWidth(this)+1,map.getHeight(this)+1);
public static void main (String[] args) 
      new UsaZ();
[/cdoe]
Noah                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

Similar Messages

  • I scanned an image and want to use the image trace tool, but it doesn't pick up all the lines.  Is there a way to darken the lines before using the image trace tool?

    I scanned an image and want to use the image trace tool, but it doesn't pick up all the lines.  Is there a way to darken the lines before using the image trace tool?  Help!

    If the scan is in B&W, then play with the Threshold setting here
    If it's in Color, then you will have to open the scan in a Raster editing software (like Photoshop) and play with the Brightness/Contrast settings to make the lines bolder.

  • Downloading images and audio file using java

    Dear All
    I have a directory on my webserver which contains images,audio files and many other data files.
    I want to download all the file but the problem is when i download image and save it, it is not viewable. I dont want to use Image class as i may have to downlaod audio files too. Please guis which stream shouldi open to download any kind of content using same code.
    Thanks in advance. And if u have some example please let me know
    Regards
    Jafery

    downloading images and audio file using java -------------------------------
    how to write it back to dataoutputstremI don't understand what you are talking about.
    Server: DataOutputStream -> Client: DataInputStream -- the two is a pair.

  • How to capture an image and save it using action script

    Hello,
    I need to know if is posible to capture an image or a screen region and save it using action scrip.
    Somebody know how to do it ??
    Thanks

    you can capture an image using the bitmapdata class and getPixel().  you can then save that to a bitmap using server-side code like php.

  • Can't select text and scroll when using Word

    Recently I have been unable to select text and scroll down the page using two fingers, while in a word document.This is only happening in Word, so far no other programs. Does anyone have an idea of how to turn on or off this feature?

    I cant scroll below or above viewable section of text in any of my Macbooks, iMac, Mac Mini. Never have been able to, never could, except by pressing the arrow keys whilst selecting, to move up or down.
    Its stupid
    I dont have flash installed (never will do since my first virus of video ad sound running in background from Cnet was downloaded and kept returning until i deinstalled flash).
    I believe that this is a universal issue because none of my Macs can scroll out of screen when selecting text, which as a webmaster and developer, is a pain in the butt
    My issue is not in word documents, its in everything

  • Images and scroll bar in Itunes bright green

    Does anyone know how to fix this issue and have the display look normal. A screenshot is below.
    Some details: the monitor is set to display 32bit color and the screen reslution isn't the problem.
    -Thanks

    I recently got iTunes 11, and the scroll bar issue is not in Preferences > General in this version.
    I really hate this new itunes. The main issues I'd like to see are the scroll bar to be permanently visible. The second issue is when I have large playlists and I switch between them, the old itunes would always know which place in the playlist was highlighted. This new version always brings me back to the top of the playlist. And this is true also with the main library. When I am going through 4,000+ songs, it makes no sense for there to be no convenient way to save the place in the library I am working on while still allowing me to navigate other parts of the sidebar.
    Even though this seems fairly elementary to me, I haven't really had much luck finding other people who are having these issues, and your answer was the closest one I'd found.

  • How to include zoom option for images and video in adobe captivate5?

    I am developing an elearning software with captivate 5 which has images and video.
    I want to include an option to be able to zoom the images and video if clicked on the particular area.
    It should zoom any part of the image when clicked on it during run time and go back to original size when clicked again.
    How can I do this?

    1. I try with two images: The thumbnail y the Big Image Zoom image Detail.
    2. I Use Dinamic Images
    3. check this tip

  • Send HTML Email with Embedded Images and CSS

    Hi All,
    I have a html page. I want to send that html page(not with attachment) with all images and css. i search and try but I cant find a good solution. can any one help... plz..........
    Thank You.....

    If you want to send the html page and have it
    reference the images and css files on your web
    site, that's pretty easy. Just create a message
    with text/html content that is your html page.
    If you want to include all the images and css files
    in your message along with the html page, you'll
    need to create a multipart/related message and
    you'll need to change all the html to reference the
    images and css files using "cid:" references.

  • I right clicked on a image and now all my thumbnails are missing, how do I get them back?

    I was in Facebook trying to delete/block an image from my likes (I wanted to delete the like but couldn't so I thought I would just block the image) Anyway what I clicked on in the right click box took all my thumbnails off of Facebook. I would like to get them back, how do I do this?
    thank you

    See http://kb.mozillazine.org/Images_or_animations_do_not_load
    Check the image exceptions: Tools > Options > Content: Load Images: Exceptions<br />
    You can see the permissions for the server in the current tab in Tools > Page Info > Permissions<br />
    *A way to see which images are blocked is to click the favicon (<i>Site Identification</i> icon) on the left side of the location bar.
    *A click on the "More Information" button will open the Security tab of the "Page Info" window (also accessible via "Tools > Page Info").
    *Open the <i>Media</i> tab of the "Page Info" window.
    *Select the first image and scroll down though the list with the Down arrow key.
    *If an image in the list is grayed and there is a check-mark in the box "<i>Block Images from...</i>" then remove that mark to unblock the images from that domain.

  • Only want to transfer NEW images and videos to N70...

    Im using PC Suite 6.80.22 and when i try to transfer all of my images and videos over using "image store" it will only transfer the whole lot over.
    How can I set it so that it only transfers the new images and videos? Otherwise, I end up with duplicates, triplicates etc.
    Thanks,
    Billy

    update pc suite to 6.81.13, it fixes the problem

  • Tiff image and pdf file

    Hi,
    I am working with project that requires showing different types of images and pdf files using oracle ADF components. It was so easy to show jpeg and gif, for example, using objectMedia. I tried to use the same component, objectMedia, to show tiff image, but it didn't work and I was unable to find suitable component to show pdf file.
    Is there anyone some can help me in showing tiff image and pdf file.
    Alice

    Correction, PDF is not required to be geneated, but displayed.
    Display PDF and tiff with af:objectImage ?
    http://www.oracle.com/technology/products/jdev/htdocs/partners/addins/exchange/jsf/doc/tagdoc/core/objectImage.html

  • Unable to view images and videos on file explorer ...

    hi. Im using a nokia E6. Im unable to view my images and videos taken using the phone camera if i go through the file explorer. But im a ble to view them through the gallery. Can anyone help me out here?. I've tried resetting the system. No change.

    hi, have you tried reinstalling the current firmware thru Nokia Suite on PC?

  • How do I restore images and backgrounds. Hide images and backgrounds will not unclick.

    Back grounds in email cannot be restored
    == This happened ==
    Every time Firefox opened
    == I clicked on "hide images and backgrounds" and it will not unclick.

    Check the image exceptions: Tools > Options > Content: Load Images: Exceptions - See [[Options window - Content panel]]
    A way to see which images are blocked is to click the favicon (''Site Identification'' icon) on the left side of the location bar and click the "More Information" button.
    This will open the Security tab of the ''Page Info'' window (also accessible via Tools > Page Info).
    Go to the ''Media'' tab of that Page Info window.
    Select the first image and scroll down though the list with the Down arrow.
    If an image is grayed and there is a check-mark in the box ''Block Images from...'' then remove that mark to unblock the images from that domain.
    See also [[Images or animations do not show]] and http://kb.mozillazine.org/Images_or_animations_do_not_load

  • The images and words are magnified and I am unable to use the phone.  I cannot zoom out either.  Please advise.  Thanks.

    On my iphone 4S, the images are all magnified and I am unable to zoom the images out and use the phone.  Any suggestion to prevent this from happening and correcting this?

    Double tap on the screen using three fingers to get back to normal.
    Turn on/off accessibility features using iPhone: Go to Settings > General > Accessibility.
    Zoom in or out
    Double-tap the screen with three fingers. By default, the screen is magnified 200 percent. If you manually change the magnification (by using the tap-and-drag gesture, described below), iPhone automatically returns to that magnification when you zoom in by double-tapping with three fingers.
    Message was edited by: Ingo2711

  • Activing Mighty Mouse "Zoom using" and "Scrolling options" prefs

    According to:
    http://www.apple.com/mightymouse/software.html
    there should be "Zoom using scroll ball... " and "Scrolling options" popups in Mouse preferences, but I only see:
    I'm running 10.4.7 and ran the Mouse software installer. How do I get these additional options to appear?
    PowerBook G4/1.67 GHz   Mac OS X (10.4.7)   7200rpm 100 GB HD, 2 GB RAM

    The 10.4.8 update adds the scroll-wheel zooming
    feature for all mice.
    Really? It hadn't done so on my Mac. I ran the usual privileges, etc., routines beforehand. I can't even recall it being mentioned in the release notes or their detailed specs. http://docs.info.apple.com/article.html?artnum=304200
    Anway, 10.4.8 didn't affect a change to the keyboard/mouse pref on my PPC so I used Pacifist to get at the additional MM features.

Maybe you are looking for