Advertisement
Guest User

Program.java

a guest
Dec 18th, 2024
40
0
2 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.99 KB | Source Code | 0 0
  1. import java.awt.Canvas;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Frame;
  5. import java.awt.Graphics;
  6. import java.awt.GraphicsDevice;
  7. import java.awt.GraphicsEnvironment;
  8. import java.awt.Rectangle;
  9. import java.awt.event.KeyEvent;
  10. import java.awt.event.KeyListener;
  11. import java.awt.event.WindowEvent;
  12. import java.awt.event.WindowListener;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.Random;
  16. import java.util.concurrent.ExecutionException;
  17. import java.util.concurrent.ExecutorService;
  18. import java.util.concurrent.Executors;
  19. import java.util.concurrent.Future;
  20.  
  21. public class Program extends Frame
  22. implements Runnable, WindowListener, KeyListener {
  23.     public static final int BLACK   = 0;
  24.     public static final int BLUE    = 1;
  25.     public static final int RED     = 2;
  26.     public static final int MAGENTA = 3;
  27.     public static final int GREEN   = 4;
  28.     public static final int CYAN    = 5;
  29.     public static final int YELLOW  = 6;
  30.     public static final int WHITE   = 7;
  31.  
  32.     private static record Ball(int x, int y, int color) {}
  33.  
  34.     private static class GameCanvas extends Canvas {
  35.         private final int width;
  36.         private final int height;
  37.         private final int[] points;
  38.         private final Color[] palette;
  39.         private int currentColor;
  40.  
  41.         public GameCanvas(int width, int height) {
  42.             this.width = width;
  43.             this.height = height;
  44.             this.points = new int[width * height];
  45.             this.palette = new Color[WHITE + 1];
  46.             this.currentColor = BLACK;
  47.             initPalette();
  48.             clear();
  49.         }
  50.  
  51.         private void initPalette() {
  52.             palette[BLACK]   = Color.BLACK;
  53.             palette[BLUE]    = new Color(0x00, 0x00, 0xAA);
  54.             palette[RED]     = new Color(0xAA, 0x00, 0x00);
  55.             palette[MAGENTA] = new Color(0xAA, 0x00, 0xAA);
  56.             palette[GREEN]   = new Color(0x00, 0xAA, 0x00);
  57.             palette[CYAN]    = new Color(0x00, 0xAA, 0xAA);
  58.             palette[YELLOW]  = new Color(0xAA, 0xAA, 0x00);
  59.             palette[WHITE]   = new Color(0xAA, 0xAA, 0xAA);
  60.         }
  61.  
  62.         @Override
  63.         public Dimension getPreferredSize() {
  64.             return new Dimension(width, height);
  65.         }
  66.  
  67.         @Override
  68.         public void paint(Graphics g) {
  69.             for (int i = 0; i < points.length; i++) {
  70.                 final int y = i / width;
  71.                 final int x = i % width;
  72.                 if (points[i] >= BLACK && points[i] <= WHITE) {
  73.                     g.setColor(palette[points[i]]);
  74.                 } else {
  75.                     g.setColor(Color.BLACK);
  76.                 }
  77.                 g.fillRect(x, y, 1, 1);
  78.             }
  79.         }
  80.  
  81.         @Override
  82.         public void update(Graphics g) {
  83.             paint(g);
  84.         }
  85.  
  86.         public int getWidth() {
  87.             return width;
  88.         }
  89.  
  90.         public int getHeight() {
  91.             return height;
  92.         }
  93.  
  94.         public void clear() {
  95.             for (int i = 0; i < points.length; i++) {
  96.                 points[i] = currentColor;
  97.             }
  98.         }
  99.  
  100.         public void plot(int x, int y) {
  101.            if (x >= 0 && x <= width && y >= 0 && y <= height) {
  102.                points[x + y * width] = currentColor;
  103.            }
  104.         }
  105.  
  106.         public void setColor(int color) {
  107.             if (color >= BLACK && color <= WHITE) {
  108.                 currentColor = color;
  109.             }
  110.         }
  111.     }
  112.  
  113.     private final String title;
  114.     private final GameCanvas gameCanvas;
  115.     private final Random random;
  116.     private volatile boolean hasRunned;
  117.  
  118.     private Program(String title, int width, int height) {
  119.         super(title);
  120.         addWindowListener(this);
  121.         addKeyListener(this);
  122.         this.title = title;
  123.         this.gameCanvas = new GameCanvas(width, height);
  124.         add(this.gameCanvas);
  125.         pack();
  126.         setResizable(false);
  127.         moveToCenter();
  128.         this.random = new Random();
  129.         this.hasRunned = false;
  130.     }
  131.  
  132.     private void moveToCenter() {
  133.         final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  134.         final GraphicsDevice[] devices = ge.getScreenDevices();
  135.         if (devices.length < 1) return;
  136.         final Rectangle bounds = devices[0].getDefaultConfiguration().getBounds();
  137.         final int windowX = bounds.width / 2 - this.getWidth() / 2 + bounds.x;
  138.         final int windowY = bounds.height / 2 - this.getHeight() / 2 + bounds.y;
  139.         this.setLocation(windowX, windowY);
  140.     }
  141.  
  142.     private void start() {
  143.         gameCanvas.setColor(BLACK);
  144.         gameCanvas.clear();
  145.     }
  146.  
  147.     private void stop() {
  148.         if (!hasRunned) return;
  149.         hasRunned = false;
  150.     }
  151.  
  152.     @Override
  153.     public void run() {
  154.         if (hasRunned) return;
  155.         hasRunned = true;
  156.         while (hasRunned) {
  157.             gameCanvas.repaint();
  158.         }
  159.         this.dispose();
  160.     }
  161.  
  162.     @Override
  163.     public void keyPressed(KeyEvent e) {}
  164.  
  165.     @Override
  166.     public void keyReleased(KeyEvent e) {
  167.         if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
  168.             stop();
  169.         }
  170.     }
  171.  
  172.     @Override
  173.     public void keyTyped(KeyEvent e) {}
  174.  
  175.     @Override
  176.     public void windowOpened(WindowEvent e) {
  177.         start();
  178.         gameCanvas.repaint();
  179.         (new Thread(this)).start();
  180.         final List<Ball> balls = new ArrayList<>();
  181.         balls.add(new Ball(gameCanvas.getWidth() / 2, gameCanvas.getHeight() / 2, WHITE));
  182.         balls.add(new Ball(1 * gameCanvas.getWidth() / 4, gameCanvas.getHeight() / 2, MAGENTA));
  183.         balls.add(new Ball(3 * gameCanvas.getWidth() / 4, gameCanvas.getHeight() / 2, GREEN));
  184.         balls.add(new Ball(3 * gameCanvas.getWidth() / 8, 1 * gameCanvas.getHeight() / 4, RED));
  185.         balls.add(new Ball(5 * gameCanvas.getWidth() / 8, 1 * gameCanvas.getHeight() / 4, YELLOW));
  186.         balls.add(new Ball(3 * gameCanvas.getWidth() / 8, 3 * gameCanvas.getHeight() / 4, BLUE));
  187.         balls.add(new Ball(5 * gameCanvas.getWidth() / 8, 3 * gameCanvas.getHeight() / 4, CYAN));
  188.         try (
  189.             ExecutorService executorSky = Executors.newVirtualThreadPerTaskExecutor();
  190.             ExecutorService executorBalls = Executors.newVirtualThreadPerTaskExecutor()
  191.         ) {
  192.             final List<Future<Double>> futuresSky = new ArrayList<>();
  193.             final List<Future<Double>> futuresBalls = new ArrayList<>();
  194.             futuresSky.add(executorSky.submit(() -> {
  195.                 synchronized (gameCanvas) {
  196.                     gameCanvas.setColor(BLACK);
  197.                     gameCanvas.clear();
  198.                 }
  199.                 for (int i = 0; i < 500; i++) {
  200.                     final int x = random.nextInt(gameCanvas.getWidth());
  201.                     final int y = random.nextInt(gameCanvas.getHeight());
  202.                     synchronized (gameCanvas) {
  203.                         gameCanvas.setColor(WHITE);
  204.                         gameCanvas.plot(x, y);
  205.                     }
  206.                 }
  207.                 return 0.0;
  208.             }));
  209.             double counterSky = 0.0;
  210.             for (Ball ball : balls) {
  211.                 final double resultSky = counterSky;
  212.                 counterSky += 1.0;
  213.                 futuresSky.add(executorSky.submit(() -> {
  214.                     final int centerX = ball.x();
  215.                     final int centerY = ball.y();
  216.                     final int ballColor = ball.color();
  217.                     synchronized (gameCanvas) {
  218.                         gameCanvas.setColor(ballColor);
  219.                         gameCanvas.plot(centerX, centerY);
  220.                     }
  221.                     double counterBalls = 0.0;
  222.                     for (double j = 0.01; j <= 0.9; j += 0.01) {
  223.                         final double radius = j;
  224.                         final double resultBalls = counterBalls;
  225.                         counterBalls += 1.0;
  226.                         futuresBalls.add(executorBalls.submit(() -> {
  227.                             final double xScale = 100.0;
  228.                             final double yScale = 100.0;
  229.                             for (int n = 0; n <= 360; n++) {
  230.                                 final double x = radius * Math.cos(n * Math.PI / 180.0);
  231.                                 final double y = radius * Math.sin(n * Math.PI / 180.0);
  232.                                 synchronized (gameCanvas) {
  233.                                     gameCanvas.setColor(ballColor);
  234.                                     gameCanvas.plot(centerX + (int)Math.floor(x * xScale), centerY - (int)Math.floor(y * yScale));
  235.                                 }
  236.                             }
  237.                             return resultBalls;
  238.                         }));
  239.                     }
  240.                     return resultSky;
  241.                 }));
  242.             }
  243.             double lastResult = 0.0;
  244.             // Draw the sky
  245.             executorSky.shutdown();
  246.             for (Future<Double> future : futuresSky) {
  247.                 lastResult = future.get();
  248.             }
  249.             gameCanvas.repaint();
  250.             // Pause before balls - not worked
  251.             Thread.sleep(5000);
  252.             // Draw the balls
  253.             executorBalls.shutdown();
  254.             for (Future<Double> future : futuresBalls) {
  255.                 lastResult = future.get();
  256.             }
  257.             System.out.println(lastResult);
  258.         } catch (InterruptedException | ExecutionException ex) {
  259.             this.stop();
  260.         }
  261.     }
  262.  
  263.     @Override
  264.     public void windowClosed(WindowEvent e) {}
  265.  
  266.     @Override
  267.     public void windowClosing(WindowEvent e) {
  268.         stop();
  269.     }
  270.  
  271.     @Override
  272.     public void windowActivated(WindowEvent e) {}
  273.  
  274.     @Override
  275.     public void windowDeactivated(WindowEvent e) {}
  276.  
  277.     @Override
  278.     public void windowIconified(WindowEvent e) {}
  279.  
  280.     @Override
  281.     public void windowDeiconified(WindowEvent e) {}
  282.  
  283.     public static void main(String[] args) {
  284.         (new Program("Game", 800, 600)).setVisible(true);
  285.     }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement