Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Frame;
- import java.awt.Graphics;
- import java.awt.GraphicsDevice;
- import java.awt.GraphicsEnvironment;
- import java.awt.Rectangle;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.event.WindowEvent;
- import java.awt.event.WindowListener;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Future;
- public class Program extends Frame
- implements Runnable, WindowListener, KeyListener {
- public static final int BLACK = 0;
- public static final int BLUE = 1;
- public static final int RED = 2;
- public static final int MAGENTA = 3;
- public static final int GREEN = 4;
- public static final int CYAN = 5;
- public static final int YELLOW = 6;
- public static final int WHITE = 7;
- private static record Ball(int x, int y, int color) {}
- private static class GameCanvas extends Canvas {
- private final int width;
- private final int height;
- private final int[] points;
- private final Color[] palette;
- private int currentColor;
- public GameCanvas(int width, int height) {
- this.width = width;
- this.height = height;
- this.points = new int[width * height];
- this.palette = new Color[WHITE + 1];
- this.currentColor = BLACK;
- initPalette();
- clear();
- }
- private void initPalette() {
- palette[BLACK] = Color.BLACK;
- palette[BLUE] = new Color(0x00, 0x00, 0xAA);
- palette[RED] = new Color(0xAA, 0x00, 0x00);
- palette[MAGENTA] = new Color(0xAA, 0x00, 0xAA);
- palette[GREEN] = new Color(0x00, 0xAA, 0x00);
- palette[CYAN] = new Color(0x00, 0xAA, 0xAA);
- palette[YELLOW] = new Color(0xAA, 0xAA, 0x00);
- palette[WHITE] = new Color(0xAA, 0xAA, 0xAA);
- }
- @Override
- public Dimension getPreferredSize() {
- return new Dimension(width, height);
- }
- @Override
- public void paint(Graphics g) {
- for (int i = 0; i < points.length; i++) {
- final int y = i / width;
- final int x = i % width;
- if (points[i] >= BLACK && points[i] <= WHITE) {
- g.setColor(palette[points[i]]);
- } else {
- g.setColor(Color.BLACK);
- }
- g.fillRect(x, y, 1, 1);
- }
- }
- @Override
- public void update(Graphics g) {
- paint(g);
- }
- public int getWidth() {
- return width;
- }
- public int getHeight() {
- return height;
- }
- public void clear() {
- for (int i = 0; i < points.length; i++) {
- points[i] = currentColor;
- }
- }
- public void plot(int x, int y) {
- if (x >= 0 && x <= width && y >= 0 && y <= height) {
- points[x + y * width] = currentColor;
- }
- }
- public void setColor(int color) {
- if (color >= BLACK && color <= WHITE) {
- currentColor = color;
- }
- }
- }
- private final String title;
- private final GameCanvas gameCanvas;
- private final Random random;
- private volatile boolean hasRunned;
- private Program(String title, int width, int height) {
- super(title);
- addWindowListener(this);
- addKeyListener(this);
- this.title = title;
- this.gameCanvas = new GameCanvas(width, height);
- add(this.gameCanvas);
- pack();
- setResizable(false);
- moveToCenter();
- this.random = new Random();
- this.hasRunned = false;
- }
- private void moveToCenter() {
- final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
- final GraphicsDevice[] devices = ge.getScreenDevices();
- if (devices.length < 1) return;
- final Rectangle bounds = devices[0].getDefaultConfiguration().getBounds();
- final int windowX = bounds.width / 2 - this.getWidth() / 2 + bounds.x;
- final int windowY = bounds.height / 2 - this.getHeight() / 2 + bounds.y;
- this.setLocation(windowX, windowY);
- }
- private void start() {
- gameCanvas.setColor(BLACK);
- gameCanvas.clear();
- }
- private void stop() {
- if (!hasRunned) return;
- hasRunned = false;
- }
- @Override
- public void run() {
- if (hasRunned) return;
- hasRunned = true;
- while (hasRunned) {
- gameCanvas.repaint();
- }
- this.dispose();
- }
- @Override
- public void keyPressed(KeyEvent e) {}
- @Override
- public void keyReleased(KeyEvent e) {
- if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
- stop();
- }
- }
- @Override
- public void keyTyped(KeyEvent e) {}
- @Override
- public void windowOpened(WindowEvent e) {
- start();
- gameCanvas.repaint();
- (new Thread(this)).start();
- final List<Ball> balls = new ArrayList<>();
- balls.add(new Ball(gameCanvas.getWidth() / 2, gameCanvas.getHeight() / 2, WHITE));
- balls.add(new Ball(1 * gameCanvas.getWidth() / 4, gameCanvas.getHeight() / 2, MAGENTA));
- balls.add(new Ball(3 * gameCanvas.getWidth() / 4, gameCanvas.getHeight() / 2, GREEN));
- balls.add(new Ball(3 * gameCanvas.getWidth() / 8, 1 * gameCanvas.getHeight() / 4, RED));
- balls.add(new Ball(5 * gameCanvas.getWidth() / 8, 1 * gameCanvas.getHeight() / 4, YELLOW));
- balls.add(new Ball(3 * gameCanvas.getWidth() / 8, 3 * gameCanvas.getHeight() / 4, BLUE));
- balls.add(new Ball(5 * gameCanvas.getWidth() / 8, 3 * gameCanvas.getHeight() / 4, CYAN));
- try (
- ExecutorService executorSky = Executors.newVirtualThreadPerTaskExecutor();
- ExecutorService executorBalls = Executors.newVirtualThreadPerTaskExecutor()
- ) {
- final List<Future<Double>> futuresSky = new ArrayList<>();
- final List<Future<Double>> futuresBalls = new ArrayList<>();
- futuresSky.add(executorSky.submit(() -> {
- synchronized (gameCanvas) {
- gameCanvas.setColor(BLACK);
- gameCanvas.clear();
- }
- for (int i = 0; i < 500; i++) {
- final int x = random.nextInt(gameCanvas.getWidth());
- final int y = random.nextInt(gameCanvas.getHeight());
- synchronized (gameCanvas) {
- gameCanvas.setColor(WHITE);
- gameCanvas.plot(x, y);
- }
- }
- return 0.0;
- }));
- double counterSky = 0.0;
- for (Ball ball : balls) {
- final double resultSky = counterSky;
- counterSky += 1.0;
- futuresSky.add(executorSky.submit(() -> {
- final int centerX = ball.x();
- final int centerY = ball.y();
- final int ballColor = ball.color();
- synchronized (gameCanvas) {
- gameCanvas.setColor(ballColor);
- gameCanvas.plot(centerX, centerY);
- }
- double counterBalls = 0.0;
- for (double j = 0.01; j <= 0.9; j += 0.01) {
- final double radius = j;
- final double resultBalls = counterBalls;
- counterBalls += 1.0;
- futuresBalls.add(executorBalls.submit(() -> {
- final double xScale = 100.0;
- final double yScale = 100.0;
- for (int n = 0; n <= 360; n++) {
- final double x = radius * Math.cos(n * Math.PI / 180.0);
- final double y = radius * Math.sin(n * Math.PI / 180.0);
- synchronized (gameCanvas) {
- gameCanvas.setColor(ballColor);
- gameCanvas.plot(centerX + (int)Math.floor(x * xScale), centerY - (int)Math.floor(y * yScale));
- }
- }
- return resultBalls;
- }));
- }
- return resultSky;
- }));
- }
- double lastResult = 0.0;
- // Draw the sky
- executorSky.shutdown();
- for (Future<Double> future : futuresSky) {
- lastResult = future.get();
- }
- gameCanvas.repaint();
- // Pause before balls - not worked
- Thread.sleep(5000);
- // Draw the balls
- executorBalls.shutdown();
- for (Future<Double> future : futuresBalls) {
- lastResult = future.get();
- }
- System.out.println(lastResult);
- } catch (InterruptedException | ExecutionException ex) {
- this.stop();
- }
- }
- @Override
- public void windowClosed(WindowEvent e) {}
- @Override
- public void windowClosing(WindowEvent e) {
- stop();
- }
- @Override
- public void windowActivated(WindowEvent e) {}
- @Override
- public void windowDeactivated(WindowEvent e) {}
- @Override
- public void windowIconified(WindowEvent e) {}
- @Override
- public void windowDeiconified(WindowEvent e) {}
- public static void main(String[] args) {
- (new Program("Game", 800, 600)).setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement