Advertisement
Shamel

Untitled

Oct 21st, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.41 KB | None | 0 0
  1. import java.awt.*;
  2. import java.util.LinkedList;
  3. import java.util.Queue;
  4. import java.util.Random;
  5. import javax.swing.*;
  6. public class Chp30Lab2st {
  7.     static int     speedOffset     = 5;
  8.     static int     speedRandomness = 7;
  9.     static boolean randomColors    = false;
  10.  
  11.     static int x  = 800;
  12.     static int y  = 600;
  13.     static int cx = x + 16;
  14.     static int cy = y + 39;
  15.  
  16.     public static void main(String args[]) {
  17.         int circleCount = 50;
  18.         int circleSize = 30;
  19.         try {
  20.             circleCount = Integer.parseInt(JOptionPane.showInputDialog("how many circles? default 50"));
  21.         } catch (NumberFormatException ignored) {
  22.         }
  23.         try {
  24.             circleSize = Integer.parseInt(JOptionPane.showInputDialog("how fat are your circles? default 30"));
  25.         } catch (NumberFormatException ignored) {
  26.         }
  27.         try {
  28.             speedOffset = Integer.parseInt(JOptionPane.showInputDialog("how fast are your circles? default 5"));
  29.         } catch (NumberFormatException ignored) {
  30.         }
  31.         try {
  32.             speedRandomness = Integer.parseInt(JOptionPane.showInputDialog("how random is the speed? default 7"));
  33.         } catch (NumberFormatException ignored) {
  34.         }
  35.         randomColors = Boolean.parseBoolean(JOptionPane.showInputDialog("random colors? default false"));
  36.  
  37.         GfxApp gfx = new GfxApp(circleCount, circleSize);
  38.         gfx.setBounds(0, 0, cx, cy);
  39.         gfx.setResizable(false);
  40.         gfx.setVisible(true);
  41.     }
  42. }
  43. class GfxApp extends Frame {
  44.     private static final int    timeDelay = 10;
  45.     private final        int    incX      = 5;
  46.     private final        int    incY      = 5;
  47.     private final        int    circleCount;
  48.     private final        int    diameter;
  49.     private final        Random r         = new Random();
  50.  
  51.     GfxApp(int count, int size) {
  52.         circleCount = count;
  53.         diameter = size;
  54.     }
  55.  
  56.     GfxApp() {
  57.         this(50, 30);
  58.     }
  59.  
  60.     public void paint(Graphics g) {
  61.         Coordinate current = new Coordinate(this,
  62.                                             incX, incY, diameter);
  63.         Queue<Circle> circles = new LinkedList<>();
  64.         circles.add(new Circle(diameter, current, getColor()));
  65.         for (int k = 0; k < 2000; k++) {
  66.             current.moveCircle();
  67.             current.hitEdge();
  68.             circles.add(new Circle(diameter, new Coordinate(current), getColor()));
  69.             if (circles.size() > circleCount)
  70.                 circles.remove().eraseCircle(g);
  71.             for (Circle c : circles) {
  72.                 c.drawCircle(g);
  73.             }
  74.             delay();
  75.         }
  76.     }
  77.  
  78.     private void delay() {
  79.         try {
  80.             Thread.sleep(timeDelay);
  81.         } catch (InterruptedException ignored) {
  82.         }
  83.     }
  84.  
  85.     private Color getColor() {
  86.         return Chp30Lab2st.randomColors ? new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256))
  87.                                         : Color.blue;
  88.     }
  89. }
  90. class Coordinate {
  91.     private final GfxApp  gfx;
  92.     private       int     x;
  93.     private       int     y;
  94.     private       int     velX;         // increment movement of X coordinate
  95.     private       int     velY;         // increment movement of Y coordinate
  96.     private       boolean xDir = true;  // flag to determine add/subtract of increment for X
  97.     private       boolean yDir = false; // flag to determine add/subtract of increment for Y
  98.     private       int     radius;
  99.  
  100.     Coordinate(Coordinate copy) {
  101.         gfx = copy.gfx;
  102.         x = copy.x;
  103.         y = copy.y;
  104.         velX = copy.velX;
  105.         velY = copy.velY;
  106.         xDir = copy.xDir;
  107.         yDir = copy.yDir;
  108.         radius = copy.radius;
  109.     }
  110.  
  111.     Coordinate(GfxApp ga, int vx, int vy, int d) {
  112.         gfx = ga;
  113.         this.x = ga.getBounds().width / 2;
  114.         this.y = ga.getBounds().height / 2;
  115.         velX = vx;
  116.         velY = vy;
  117.         radius = d / 2;
  118.     }
  119.  
  120.     void moveCircle() {
  121.         x += xDir ? velX : -velX;
  122.         y += yDir ? velY : -velY;
  123.     }
  124.  
  125.     private void newData() {
  126.         Random random = new Random();
  127.         velX = random.nextInt(Chp30Lab2st.speedRandomness) + Chp30Lab2st.speedOffset;
  128.         velY = random.nextInt(Chp30Lab2st.speedRandomness) + Chp30Lab2st.speedOffset;
  129.     }
  130.  
  131.     void hitEdge() {
  132.         xDir = outOfRangeX() ? x < gfx.getBounds().width / 2 : xDir;
  133.         yDir = outOfRangeY() ? y < gfx.getBounds().height / 2 : yDir;
  134.         if (outOfRangeX() || outOfRangeY())
  135.             newData();
  136.     }
  137.  
  138.     private boolean outOfRangeX() {
  139.         return x < radius || x > gfx.getBounds().width - radius;
  140.     }
  141.  
  142.     private boolean outOfRangeY() {
  143.         return y < radius + 30 || y > gfx.getBounds().height - radius;
  144.     }
  145.  
  146.     int x() {
  147.         return x;
  148.     }
  149.  
  150.     int y() {
  151.         return y;
  152.     }
  153. }
  154. class Circle {
  155.     private int        diameter;
  156.     private int        radius;
  157.     private Coordinate xy;
  158.     private Color      color;
  159.  
  160.     Circle(int s, Coordinate coord, Color c) {
  161.         diameter = s;
  162.         radius = s / 2;
  163.         xy = coord;
  164.         color = c;
  165.     }
  166.  
  167.     void drawCircle(Graphics g) {
  168.         g.setColor(color);
  169.         g.drawOval(xy.x() - radius, xy.y() - radius, diameter, diameter);
  170.     }
  171.  
  172.     void eraseCircle(Graphics g) {
  173.         g.setColor(Color.white);
  174.         g.drawOval(xy.x() - radius, xy.y() - radius, diameter, diameter);
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement