Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.util.LinkedList;
- import java.util.Queue;
- import java.util.Random;
- import javax.swing.*;
- public class Chp30Lab2st {
- static int speedOffset = 5;
- static int speedRandomness = 7;
- static boolean randomColors = false;
- static int x = 800;
- static int y = 600;
- static int cx = x + 16;
- static int cy = y + 39;
- public static void main(String args[]) {
- int circleCount = 50;
- int circleSize = 30;
- try {
- circleCount = Integer.parseInt(JOptionPane.showInputDialog("how many circles? default 50"));
- } catch (NumberFormatException ignored) {
- }
- try {
- circleSize = Integer.parseInt(JOptionPane.showInputDialog("how fat are your circles? default 30"));
- } catch (NumberFormatException ignored) {
- }
- try {
- speedOffset = Integer.parseInt(JOptionPane.showInputDialog("how fast are your circles? default 5"));
- } catch (NumberFormatException ignored) {
- }
- try {
- speedRandomness = Integer.parseInt(JOptionPane.showInputDialog("how random is the speed? default 7"));
- } catch (NumberFormatException ignored) {
- }
- randomColors = Boolean.parseBoolean(JOptionPane.showInputDialog("random colors? default false"));
- GfxApp gfx = new GfxApp(circleCount, circleSize);
- gfx.setBounds(0, 0, cx, cy);
- gfx.setResizable(false);
- gfx.setVisible(true);
- }
- }
- class GfxApp extends Frame {
- private static final int timeDelay = 10;
- private final int incX = 5;
- private final int incY = 5;
- private final int circleCount;
- private final int diameter;
- private final Random r = new Random();
- GfxApp(int count, int size) {
- circleCount = count;
- diameter = size;
- }
- GfxApp() {
- this(50, 30);
- }
- public void paint(Graphics g) {
- Coordinate current = new Coordinate(this,
- incX, incY, diameter);
- Queue<Circle> circles = new LinkedList<>();
- circles.add(new Circle(diameter, current, getColor()));
- for (int k = 0; k < 2000; k++) {
- current.moveCircle();
- current.hitEdge();
- circles.add(new Circle(diameter, new Coordinate(current), getColor()));
- if (circles.size() > circleCount)
- circles.remove().eraseCircle(g);
- for (Circle c : circles) {
- c.drawCircle(g);
- }
- delay();
- }
- }
- private void delay() {
- try {
- Thread.sleep(timeDelay);
- } catch (InterruptedException ignored) {
- }
- }
- private Color getColor() {
- return Chp30Lab2st.randomColors ? new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256))
- : Color.blue;
- }
- }
- class Coordinate {
- private final GfxApp gfx;
- private int x;
- private int y;
- private int velX; // increment movement of X coordinate
- private int velY; // increment movement of Y coordinate
- private boolean xDir = true; // flag to determine add/subtract of increment for X
- private boolean yDir = false; // flag to determine add/subtract of increment for Y
- private int radius;
- Coordinate(Coordinate copy) {
- gfx = copy.gfx;
- x = copy.x;
- y = copy.y;
- velX = copy.velX;
- velY = copy.velY;
- xDir = copy.xDir;
- yDir = copy.yDir;
- radius = copy.radius;
- }
- Coordinate(GfxApp ga, int vx, int vy, int d) {
- gfx = ga;
- this.x = ga.getBounds().width / 2;
- this.y = ga.getBounds().height / 2;
- velX = vx;
- velY = vy;
- radius = d / 2;
- }
- void moveCircle() {
- x += xDir ? velX : -velX;
- y += yDir ? velY : -velY;
- }
- private void newData() {
- Random random = new Random();
- velX = random.nextInt(Chp30Lab2st.speedRandomness) + Chp30Lab2st.speedOffset;
- velY = random.nextInt(Chp30Lab2st.speedRandomness) + Chp30Lab2st.speedOffset;
- }
- void hitEdge() {
- xDir = outOfRangeX() ? x < gfx.getBounds().width / 2 : xDir;
- yDir = outOfRangeY() ? y < gfx.getBounds().height / 2 : yDir;
- if (outOfRangeX() || outOfRangeY())
- newData();
- }
- private boolean outOfRangeX() {
- return x < radius || x > gfx.getBounds().width - radius;
- }
- private boolean outOfRangeY() {
- return y < radius + 30 || y > gfx.getBounds().height - radius;
- }
- int x() {
- return x;
- }
- int y() {
- return y;
- }
- }
- class Circle {
- private int diameter;
- private int radius;
- private Coordinate xy;
- private Color color;
- Circle(int s, Coordinate coord, Color c) {
- diameter = s;
- radius = s / 2;
- xy = coord;
- color = c;
- }
- void drawCircle(Graphics g) {
- g.setColor(color);
- g.drawOval(xy.x() - radius, xy.y() - radius, diameter, diameter);
- }
- void eraseCircle(Graphics g) {
- g.setColor(Color.white);
- g.drawOval(xy.x() - radius, xy.y() - radius, diameter, diameter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement