0

Please see the below code. I'm trying to display an image on the JPanel. Look at the screenshoot. I want to paint image over FrameImagePanel. but it is not working.

this.imagePanel = new ImagePanel();
            FrameImagePanel.add(this.imagePanel);
            FrameImagePanel.setSize(this.imagePanel.getWidth(), this.imagePanel.getHeight());

Look at the part above. this code was supposed to show the image. ?? some part of the code is not added here. as stackoverflow does not allow much details code.

enter image description here

package imagetopixelvalue;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author MARUF AHMAD
 */
public class ViewImage extends javax.swing.JFrame {
    public ImagePanel imagePanel;


    /** Creates new form ViewImage */
    public ViewImage() {
        initComponents();

      //  JFrame frame = new JFrame("Testing");
      //          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                this.imagePanel = new ImagePanel();
                FrameImagePanel.add(this.imagePanel);
                FrameImagePanel.setSize(this.imagePanel.getWidth(), this.imagePanel.getHeight());
                System.out.print(" --> "+this.imagePanel.getWidth()+" || "+ this.imagePanel.getHeight());
      //          frame.pack();
       //         frame.setLocationRelativeTo(null);
       //         frame.setVisible(true);
    }


    public class ImagePanel extends JPanel {
         int ImageH =0;
         int ImageW =0;
         BufferedImage imageBuff;

        public ImagePanel(){
            try{
                BufferedImage image = ImageIO.read(new File("image.jpg"));
                imageBuff = image;
                System.out.print(image.getWidth()+" || "+ image.getHeight());
                ImageH = image.getHeight();
                ImageW = image.getWidth();
            }
            catch(IOException e){
            }


        }



        @Override
        public Dimension getPreferredSize() {
            return new Dimension(ImageW, ImageH);
        }
        @Override
        protected void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setColor(Color.red);
            g.drawImage(imageBuff, 0, 0, this);

        }

    }


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

        FrameImagePanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        FrameImagePanel.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout FrameImagePanelLayout = new javax.swing.GroupLayout(FrameImagePanel);
        FrameImagePanel.setLayout(FrameImagePanelLayout);
        FrameImagePanelLayout.setHorizontalGroup(
            FrameImagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 380, Short.MAX_VALUE)
        );
        FrameImagePanelLayout.setVerticalGroup(
            FrameImagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 278, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(FrameImagePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(FrameImagePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

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

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ViewImage().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JPanel FrameImagePanel;
    // End of variables declaration
}

you do not need to check all the code just see the above part.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ahmad
  • 1
  • 3
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 3) Don't ignore exceptions! They inform us exactly what went wrong. Unless logging is implemented, at least call `Throwable.printStackTrace()` .. – Andrew Thompson Dec 09 '18 at 18:55
  • .. 4) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. 5) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Dec 09 '18 at 18:55

0 Answers0