请参见以下代码。我正在尝试在JPanel上显示图像。查看屏幕快照。我想在FrameImagePanel上绘制图像。但它不起作用。
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
}
看上面的部分。该代码应该显示图像。 ??此处未添加部分代码。因为stackoverflow不允许太多详细信息代码。
{{1}}
您无需检查所有代码,只需参见上面的部分。