Results 1 to 4 of 4
  1. #1
    Sharad99jash is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Post Not able to load a Planarimage in a JPanel

    Hello guys I'm trying to load a image in a Jpanel using a button but the image is not showing.
    Anyone plz rectify the given code-(The code is written using NetBeans)

    /**
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package edu.iirs.testCases;

    import com.sun.media.jai.widget.DisplayJAI;
    import edu.iirs.displayui.DisplayTwoSynchronizedImages;
    import edu.iirs.gisintergrator.ImagePanel;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.geom.AffineTransform;
    import java.io.FileNotFoundException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.media.jai.JAI;
    import javax.media.jai.PlanarImage;
    import javax.swing.JComponent;
    import javax.swing.JFileChooser;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollBar;
    import javax.swing.JScrollPane;

    /**
    *
    * @author Sharad
    */
    public class LoadMyImage extends javax.swing.JFrame {
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton4;
    private javax.swing.JTextField jTextField1;

    private boolean fimStatus=false;
    private String imageName;
    private JPanel jPanel1;
    protected ImagePanel viewer;
    /**
    * Creates new form LoadMyImage
    */
    public LoadMyImage() {
    initComponents();
    }

    /**
    * 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() {

    jButton1 = new javax.swing.JButton();
    jTextField1 = new javax.swing.JTextField();
    jButton4 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jButton1.setText("Load");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    jTextField1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jTextField1ActionPerformed(evt);
    }
    });

    jButton4.setText("Get Image");
    jButton4.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton4ActionPerformed(evt);
    }
    });
    jButton4.addFocusListener(new java.awt.event.FocusAdapter() {
    public void focusGained(java.awt.event.FocusEvent evt) {
    jButton4FocusGained(evt);
    }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(32, 32, 32)
    .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(29, 29, 29)
    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(18, 18, 18)
    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(57, 57, 57))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(18, 18, 18)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addComponent(jButton4)
    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jButton1))
    .addGap(456, 456, 456))
    );

    pack();
    }// </editor-fold>

    private void jTextField1ActionPerformed(java.awt.event.ActionEv ent evt) {
    // TODO add your handling code here:
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    imageName = jTextField1.getText();

    if(fimStatus) {
    JOptionPane.showConfirmDialog(rootPane, "An Image Already Loaded!", "ERROR", JOptionPane.WARNING_MESSAGE);
    this.remove(jPanel1);
    jPanel1.repaint();
    this.repaint();

    }
    else{
    PlanarImage image = JAI.create("fileload",imageName);
    viewer = new ImagePanel(image);
    getContentPane().add(viewer,BorderLayout.CENTER);
    viewer.setVisible(true);

    }
    }

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
    String[] pics = new String[] {"tif",".jpg",".gif"};
    System.out.println("inside image browse");
    JFileChooser jfc = new JFileChooser();
    jfc.addChoosableFileFilter(new edu.iirs.controllers.SimpleFileFilter(pics,"*.tif, *.jpg,*.gif"));

    int result=jfc.showDialog(jfc, "Choose an image file");

    if(result==JFileChooser.CANCEL_OPTION)
    {
    imageName=null;
    JOptionPane.showMessageDialog(rootPane,"No File Specified" , "ERROR", JOptionPane.ERROR_MESSAGE);

    }
    else
    {
    imageName = jfc.getSelectedFile().getAbsolutePath();
    jTextField1.setText(imageName);
    }
    }

    private void jButton4FocusGained(java.awt.event.FocusEvent evt) {
    // JLabel jl1 = new JLabel("Made By Mr. Unmesh Khati, Smd. Musthafa,Mr. A. Ponraj,Mr. Anil Singh,Mr. Dhanesh Lal");
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
    * For details see How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)
    */
    try {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
    javax.swing.UIManager.setLookAndFeel(info.getClass Name());
    break;
    }
    }
    } catch (ClassNotFoundException ex) {
    java.util.logging.Logger.getLogger(LoadMyImage.cla ss.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
    java.util.logging.Logger.getLogger(LoadMyImage.cla ss.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(LoadMyImage.cla ss.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(LoadMyImage.cla ss.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
    new LoadMyImage().setVisible(true);
    }
    });
    }


    public PlanarImage readAsPlanarImage(String fileName){
    return JAI.create("fileload",fileName);
    }


    }


    /*Imagepanel Class*/

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.AdjustmentEvent;
    import java.awt.event.AdjustmentListener;
    import java.awt.image.RenderedImage;
    import javax.media.jai.PlanarImage;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;

    /**
    *
    * @author Sharad
    */
    public class ImagePanel extends JPanel implements AdjustmentListener{
    // two planar images for input and output
    private PlanarImage inpmg,opimg;

    // boolean flag value for display
    public boolean fstatus=false;

    /** The JScrollPane which will contain the first of the images */
    protected JScrollPane jsp1;

    /** The JScrollPane which will contain the second of the images */
    //protected JScrollPane jsp2;

    /** The DisplayJAIWithPixelInfo for the first image. */
    protected DisplayJAIWithPixelInfo dj1;


    public ImagePanel(RenderedImage rimg) {
    super();
    inpmg = (PlanarImage) rimg;

    System.out.println("Inside ImagePnael cons");
    setLayout(new BorderLayout());
    setLocation(200, 450);
    dj1 = new DisplayJAIWithPixelInfo(inpmg); // Instances of DisplayJAI for the
    jsp1 = new JScrollPane(dj1); // JScrollPanes for the both
    add(jsp1);
    // Retrieve the scroll bars of the images and registers adjustment
    // listeners to them.
    // Horizontal scroll bar of the first image.
    jsp1.getHorizontalScrollBar().addAdjustmentListene r(this);
    // Vertical scroll bar of the first image.
    jsp1.getVerticalScrollBar().addAdjustmentListener( this);
    setVisible(true);
    setBackground(Color.red);
    System.out.println("\nLeaving ImagePanel cons");
    invalidate();
    repaint();
    }
    public void setImage1(RenderedImage newimage)
    {
    dj1.set(newimage);
    repaint();

    }

    /**
    * This method returns the first image.
    * @return the first image.
    */
    public RenderedImage getImage1()
    {
    return dj1.getSource();
    }

    @Override
    public void adjustmentValueChanged(AdjustmentEvent e) {
    if (e.getSource() == jsp1.getHorizontalScrollBar())
    {
    // We change the position of the horizontal bar of the second image.
    jsp1.getHorizontalScrollBar().setValue(e.getValue( ));
    }
    // If the vertical bar of the first image was changed...
    if (e.getSource() == jsp1.getVerticalScrollBar())
    {
    // We change the position of the vertical bar of the second image.
    jsp1.getVerticalScrollBar().setValue(e.getValue()) ;
    }

    }
    }


    /************DisplayJAIWithPixelInfo class************/

    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
    import java.awt.image.DataBuffer;
    import java.awt.image.IndexColorModel;
    import java.awt.image.RenderedImage;
    import javax.media.jai.iterator.RandomIter;
    import javax.media.jai.iterator.RandomIterFactory;
    import com.sun.media.jai.widget.DisplayJAI;

    /**
    * This class shows how one can extend the DisplayJAI class. We'll override the
    * mouseMoved method of DisplayJAI so when the mouse is moved, some information
    * about the pixel beneath the mouse will be stored (but not displayed).
    */
    public class DisplayJAIWithPixelInfo extends DisplayJAI implements MouseMotionListener
    {
    private StringBuffer pixelInfo; // the pixel information (formatted in a StringBuffer).
    private double[] dpixel; // the pixel information as an array of doubles.
    private int[] ipixel; // the pixel information as an array of integers.
    private boolean isDoubleType; // indicates which of the above arrays we will use.
    private RandomIter readIterator; // a RandomIter that allow us to get the data
    // on a single pixel.
    private boolean isIndexed; // true if the image has a indexed color model.
    private short[][] lutData; // will contain the look-up table data if isIndexed is true.
    protected int width,height; // the dimensions of the image

    /**
    * The constructor of the class, which creates the arrays and instances needed
    * to obtain the image data and registers the class to listen to mouse motion
    * events.
    * @param image a RenderedImage for display
    */
    public DisplayJAIWithPixelInfo(RenderedImage image)
    {
    super(image); // calls the constructor for DisplayJAI.
    readIterator = RandomIterFactory.create(image, null); // creates the iterator.
    // Get some data about the image
    width = image.getWidth();
    height = image.getHeight();
    int dataType = image.getSampleModel().getDataType(); // gets the data type
    switch(dataType)
    {
    case DataBuffer.TYPE_BYTE:
    case DataBuffer.TYPE_SHORT:
    case DataBuffer.TYPE_USHORT:
    case DataBuffer.TYPE_INT: isDoubleType = false; break;
    case DataBuffer.TYPE_FLOAT:
    case DataBuffer.TYPE_DOUBLE: isDoubleType = true; break;
    }
    // Depending on the image data type, allocate the double or the int array.
    if (isDoubleType) dpixel = new double[image.getSampleModel().getNumBands()];
    else ipixel = new int[image.getSampleModel().getNumBands()];
    // Is the image color model indexed ?
    isIndexed = (image.getColorModel() instanceof IndexColorModel);
    if (isIndexed)
    {
    // Retrieve the index color model of the image.
    IndexColorModel icm = (IndexColorModel)image.getColorModel();
    // Get the number of elements in each band of the colormap.
    int mapSize = icm.getMapSize();
    // Allocate an array for the lookup table data.
    byte[][] templutData = new byte[3][mapSize];
    // Load the lookup table data from the IndexColorModel.
    icm.getReds(templutData[0]);
    icm.getGreens(templutData[1]);
    icm.getBlues(templutData[2]);
    // Load the lookup table data into a short array to avoid negative numbers.
    lutData = new short[3][mapSize];
    for(int entry=0;entry<mapSize;entry++)
    {
    lutData[0][entry] = templutData[0][entry] > 0 ?
    templutData[0][entry] : (short)(templutData[0][entry]+256);
    lutData[1][entry] = templutData[1][entry] > 0 ?
    templutData[1][entry] : (short)(templutData[1][entry]+256);
    lutData[2][entry] = templutData[2][entry] > 0 ?
    templutData[2][entry] : (short)(templutData[2][entry]+256);
    }
    } // end if indexed
    // Registers the mouse motion listener.
    addMouseMotionListener(this);
    // Create the StringBuffer instance for the pixel information.
    pixelInfo = new StringBuffer(50);
    }

    /**
    * This method will be called when the mouse is moved over the image being
    * displayed.
    * @param me the mouse event that caused the execution of this method.
    */
    public void mouseMoved(MouseEvent me)
    {
    pixelInfo.setLength(0); // clear the StringBuffer
    int x = me.getX();
    int y = me.getY();
    if ((x >= width) || (y >= height))
    {
    pixelInfo.append("No data!");
    return;
    }
    if (isDoubleType) // process the pixel as an array of double values
    {
    pixelInfo.append("(floating-point data) ");
    readIterator.getPixel(me.getX(),me.getY(),dpixel); // read the pixel
    for(int b=0;b<dpixel.length;b++)
    pixelInfo.append(dpixel[b]+","); // append to the StringBuffer
    pixelInfo = pixelInfo.deleteCharAt(pixelInfo.length()-1); // erase last comma
    }
    else // pixel type is not floating point, will be processed as integers.
    {
    if (isIndexed) // if color model is indexed
    {
    pixelInfo.append("(integer data with colormap) ");
    readIterator.getPixel(me.getX(),me.getY(),ipixel); // read the pixel
    // Assume ipixel.length = 1
    pixelInfo.append("Index: "+ipixel[0]);
    // Add also the RGB entry from the LUT.
    pixelInfo.append(" RGB:"+lutData[0][ipixel[0]]+","+
    lutData[1][ipixel[0]]+","+
    lutData[2][ipixel[0]]);
    }
    else // pixels are of integer type, but not indexed
    {
    pixelInfo.append("(integer data) ");
    readIterator.getPixel(me.getX(),me.getY(),ipixel); // read the pixel
    for(int b=0;b<ipixel.length;b++)
    pixelInfo.append(ipixel[b]+","); // append to the StringBuffer
    pixelInfo = pixelInfo.deleteCharAt(pixelInfo.length()-1); // erase last comma
    }
    } // pixel is integer type
    } // end of method mouseMoved

    /**
    * This method allows external classes access to the pixel info which was
    * obtained in the mouseMoved method.
    * @return the pixel information, formatted as a string
    */
    public String getPixelInfo()
    {
    return pixelInfo.toString();
    }

    } // end class

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Not able to load a Planarimage in a JPanel

    Too much code to look at. Read the section from the Swing tutorial on How to Use Icons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) for short, examples and explanations.

  3. #3
    Sharad99jash is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default Re: Not able to load a Planarimage in a JPanel

    Already read that tutorials from Oracle and I need to open TIFF and GeoTIFF satellite images so that i can do some manipulation on those images.
    If you can rectify the ImagePanel class and button1 code of LoadMyImage it will be helpful.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Not able to load a Planarimage in a JPanel

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    One problem I see is the code uses third party packages that others might not have. Can you rewrite the code for testing to only use Java SE classes?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: 10-28-2012, 05:14 PM
  2. could not load load from location:/test_subreport.jasper
    By jadeite100 in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 03-27-2012, 12:43 PM
  3. Unable to load an image on JPanel
    By shanthi in forum Java Applets
    Replies: 1
    Last Post: 02-28-2011, 05:55 PM
  4. Load image to jPanel
    By DJIT in forum AWT / Swing
    Replies: 7
    Last Post: 12-27-2010, 01:48 PM
  5. Load website on a JPanel.
    By Masochist in forum New To Java
    Replies: 2
    Last Post: 05-11-2010, 08:44 PM

Tags for this Thread

Posting Permissions

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