Advertisement
Shamel

Untitled

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