/* * This software is distributed as-is with no warranty of any kind and I accept no responsibility * for its use. You may use this software however you wish, just give me credit. * * Mike Cynn (http://mike.cynn.com/applets/pascal/PascalPanel.java) */ import javax.swing.*; import javax.swing.text.*; import java.awt.event.*; import java.awt.*; import java.text.*; public class PascalPanel extends JPanel implements ActionListener { private JTextField NUMROWS; private JTextArea DISPLAY; private JButton DOIT; private JButton CLR; private static String CLEAR_COMMAND = "clear"; private PascalPanel treePanel; public PascalPanel() { super(new FlowLayout()); DOIT = new JButton("Do it!"); CLR = new JButton("Clear"); NUMROWS = new JTextField(10); DISPLAY = new JTextArea(20,100); DISPLAY.setLineWrap(false); DISPLAY.setEditable(false); setLayout(new BorderLayout()); Font font = new Font("fixed",1,10); DISPLAY.setFont(font); JScrollPane sp = new JScrollPane(DISPLAY); sp.setPreferredSize(new Dimension(800,1000)); sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); add("Center", sp); Panel SouthPanel = new Panel(); Panel NorthPanel = new Panel(); NorthPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); NorthPanel.add("North",new Label("Rows: ")); NorthPanel.add("North",NUMROWS); DOIT.addActionListener(this); CLR.addActionListener(this); NorthPanel.add("North",DOIT); NorthPanel.add("North",CLR); add("South",SouthPanel); add("North",NorthPanel); } public void printOut(String s) { int maxDig=0; // number of digits in biggest number in triangle int curDig=0; // number of digits in current element double result; // the value of the entry /* NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(0); nf.setGroupingUsed(false); */ int num = Integer.parseInt(s); int prev[][] = new int[num][num]; // lengths of string bet each num String vals[][] = new String[num][num]; // the PT values StringBuffer strBuf = new StringBuffer(); DecimalFormat df = new DecimalFormat(); df.setGroupingUsed(false); df.setMaximumFractionDigits(0); DecimalFormat dfexp = new DecimalFormat("#.##############################E0"); initArray(num,vals); DISPLAY.append("\n"+s+" Rows of Pascal's Triangle: \n\n"); double largest = 0; for(int i=0;i 10000000000.0) { vals[i][j] = dfexp.format(result); } else { vals[i][j] = df.format(result); } if(result > largest) { largest = result; // find largest number to count digits } } // end of one row } // end of all rows maxDig = df.format(largest).length(); for(int n=0;nn;k--) { for(int a=maxDig;a>=0;a--) { strBuf.append(" "); // spaces in front of first element } } } strBuf.append(vals[n][m]); // the element/number prev[n][m] = strBuf.toString().length(); if(n!=0) { // after first row int nextDig; // number of digits in next value if(m < vals[n].length-1) { nextDig = vals[n][m+1].length(); } else { nextDig = 0; } int len = strBuf.toString().length(); int mx; if(m == n) { mx = len + 2*(prev[n-1][1]-len); } else { mx = len + 2*(prev[n-1][m]-len) - nextDig; } while(len <= mx) { strBuf.append(" "); // spaces between values len++; } } } // end of one row strBuf.append("\n"); DISPLAY.append(strBuf.toString()); strBuf.setLength(0); } // end of all rows } // end printOut method public void initArray(int num,String vals[][]) { for(int goo=0;goo