Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package main;
- /**
- *
- * @author markjasongalang
- */
- import java.awt.*;
- import java.awt.event.*;
- import java.util.Locale;
- import javax.swing.*;
- public class Main implements ActionListener {
- private JFrame frame = new JFrame();
- private JPanel main_panel = new JPanel();
- private JPanel[] mini_panels;
- private JLabel label;
- private JScrollPane sp;
- boolean CheckPrime (int n) {
- boolean prime = true;
- for (int i = 2; i <= (int) Math.sqrt(n); i++) {
- if (n % i == 0) {
- prime = false;
- break;
- }
- }
- return prime;
- }
- Main() {
- frame.setTitle("Sample");
- // frame.setLocationRelativeTo(null);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setResizable(false);
- int n = 640;
- int root = (int) (Math.sqrt(n));
- mini_panels = new JPanel[n];
- main_panel.setLayout(new FlowLayout(FlowLayout.LEADING, 10, 10));
- for (int i = 0; i < n; i++) {
- mini_panels[i] = new JPanel();
- label = new JLabel(Integer.toString(i + 1));
- label.setFont(new Font("ARIAL", Font.PLAIN, 30));
- mini_panels[i].setLayout(new GridBagLayout());
- mini_panels[i].add(label);
- mini_panels[i].setPreferredSize(new Dimension(100, 100));
- if (i + 1 <= 1) {
- mini_panels[i].setBackground(Color.GRAY);
- } else if (CheckPrime(i + 1)) {
- mini_panels[i].setBackground(Color.GREEN);
- } else {
- mini_panels[i].setBackground(Color.YELLOW);
- }
- main_panel.add(mini_panels[i]);
- }
- int size = ((n % 8 == 0 ? n / 8 : n / 8 + 1)) * 110;
- main_panel.setPreferredSize(new Dimension(900, size));
- sp = new JScrollPane(main_panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
- frame.add(sp);
- frame.setVisible(true);
- frame.setSize(900, 500);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- }
- public static void main(String[] args) {
- new Main();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement