Ask many times, almost despaired.
Please see my executable code. I am going to plot a 2D object, but can't add on the panel. What's wrong?
import java.io.*;
import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.BasicStroke;
import java.awt.GradientPaint;
import java.awt.geom.GeneralPath;
import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.geom.*;
import java.awt.*;
import java.awt.BorderLayout;
import javax.swing.*;
import java.util.Hashtable;
import java.awt.event.*;
import javax.swing.event.*;
import java.applet.Applet;
publicclass Comboxextends JApplet
{
private Drawing drawing;
private JPanel jPanel1;
private JSlider dJSlider;
private JComboBox jComboBox1;
private JComboBox jComboBox2;
private JComboBox jComboBox3;
private Container panel;
privateint id;
private String af[] ={"Paint","No Paint"};
privatedouble scale=1;
privateint a;
private String energyId[] ={"one","two"};
publicvoid init(){
try{
java.awt.EventQueue.invokeAndWait(new Runnable(){
publicvoid run(){
initComponents();
}
});
}catch (Exception ex){
ex.printStackTrace();
}
}
privatevoid initComponents(){
jPanel1 =new JPanel();
jComboBox1 =new JComboBox();
jComboBox2 =new JComboBox();
jComboBox3 =new JComboBox();
int min = 1, max = 16, inc = 5;
dJSlider =new javax.swing.JSlider(min, max, 6);
dJSlider.setMajorTickSpacing(5);
dJSlider.setMinorTickSpacing(1);
dJSlider.setSnapToTicks(true);
dJSlider.setLabelTable(getLabelTable(min, max, inc));
dJSlider.setPaintTicks(true);
dJSlider.setPaintLabels(true);
dJSlider.addChangeListener(
new ChangeListener()// anonymous inner class
// handle change in slider value
{
publicvoid stateChanged(ChangeEvent e)
{
int value = ((JSlider)e.getSource()).getValue();
double scale = (value+4)/10.0;drawing.setscale(scale);
}
}
);
//panel = getContentPane();panel.add(jPanel1);
drawing =new Drawing(a,id,scale);// probably wrong here around
jPanel1.add(drawing,"Center");// probably wrong here
jPanel1.setLayout(new java.awt.GridBagLayout());
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(energyId));
jComboBox1.addActionListener(
new ActionListener()// anonymous inner class
{
publicvoid actionPerformed(ActionEvent e)
{
JComboBox cb = (JComboBox)e.getSource();
id = cb.getSelectedIndex();
drawing.setid(id);
}
}
);
jPanel1.add(jComboBox1,new java.awt.GridBagConstraints());
jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(af));
jComboBox2.addActionListener(
new ActionListener()// anonymous inner class
{
publicvoid actionPerformed(ActionEvent e)
{
JComboBox cc = (JComboBox)e.getSource();
a = cc.getSelectedIndex();
drawing.seta(a);
}
}
);
jPanel1.add(jComboBox2,new java.awt.GridBagConstraints());
jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Item 1","Item 2","Item 3","Item 4"}));
jPanel1.add(jComboBox3,new java.awt.GridBagConstraints());
jPanel1.add(dJSlider,new java.awt.GridBagConstraints());
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.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
}
private Hashtable getLabelTable(int min,int max,int inc){
Hashtable<Integer, JLabel> table =new Hashtable<Integer, JLabel>();
for(int j = min; j <= max; j+=inc){
String s = String.format("%.1f", (j+4)/10.0);
table.put(Integer.valueOf(j),new JLabel(s));
}
return table;
}
class Drawingextends JPanel
{
privatedouble x1, y1, x2, y2;// to plot lines from (x1,y1) to (x2,y2)
privatedouble lineWidth;
int a,id;
double scale;
Drawing(int a,int id,double scale)
{this.a = a;
this.id = id;
this.scale = scale;
}
publicvoid setscale(double scale)
{
this.scale = scale;
repaint();
}
publicvoid setid(int id)
{
this.id = id;
repaint();
}
publicvoid seta(int a)
{
this.a = a;
repaint();
}
publicvoid paintComponent(Graphics g)
{
super.paintComponent( g );// call superclass's paint method
this.setBackground( Color.WHITE );
Graphics2D g2d = (Graphics2D)g;// cast g to Graphics 2D
g2d.drawRect(4*(int)scale+2,40*id+50,a*80+41,88);
}
// end method paintComponent
}
publicstaticvoid main(String[] args)
{
JFrame appletFrame =new JFrame("My Applet");
Applet theApplet =new Combox();
appletFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
appletFrame.setSize(800,800);
appletFrame.add(theApplet,"Center");
theApplet.init();
appletFrame.setVisible(true);
}
}
any help, thank a million.
[11510 byte] By [
ardmorea] at [2007-11-27 10:57:13]

