/** * Prime Number Generator. * @author Mark Chamness * Any suggestions are appreciated. * * This applet calculates prime numbers. * It stores the first 10,000 primes it generates. * It evaluates the rest based on those * such that (prime[n] squared) < test prime. * The 10,000th prime save is 104,743 . So this applet * should calculate primes to 10,971,096,049 * * Since this Applet would run out of memory, * the TextArea is cleared after every 200,000 primes listed. **/ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PrimesApplet extends JApplet implements ActionListener { JButton runButton = new JButton("Run"); JButton stopButton = new JButton("Stop"); Color appColor = Color.lightGray; private JTextArea dataTextArea = new JTextArea(20, 40); private JScrollPane scrollPane = new JScrollPane(dataTextArea); private Calculate calc = new Calculate(dataTextArea); public PrimesApplet() { Container cont = getContentPane(); cont.setLayout(new BorderLayout()); JPanel northPanel = new JPanel(); northPanel.setLayout(new GridLayout(2, 1)); northPanel.setBackground(Color.gray); northPanel.add(new JLabel("Calculate Primes", JLabel.CENTER)); northPanel.add(new JLabel("Click Run to Start", JLabel.CENTER)); northPanel.setBackground(appColor); cont.add("North", northPanel); JPanel southPanel = new JPanel(); southPanel.setBackground(appColor); southPanel.setLayout(new FlowLayout()); runButton.addActionListener(this); runButton.setPreferredSize(new Dimension(85, 23)); southPanel.add(runButton); stopButton.addActionListener(this); southPanel.add(stopButton); cont.add("South", southPanel); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); scrollPane.setPreferredSize(new Dimension(100, 200)); cont.add("Center", scrollPane); cont.setBackground(appColor); runButton.requestFocus(); } public void actionPerformed(ActionEvent ae) { Object object = ae.getSource(); if (object == runButton) { if (calc.getStatus() == Calculate.STOPPED) { dataTextArea.setText(""); calc = new Calculate(dataTextArea); calc.start(); runButton.setText("Pause"); } else if (calc.getStatus() == Calculate.RUNNING) { calc.pause(); runButton.setText("Resume"); } else if (calc.getStatus() == Calculate.PAUSED) { runButton.setText("Pause"); calc.continueRunning(); } } else if (object == stopButton) { calc.stopRunning(); runButton.setText("Run"); } } }