I've been trying to program an applet with Netbeans able to draw some graphics into a jPanel, as you can see: ![enter image description here][1]
Applet form:
package Experimento2;
import javax.swing.*;
import java.awt.*;
public class Experimento2 extends javax.swing.JApplet {
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
//ZonaGrafica zg = new ZonaGrafica();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 466, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 259, Short.MAX_VALUE)
);
jButton1.setText("jButton1");
jButton2.setText("jButton2");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(23, 23, 23))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(85, 85, 85)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(37, 37, 37))
);
//jPanel1.add(zg,BorderLayout.CENTER);
//zg.repaint();
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
And the ZonaGrafica class, which is in the same package:
package Experimento2;
import javax.swing.JApplet;
import java.awt.*;
public class ZonaGrafica extends JApplet{
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
g.fillOval(45, 65, 34, 54);
g.fillOval(45, 120, 34, 54);
g.fillOval(45, 180, 34, 54);
}
}
But unfortunately, nothing happens when I run the jApplet form. In the place where the paint method is supposed to draw some red ovals, nothing is shown. I have no clue to solve this issue, and I would be grateful to whom could help me to solve it..
Don't override paint. Try with paintComponent instead.
Your ZonaGrafica object is never instantiated(commented). I hope you are at least doing that.
In addition to that, you are trying to ad a JApplet (ZonaGrafica) to another JApplet (Experimento2). Refactor ZonaGrafica and let it extend JPanel or JLabel.
The form editor “would” work with either overriden paint
or paintComponent
, though Heisenbug's suggestion to make it paintComponent
is still a good one.
From reading your code I think that you simply hit the “customise code” button added a few lines and thought that would sort it. It doesn't, because:
someContainer.add()
approach of adding UI. jPanel3 = new ZonaGrafica();
. Which brings me to: So to sum up: (1) compile your code, (2) then add the ZoneGrafica
using the “Add Java Bean” function. You will need to know its fully qualified classname (eg com.foo.ZonaGrafica
) for that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.