Advertisement
jasonpogi1669

Prime Numbers Generation using Java with GUI

May 2nd, 2021
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package main;
  7.  
  8. /**
  9.  *
  10.  * @author markjasongalang
  11.  */
  12. import java.awt.*;
  13. import java.awt.event.*;
  14. import java.util.Locale;
  15. import javax.swing.*;
  16.  
  17. public class Main implements ActionListener {
  18.     private JFrame frame = new JFrame();
  19.     private JPanel main_panel = new JPanel();
  20.     private JPanel[] mini_panels;
  21.     private JLabel label;
  22.     private JScrollPane sp;
  23.    
  24.     boolean CheckPrime (int n) {
  25.         boolean prime = true;
  26.         for (int i = 2; i <= (int) Math.sqrt(n); i++) {
  27.             if (n % i == 0) {
  28.                 prime = false;
  29.                 break;
  30.             }
  31.         }
  32.         return prime;
  33.     }
  34.        
  35.     Main() {
  36.         frame.setTitle("Sample");
  37. //        frame.setLocationRelativeTo(null);
  38.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.         frame.setResizable(false);
  40.        
  41.         int n = 640;
  42.         int root = (int) (Math.sqrt(n));
  43.         mini_panels = new JPanel[n];
  44.         main_panel.setLayout(new FlowLayout(FlowLayout.LEADING, 10, 10));
  45.         for (int i = 0; i < n; i++) {
  46.             mini_panels[i] = new JPanel();
  47.             label = new JLabel(Integer.toString(i + 1));
  48.             label.setFont(new Font("ARIAL", Font.PLAIN, 30));
  49.             mini_panels[i].setLayout(new GridBagLayout());
  50.             mini_panels[i].add(label);
  51.             mini_panels[i].setPreferredSize(new Dimension(100, 100));
  52.             if (i + 1 <= 1) {
  53.                 mini_panels[i].setBackground(Color.GRAY);
  54.             } else if (CheckPrime(i + 1)) {
  55.                 mini_panels[i].setBackground(Color.GREEN);
  56.             } else {
  57.                 mini_panels[i].setBackground(Color.YELLOW);
  58.             }
  59.             main_panel.add(mini_panels[i]);
  60.         }
  61.         int size = ((n % 8 == 0 ? n / 8 : n / 8 + 1)) * 110;
  62.         main_panel.setPreferredSize(new Dimension(900, size));
  63.         sp = new JScrollPane(main_panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  64.         frame.add(sp);
  65.         frame.setVisible(true);
  66.         frame.setSize(900, 500);
  67.     }
  68.    
  69.     @Override
  70.     public void actionPerformed(ActionEvent e) {
  71.        
  72.     }
  73.    
  74.     public static void main(String[] args) {
  75.         new Main();
  76.     }
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement