// // FSC.java // FSC // // Created by larry aasen and jason rusoff // // package FSC; import java.awt.*; import java.awt.event.*; import java.text.*; import javax.swing.*; import java.awt.Rectangle; import java.lang.reflect.Array; import javax.swing.table.*; public class FSCApplet extends JApplet { private JTable myTable; private FSCTableModel myTableModel = new FSCTableModel(); private JLabel jLabel1 = new JLabel(); private JComboBox cmbFilmStock = new JComboBox(); private TextField txtRatio = new TextField(); private TextField txtRunningTime = new TextField(); private TextField txtStockPrice = new TextField(); private TextField txtDiscount = new TextField(); private TextField txtTax = new TextField(); private JButton btnCalcLen = new JButton(); private JButton btnClearList = new JButton(); private JLabel jLabel2 = new JLabel(); private JLabel jLabel3 = new JLabel(); private JLabel jLabel4 = new JLabel(); private JLabel jLabel5 = new JLabel(); private JLabel jLabel6 = new JLabel(); private JLabel jLabel7 = new JLabel(); private JLabel jLabel8 = new JLabel(); private JLabel jLabel9 = new JLabel(); private JLabel jLabel10 = new JLabel(); private JLabel jLabel11 = new JLabel(); private JLabel jLabel12 = new JLabel(); //private List lstOutput = new List(); private FilmStockCalc mFilmStockCalc = new FilmStockCalc(); private double m_cost; private int m_totalFeet; public void init() { try { jbInit(); // Make a table that uses our custom JSC data model myTable = new JTable(myTableModel); myTableModel.addTableModelListener(myTable);// do I need this?? // set my own renderer so I can justify text. There's gotta be a better way, but I couldn't yet find it for(int i=0; i< myTable.getColumnCount(); ++i) { TableColumn tc= myTable.getColumn(myTable.getColumnName(i)); tc.setCellRenderer(new MyRenderer()); } JScrollPane scrollpane = new JScrollPane(myTable); scrollpane.setBounds(new Rectangle(10, 185, 540, 125));// copy original list bounds, for now this.getContentPane().add(scrollpane, null); for ( int zz=0; zz!=mFilmStockCalc.getFilmStockList().size(); zz++ ) { FilmStock stock = (FilmStock)mFilmStockCalc.getFilmStockList().get( zz ); cmbFilmStock.addItem( stock.getName() ); } } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { FSCApplet applet = new FSCApplet(); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(applet, BorderLayout.CENTER); frame.setTitle("Film Stock Calculator"); applet.init(); applet.start(); frame.setSize( 569, 348 ); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); frame.setLocation((d.width - frameSize.width) / 2, (d.height - frameSize.height) / 2); frame.setVisible(true); } public void start() { } public void stop() { } public void destroy() { } private void jbInit() throws Exception { this.getContentPane().setLayout(null); this.setSize(new Dimension(564, 318)); this.setBackground(Color.lightGray); jLabel1.setText("Film Stock Calculator"); jLabel1.setBounds(new Rectangle(5, 0, 290, 35)); jLabel1.setFont(new Font("Arial", 1, 16)); jLabel1.setAlignmentY((float)0.0); jLabel1.setForeground(Color.blue); cmbFilmStock.setBounds(new Rectangle(10, 75, 115, 20)); txtRatio.setText("8"); txtRatio.setBounds(new Rectangle(125, 75, 95, 20)); txtRunningTime.setText("90"); txtRunningTime.setBounds(new Rectangle(220, 75, 95, 20)); txtStockPrice.setText("0.30"); txtStockPrice.setBounds(new Rectangle(315, 75, 95, 20)); txtDiscount.setText("0.0"); txtDiscount.setBounds(new Rectangle(410, 75, 70, 20)); txtTax.setText("8.25"); txtTax.setBounds(new Rectangle(480, 75, 70, 20)); btnCalcLen.setText("Calculate"); btnCalcLen.setBounds(new Rectangle(215, 110, 130, 25)); btnCalcLen.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { btnCalcLen_actionPerformed(e); } }); btnClearList.setText("Clear List"); btnClearList.setBounds(new Rectangle(215, 140, 130, 25)); btnClearList.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { btnClearList_actionPerformed(e); } }); jLabel2.setText("Film Stock"); jLabel2.setBounds(new Rectangle(10, 40, 115, 20)); jLabel2.setHorizontalAlignment(JLabel.CENTER); jLabel2.setForeground(Color.blue); jLabel2.setFont(new Font("Arial", 0, 11)); jLabel3.setText("Running Time"); jLabel3.setBounds(new Rectangle(220, 40, 95, 20)); jLabel3.setHorizontalAlignment(JLabel.CENTER); jLabel3.setForeground(Color.blue); jLabel3.setFont(new Font("Arial", 0, 11)); jLabel4.setText("(minutes)"); jLabel4.setBounds(new Rectangle(220, 55, 95, 20)); jLabel4.setHorizontalAlignment(JLabel.CENTER); jLabel4.setForeground(Color.blue); jLabel4.setFont(new Font("Arial", 0, 11)); jLabel5.setText("Shooting Ratio"); jLabel5.setBounds(new Rectangle(125, 40, 95, 20)); jLabel5.setHorizontalAlignment(JLabel.CENTER); jLabel5.setForeground(Color.blue); jLabel5.setFont(new Font("Arial", 0, 11)); jLabel6.setText("(?:1)"); jLabel6.setBounds(new Rectangle(125, 55, 95, 20)); jLabel6.setHorizontalAlignment(JLabel.CENTER); jLabel6.setForeground(Color.blue); jLabel6.setFont(new Font("Arial", 0, 11)); jLabel7.setText("Film Stock Price"); jLabel7.setBounds(new Rectangle(315, 40, 95, 20)); jLabel7.setHorizontalAlignment(JLabel.CENTER); jLabel7.setForeground(Color.blue); jLabel7.setFont(new Font("Arial", 0, 11)); jLabel8.setText("(per foot)"); jLabel8.setBounds(new Rectangle(315, 55, 95, 20)); jLabel8.setHorizontalAlignment(JLabel.CENTER); jLabel8.setForeground(Color.blue); jLabel8.setFont(new Font("Arial", 0, 11)); jLabel9.setText("(%)"); jLabel9.setBounds(new Rectangle(410, 55, 70, 20)); jLabel9.setHorizontalAlignment(JLabel.CENTER); jLabel9.setForeground(Color.blue); jLabel9.setFont(new Font("Arial", 0, 11)); jLabel10.setText("Discount"); jLabel10.setBounds(new Rectangle(410, 40, 70, 20)); jLabel10.setHorizontalAlignment(JLabel.CENTER); jLabel10.setForeground(Color.blue); jLabel10.setFont(new Font("Arial", 0, 11)); jLabel11.setText("(%)"); jLabel11.setBounds(new Rectangle(480, 55, 70, 20)); jLabel11.setHorizontalAlignment(JLabel.CENTER); jLabel11.setForeground(Color.blue); jLabel11.setFont(new Font("Arial", 0, 11)); jLabel12.setText("Tax"); jLabel12.setBounds(new Rectangle(480, 40, 70, 20)); jLabel12.setHorizontalAlignment(JLabel.CENTER); jLabel12.setForeground(Color.blue); jLabel12.setFont(new Font("Arial", 0, 11)); // lstOutput.setBounds(new Rectangle(10, 185, 540, 125)); // lstOutput.setFont(new Font("Courier", 0, 12)); this.getContentPane().add(cmbFilmStock, null); //this.getContentPane().add(lstOutput, null); this.getContentPane().add(jLabel12, null); this.getContentPane().add(jLabel11, null); this.getContentPane().add(jLabel10, null); this.getContentPane().add(jLabel9, null); this.getContentPane().add(jLabel8, null); this.getContentPane().add(jLabel7, null); this.getContentPane().add(jLabel6, null); this.getContentPane().add(jLabel5, null); this.getContentPane().add(jLabel4, null); this.getContentPane().add(jLabel3, null); this.getContentPane().add(jLabel2, null); this.getContentPane().add(btnClearList, null); this.getContentPane().add(btnCalcLen, null); //this.getContentPane().add(lstOutput, null); this.getContentPane().add(txtTax, null); this.getContentPane().add(txtDiscount, null); this.getContentPane().add(txtStockPrice, null); this.getContentPane().add(txtRunningTime, null); this.getContentPane().add(txtRatio, null); this.getContentPane().add(jLabel1, null); } static { try { // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); } catch(Exception e) { } } private void ClearList() { myTableModel.removeAll(); } private void btnCalcLen_actionPerformed(ActionEvent e) { OnCalcLength(); UpdateList(); } private void OnCalcLength() { try { int idx = cmbFilmStock.getSelectedIndex(); if ( idx < 0 ) return; FilmStock stock = (FilmStock)mFilmStockCalc.getFilmStockList().get( idx ); String sRatio = txtRatio.getText(); int ratio = Integer.parseInt( sRatio ); String sTime = txtRunningTime.getText(); int m_Time = Integer.parseInt( sTime ); String sPrice = txtStockPrice.getText(); m_cost = Double.parseDouble( sPrice ); String sDisc = txtDiscount.getText(); double m_Disc = Double.parseDouble( sDisc ); String sTax = txtTax.getText(); double m_Tax = Double.parseDouble( sTax ); // Determine the length m_totalFeet = mFilmStockCalc.CalculateLength( stock, ratio, m_Time ); // Determine the cost m_cost = mFilmStockCalc.CalculateCost( stock, ratio, m_Time, m_cost, m_Disc, m_Tax ); } catch( Exception e ) { } } private void UpdateList() { Integer iLength = new Integer( m_totalFeet ); // For now, we simply assume the output is an array of 8 strings. We may want to // change this so we can keep numeric values... String outputArray[] = (String[])Array.newInstance(String.class,8); int arrayIndex = 0; // "Cost Stock Ratio Time Price Discount Tax Length" // Cost DecimalFormat formatter = new DecimalFormat( "$#,##0.00" ); //String strLine; outputArray[arrayIndex++] = formatter.format( m_cost );// LeftPadString( formatter.format( m_cost ), 13 ); // Stock outputArray[arrayIndex++] = (String)cmbFilmStock.getSelectedItem();//LeftPadString( (String)cmbFilmStock.getSelectedItem(), 8 ); // Ratio outputArray[arrayIndex++]= txtRatio.getText();//LeftPadString( txtRatio.getText(), 5 ); // Time outputArray[arrayIndex++] = txtRunningTime.getText();//LeftPadString( txtRunningTime.getText(), 4 ); // Price outputArray[arrayIndex++] = txtStockPrice.getText();//LeftPadString( txtStockPrice.getText(), 5 ); // Discount outputArray[arrayIndex++] = txtDiscount.getText();//LeftPadString( txtDiscount.getText(), 5 ); // Tax outputArray[arrayIndex++] = txtTax.getText();//LeftPadString( txtTax.getText(), 4 ); // Length outputArray[arrayIndex++] = iLength.toString();//LeftPadString( iLength.toString(), 6 ); myTableModel.addNewRow(outputArray); } private void txtStockPrice_actionPerformed(ActionEvent e) { } private void btnClearList_actionPerformed(ActionEvent e) { ClearList(); } private String LeftPadString( String text, int width ) { int length = text.length(); if ( length >= width ) return text; String strPad = ""; for ( int zz=0; zz!=width-length; zz++ ) strPad += " "; return strPad + text; } private String RightPadString( String text, int width ) { int length = text.length(); if ( length >= width ) return text; String strPad = ""; for ( int zz=0; zz!=width-length; zz++ ) strPad += " "; return text + strPad; } } //CellRenderer class MyRenderer extends DefaultTableCellRenderer { public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected,boolean hasFocus,int row,int column) { switch(column) { default: setHorizontalAlignment(JLabel.CENTER); break; } return super.getTableCellRendererComponent(table,value, isSelected, hasFocus, row, column); } }