afisher1117
5/26/2018 - 3:59 PM

Prime.java

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Set;
import java.util.TreeSet;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;

public class Prime {
	public static void main(String[] args) {
		new Prime();
	}
	
	ArrayList<String> numbers = new ArrayList<String>();
    Border paddingBorder = BorderFactory.createEmptyBorder(3, 3, 3, 3);
	
	public Prime() {
		EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Sieve of Eratosthenes");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setPreferredSize(new Dimension(800, 800));
                frame.setSize(800, 800);
                frame.setResizable(false);
                frame.add(new MakeGridPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
	}
	
	public class MakeGridPanel extends JPanel{

		public MakeGridPanel(){
            setSize(getPreferredSize());
            setBorder(paddingBorder);
            int n = getNumbers();
            int squareRootN = ((int)Math.sqrt(n)) + 1;
			setLayout(new GridLayout(squareRootN, squareRootN, 3, 3));
			setBackground(Color.black);
			
			JPanel[] panel = new JPanel[n];
			JLabel[] label = new JLabel[n];
			
			System.out.println(Math.sqrt(n));
			System.out.println(squareRootN); //debugging
			
			int count = 1;
			for(int rows = 0; rows < squareRootN; rows++) {
				for(int cols = 0; cols < squareRootN; cols++) {
					count++;
					panel[rows] = new JPanel(new GridBagLayout());
					panel[rows].setSize(getPreferredSize());
					panel[rows].setBackground(Color.white);
					add(panel[rows]);
					label[rows] = new JLabel("<html><h2 style='vertical-align text-align: center;'>" + 
							Integer.toString(count) + 
							"</h2></html>", SwingConstants.CENTER);
					label[rows].setVerticalTextPosition(SwingConstants.CENTER);
					label[rows].setSize(getPreferredSize());
	                panel[rows].add(label[rows]);
	                System.out.println("COLS " + cols + " inner loop ran"); //debugging
	                System.out.println(label[rows].getText());
				}
				System.out.println("ROWS " + rows + " outer loop ran"); //debugging
			}
		}
	}
	
	public int getNumbers() {
		String input = JOptionPane.showInputDialog("Enter number to find primes up to: ");
		int n = Integer.parseInt(input);
		return n;
	}	
	
	public void getPrimes(int n) {
		Set<Integer> primes = new TreeSet<>();
        boolean[] a = new boolean[n + 1];
        
        for(int i = 0; i < a.length; i++){
            a[i] = true;
        }
        
        for(int i = 2; i <= n; i++){
            if(a[i]){
                for(int j = i*i; j <= n; j = j + i){
                    a[j] = false;
                }
            }
        }
        
        for(int i = 0; i < a.length; i++){
            if(a[i] && i > 1){
                primes.add(i);
            }
        }
	}
	
}