# 1
I'm using j2se 1.5 and do not have access to the GroupLayout class.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.Hashtable;
import javax.swing.*;
import javax.swing.event.*;
public class ComboxRx2 extends JApplet {
private Drawing drawing;
private JPanel jPanel1;
private JSlider dJSlider;
private JComboBox jComboBox1;
private JComboBox jComboBox2;
private JComboBox jComboBox3;
//private Container panel;
private int id;
private String af[] = {"Paint","No Paint"};
private double scale=1;
private int a;
private String energyId[] ={"one","two"};
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void initComponents() {
jPanel1 = new JPanel();
jComboBox1 = new JComboBox();
jComboBox2 = new JComboBox();
jComboBox3 = new JComboBox();
int min = 1, max = 16, inc = 5;
dJSlider = new JSlider(min, max, 6);
dJSlider.setMajorTickSpacing(5);
dJSlider.setMinorTickSpacing(1);
dJSlider.setSnapToTicks(true);
dJSlider.setLabelTable(getLabelTable(min, max, inc));
dJSlider.setPaintTicks(true);
dJSlider.setPaintLabels(true);
dJSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int value = ((JSlider)e.getSource()).getValue();
double scale = (value+4)/10.0;drawing.setscale(scale);
}
});
//panel = getContentPane();panel.add(jPanel1);
// Have the variables a, id and scale been initialized/given values?
drawing = new Drawing(a,id,scale); // probably wrong here around
// Using a BorderLayout constraint: what layout does jPanel1 have?
// Until/unless you set it the default layout manager is FlowLayout.
//jPanel1.add(drawing,"Center");// probably wrong here
// You must set a containers layout manager before adding components.
//jPanel1.setLayout(new GridBagLayout());
jComboBox1.setModel(new DefaultComboBoxModel(energyId));
jComboBox1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
id = cb.getSelectedIndex();
drawing.setid(id);
}
});
jPanel1.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
// Configure constraints as you go...
gbc.insets = new Insets(2,2,2,2);
gbc.weightx = 1.0;
jPanel1.add(jComboBox1, gbc);
jComboBox2.setModel(new DefaultComboBoxModel(af));
jComboBox2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox cc = (JComboBox)e.getSource();
a = cc.getSelectedIndex();
drawing.seta(a);
}
});
jPanel1.add(jComboBox2, gbc);
jComboBox3.setModel(new DefaultComboBoxModel(
new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jPanel1.add(jComboBox3, gbc);
gbc.gridy = 1;
gbc.gridwidth = 3;
// Next line requires non-zero weightx value which we have above.
gbc.fill = GridBagConstraints.HORIZONTAL;
// If you don't want to use the fill constraint to extend the
// slider all the way across the top-level container you can,
// instead, set its preferred size. You can do it like this:
// Dimension d = dJSlider.getPreferredSize();
// d.width = 400;
// dJSlider.setPreferredSize(d);
jPanel1.add(dJSlider, gbc);
Container cp = getContentPane();
cp.add(drawing, "Center");
cp.add(jPanel1, "Last");
/*
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
);
// Looks like you added jPanel1 above and then again below. It will
// be removed from the top and added below. You can add it only once.
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
);
*/
}
private Hashtable getLabelTable(int min, int max, int inc) {
Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>();
for(int j = min; j <= max; j+=inc) {
String s = String.format("%.1f", (j+4)/10.0);
table.put(Integer.valueOf(j), new JLabel(s));
}
return table;
}
class Drawing extends JPanel {
// to plot lines from (x1,y1) to (x2,y2)
private double x1, y1, x2, y2;
private double lineWidth;
int a,id;
double scale;
Drawing(int a, int id, double scale) {
this.a = a;
this.id = id;
this.scale = scale;
}
public void setscale(double scale) {
this.scale = scale;
repaint();
}
public void setid(int id) {
this.id = id;
repaint();
}
public void seta(int a) {
this.a = a;
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent( g );
this.setBackground( Color.WHITE );
Graphics2D g2d = (Graphics2D)g;
g2d.drawRect(4*(int)scale+2,40*id+50,a*80+41,88);
}
}
public static void main(String[] args) {
JFrame appletFrame = new JFrame("My Applet");
JApplet theApplet = new ComboxRx2();
appletFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
appletFrame.setSize(800,600);
appletFrame.add(theApplet,"Center");
theApplet.init();
appletFrame.setVisible(true);
}
}