簡體   English   中英

Java Applet不創建形狀

[英]Java Applet not creating shapes

嘗試制作一個簡單的小程序-第一部分只是創建一個簡單的gui,用戶可以在其中選擇數字,形狀和顏色...然后當他們提交它時,加載另一個小程序,該小程序將獲取該信息並以該顏色創建許多形狀。 非常簡單,除了無法正常工作-第二個applet只是空白。 它不是網站的一部分,只是試圖使其在Netbeans中工作。

創建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                   


}

第二部分應該做繪圖...

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;
          }

        }
}
}

小程序是在頁面上運行的獨立應用程序。 您可以讓他們進行交流。 查看此示例代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM