Thursday, 18 August 2016

java - add image to JPanel in NetBeans



i am new to java swing, and i am using now NetBeant for building the GUI using the drag and drop designer built in NetBeans. the problem i am facing now is, i have the below posted code and it was written in Eclipse, and as you see
I have a class that extends JPanel and I am adding an image captured from the WEB_CAM to that JPanel.now since i switched to NetBeans, I created the JFrame, JPanel, JButtons using the designer, and in the code i am writing in NetBeans, i do not
know how to add the captured image to the JPanel.



in eclipse I created the below posted code in another class, but in NetBeans every thing is generated automatically and i have to do the same work i did in eclipse. i do not know how to add an image to JPanel in NetBeans.



JPanel inEclipse




class FacePanel extends JPanel {
private BufferedImage image;
int count = 0;

public FacePanel() {
super();
}

public void setFace (BufferedImage img) {

this.image = img;
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

if (this.image == null) {
System.out.println("image is null");
return;
}


g.drawImage(this.image, 10, 10, this.image.getWidth(), this.image.getHeight(), null);
g.setFont(new Font("arial", 2, 20));
g.setColor(Color.WHITE);
g.drawString("processing frame by frame [ frame: " + ( count++) + " ]", 150, 150);
}

}



Code in NetBeans



    private void initComponents() {

jpanel1_Preview = new javax.swing.JPanel();
btn_Play = new javax.swing.JButton();
btn_Pause = new javax.swing.JButton();
btn_Capture = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);


jpanel1_Preview.setBorder(javax.swing.BorderFactory.createTitledBorder("Cam_Preview"));

javax.swing.GroupLayout jpanel1_PreviewLayout = new javax.swing.GroupLayout(jpanel1_Preview);
jpanel1_Preview.setLayout(jpanel1_PreviewLayout);
jpanel1_PreviewLayout.setHorizontalGroup(
jpanel1_PreviewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jpanel1_PreviewLayout.setVerticalGroup(

jpanel1_PreviewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 225, Short.MAX_VALUE)
);

btn_Play.setText("play");
btn_Play.setSelected(true);

btn_Pause.setText("pause");
btn_Pause.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {

btn_PauseActionPerformed(evt);
}
});

btn_Capture.setText("capture");
btn_Capture.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_CaptureActionPerformed(evt);
}
});


Answer




  1. Change class FacePanel extends JPanel { to public class FacePanel extends JPanel {, otherwise Netbeans won't be able to create an instance of it.

  2. Do a "Clean and build"

  3. Drag the FacePanel class from the Project explorer onto your form...



Drag


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...