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.
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.