Advertisement
OrangoMango

Primes

Nov 22nd, 2023 (edited)
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.49 KB | Science | 0 0
  1. import javafx.application.Application;
  2. import javafx.stage.Stage;
  3. import javafx.scene.Scene;
  4. import javafx.scene.layout.StackPane;
  5. import javafx.scene.canvas.*;
  6. import javafx.scene.paint.Color;
  7. import javafx.animation.AnimationTimer;
  8. import javafx.scene.input.KeyCode;
  9.  
  10. import java.util.*;
  11.  
  12. public class Primes extends Application{
  13.     private static final int WIDTH = 800;
  14.     private static final int HEIGHT = 600;
  15.     private static final int FPS = 40;
  16.  
  17.     private int squareSize = 5;
  18.     private int cameraX, cameraY;
  19.     private HashMap<KeyCode, Boolean> keys = new HashMap<>();
  20.  
  21.     private int frames, fps;
  22.  
  23.     @Override
  24.     public void start(Stage stage){
  25.         StackPane pane = new StackPane();
  26.         Canvas canvas = new Canvas(WIDTH, HEIGHT);
  27.         pane.getChildren().add(canvas);
  28.         GraphicsContext gc = canvas.getGraphicsContext2D();
  29.  
  30.         canvas.setOnScroll(e -> {
  31.             if (e.getDeltaY() > 0){
  32.                 this.squareSize += 1;
  33.             } else if (e.getDeltaY() < 0){
  34.                 this.squareSize -= 1;
  35.             }
  36.  
  37.             this.squareSize = Math.min(150, Math.max(this.squareSize, 1));
  38.         });
  39.  
  40.         canvas.setFocusTraversable(true);
  41.         canvas.setOnKeyPressed(e -> this.keys.put(e.getCode(), true));
  42.         canvas.setOnKeyReleased(e -> this.keys.put(e.getCode(), false));
  43.  
  44.         AnimationTimer timer = new AnimationTimer(){
  45.             @Override
  46.             public void handle(long time){
  47.                 update(gc);
  48.                 Primes.this.frames++;
  49.                 stage.setTitle("Prime numbers - FPS:"+Primes.this.fps); // Set FPS title
  50.             }
  51.         };
  52.         timer.start();
  53.  
  54.         Thread fpsCounter = new Thread(() -> {
  55.             while (true){
  56.                 try {
  57.                     this.fps = this.frames;
  58.                     this.frames = 0;
  59.                     Thread.sleep(1000);
  60.                 } catch (InterruptedException ex){
  61.                     ex.printStackTrace();
  62.                 }
  63.             }
  64.         });
  65.         fpsCounter.setDaemon(true);
  66.         fpsCounter.start();
  67.  
  68.         Scene scene = new Scene(pane, WIDTH, HEIGHT);
  69.         stage.setScene(scene);
  70.         stage.setResizable(false);
  71.         stage.show();
  72.     }
  73.  
  74.     private void update(GraphicsContext gc){
  75.         gc.clearRect(0, 0, WIDTH, HEIGHT);
  76.         gc.setFill(Color.BLACK);
  77.         gc.fillRect(0, 0, WIDTH, HEIGHT);
  78.  
  79.         final int cameraSpeed = 4;
  80.         if (this.keys.getOrDefault(KeyCode.W, false)){
  81.             this.cameraY -= cameraSpeed;
  82.             this.keys.put(KeyCode.W, false);
  83.         } else if (this.keys.getOrDefault(KeyCode.A, false)){
  84.             this.cameraX -= cameraSpeed;
  85.             this.keys.put(KeyCode.A, false);
  86.         } else if (this.keys.getOrDefault(KeyCode.S, false)){
  87.             this.cameraY += cameraSpeed;
  88.             this.keys.put(KeyCode.S, false);
  89.         } else if (this.keys.getOrDefault(KeyCode.D, false)){
  90.             this.cameraX += cameraSpeed;
  91.             this.keys.put(KeyCode.D, false);
  92.         } else if (this.keys.getOrDefault(KeyCode.ESCAPE, false)){
  93.             System.exit(0);
  94.         }
  95.        
  96.         for (int i = 0; i < WIDTH; i += squareSize){
  97.             for (int j = 0; j < HEIGHT; j += squareSize){
  98.                 int xpos = i/squareSize+this.cameraX;
  99.                 int ypos = j/squareSize+this.cameraY;
  100.                 if (xpos < 0 || ypos < 0) continue;
  101.                 int id = xpos ^ ypos; //xpos+ypos*WIDTH;
  102.                 if (isPrime(id)){
  103.                     for (int x = 0; x < squareSize; x++){
  104.                         for (int y = 0; y < squareSize; y++){
  105.                             gc.getPixelWriter().setColor(i+x, j+y, getColor(id));
  106.                         }
  107.                     }
  108.                 }
  109.             }
  110.         }
  111.     }
  112.  
  113.     private static Color getColor(int prime){
  114.         Random random = new Random(prime);
  115.         return Color.color(random.nextDouble(), random.nextDouble(), random.nextDouble());
  116.     }
  117.  
  118.     private static boolean isPrime(int num){
  119.         for (int i = 2; i <= Math.sqrt(num); i++){
  120.             if (num % i == 0){
  121.                 return false;
  122.             }
  123.         }
  124.  
  125.         return true;
  126.     }
  127.  
  128.     public static void main(String[] args){
  129.         launch(args);
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement