/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package colorfulbouncingcircle; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; import javax.swing.*; import java.awt.*; import java.util.ArrayList; public class ColorfulBouncingCircle { public double setPlayingFieldSize( int x, int y){ double v; v=2+x+y; return v; } /**setPlayingFieldSize (double newWidth, double newHeight){} * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here PlayingField playingField = new PlayingField(); playingField.run(); } } class PlayingField { private ColorfulBouncingCircle objA = new ColorfulBouncingCircle ( ); public void b1 ( ) { // Call to method a1. This works. objA.setPlayingFieldSize (640,480); } JFrame frame = new JFrame("InputDialog Example #1"); // prompt the user to enter their name String mywidth = JOptionPane.showInputDialog(frame, "What Window width?"); String myheight = JOptionPane.showInputDialog(frame, "What window height?"); int w = Integer.parseInt(mywidth); int h = Integer.parseInt(myheight); private JFrame mainFrame; private DrawPanel drawPanel; private java.util.List allCircles; private int windowWidth = w; private int windowHeight = h; private String windowLabel = "ColourfulBouncingCircles"; void run() { allCircles = new ArrayList<>(); /* Generate allCircles */ for (int i = 0; i < 50; i++) { Ball ball = new Ball( /* Random positions from 0 to windowWidth or windowHeight */ (int) Math.floor(Math.random() * windowWidth), (int) Math.floor(Math.random() * windowHeight), /* Random size from 10 to 30 */ (int) Math.floor(Math.random() * 20) + 10, /* Random RGB colors*/ new Color( (int) Math.floor((Math.random() * 256)), (int) Math.floor((Math.random() * 256)), (int) Math.floor((Math.random() * 256)) ), /* Set velocities */ (int) Math.floor((Math.random() * 10) - 5), (int) Math.floor((Math.random() * 10) - 5) ); allCircles.add(ball); } /* Initialize program */ mainFrame = new JFrame(); drawPanel = new DrawPanel(); mainFrame.getContentPane().add(drawPanel); mainFrame.setTitle(windowLabel); mainFrame.setSize(windowWidth, windowHeight); mainFrame.setVisible(true); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); while (true) { for (Ball b: allCircles) { b.update(); } /* Give Swing 10 milliseconds to see the update! */ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } mainFrame.repaint(); } } class DrawPanel extends JPanel { @Override public void paintComponent(Graphics graphics) { super.paintComponent(graphics); for (Ball b: allCircles) { b.draw(graphics); } } } class Ball { private int posX, posY, size; private Color color; private int vx = 5; private int vy = 5; public Ball(int posX, int posY, int size, Color color, int vx, int vy) { this.posX = posX; this.posY = posY; this.size = size; this.color = color; this.vx = vx; this.vy = vy; } void update() { if (posX > mainFrame.getWidth() || posX < 0) { vx *= -1; } if (posY > mainFrame.getHeight() || posY < 0) { vy *= -1; } if (posX > mainFrame.getWidth()) { posX = mainFrame.getWidth(); } if (posX < 0) { posX = 0; } if (posY > mainFrame.getHeight()) { posY = mainFrame.getHeight(); } if (posY < 0) { posY = 0; } this.posX += vx; this.posY += vy; } void draw(Graphics g) { g.setColor(color); g.fillOval(posX, posY, size, size); } } }