tags:

views:

33

answers:

1

Trying to make this simple applet - the first part just creates a simple gui where the user selects the number, shape and color... then when they submit that it loads another applet which takes that information and creates that many shapes in that color. Simple enough except it's not working - the second applet just sits there blank. Not part of a website, just trying to get it to work in netbeans.

The first part that creates the gui

    public class NewJApplet extends javax.swing.JApplet {

    /** Initializes the applet NewJApplet */
    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /** This method is called from within the init() method 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() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jComboBox3 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        jLabel1.setText("Number of Figures");

        jLabel2.setText("Type of Figure");

        jLabel3.setText("Color of Figure");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "4", "8", "16" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Circle", "Oval", "Rectangle", "Square" }));

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Red", "Blue", "Green", "Yellow", "Pink", "Black", "Cyan", "Magenta" }));

        jButton1.setText("Draw");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(184, 184, 184)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(55, 55, 55)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(51, 51, 51)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))))
                .addContainerGap(192, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addGap(18, 18, 18)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(18, 18, 18)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(54, 54, 54)
                .addComponent(jButton1)
                .addContainerGap(289, Short.MAX_VALUE))
        );
    }// </editor-fold>                        

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           

    }                                          

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
new DrawApplet().setVisible(true);
setVisible(false);
    }                                        


    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    public static javax.swing.JComboBox jComboBox1;
    public static javax.swing.JComboBox jComboBox2;
    public static javax.swing.JComboBox jComboBox3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration                   


}

The second part that is supposed to do the drawing...

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.io.*;

/**
 *
 * @author Jason
 */
public class DrawApplet extends Applet {


    public void paint(Graphics g) {

int size = 200;
int i = 0;
int x;
int y;
int width;
int height;
int shapeInt;

        String number = (String)NewJApplet.jComboBox1.getSelectedItem();
        int numberInt = Integer.parseInt(number);
        String shape = (String)NewJApplet.jComboBox2.getSelectedItem();
        String color = (String)NewJApplet.jComboBox3.getSelectedItem();


        for (i = 0; i < numberInt; i++) {
            if (color.equals("red")) {
                g.setColor(Color.red);
            } else if (color.equals("blue")) {
                g.setColor(Color.blue);
            } else if (color.equals("green")) {
                g.setColor(Color.green);
            } else if (color.equals("yello")) {
                g.setColor(Color.yellow);
            } else if (color.equals("pink")) {
                g.setColor(Color.pink);
            } else if (color.equals("black")) {
                g.setColor(Color.black);
            } else if (color.equals("cyan")) {
                g.setColor(Color.cyan);
            } else {
                g.setColor(Color.magenta);
            }

          x = (int)(Math.random() * size);
          y = (int)(Math.random() * size);
          width = (int)(Math.random() * size);
          height = (int)(Math.random() * size);

          if (shape.equals("circle")) {
              shapeInt = 1;
          } else if (shape.equals("oval")) {
              shapeInt = 2;
          } else if (shape.equals("rectangle")) {
              shapeInt = 3;
          } else {
              shapeInt = 4;
          }

          switch (shapeInt) {
              case 1:
                  g.fillOval(x, y, width, height);
                  break;

              case 2:
                  g.fillOval(x, y, width, height);
                  break;

              case 3:
                  g.fillRect(x, y, width, height);
                  break;

              case 4:
                  g.fillRect(x, y, width, height);
                  break;
          }

        }
}
}
+1  A: 

Applets are independent application running on a page. You can get them to communicate. Look at this sample code.

Romain Hippeau