Skip to content

Instantly share code, notes, and snippets.

@raunaqsingh2020
Last active January 17, 2020 16:57
Show Gist options
  • Save raunaqsingh2020/a989518f93f43c0a4e53d19caf0c5be4 to your computer and use it in GitHub Desktop.
Save raunaqsingh2020/a989518f93f43c0a4e53d19caf0c5be4 to your computer and use it in GitHub Desktop.
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.*;
import java.io.*;
import java.awt.Font;
import java.awt.*;
public class GUI {
private JFrame frame;
public GUI() {
frame = new JFrame("Bubbles Program");
frame.setSize(800, 800);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setPreferredSize(frame.getSize());
frame.add(new InnerProgram(frame.getSize()));
frame.pack();
frame.setVisible(true);
}
public static void main(String... argv) {
new GUI();
}
public static class InnerProgram extends JPanel implements Runnable, MouseListener {
ArrayList<String> names = new ArrayList<String>();
private Thread animator;
Dimension d;
String str = "";
//Bubbles b = null;
private Queue bubs = new Queue<Bubbles>(25);
public InnerProgram (Dimension dimension) {
setSize(dimension);
setPreferredSize(dimension);
addMouseListener(this);
addKeyListener(new TAdapter());
setFocusable(true);
d = getSize();
//for animating the screen - you won't need to edit
if (animator == null) {
animator = new Thread(this);
animator.start();
}
setDoubleBuffered(true);
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.black);
g2.fillRect(0, 0,(int)d.getWidth() , (int)d.getHeight());
Color co = new Color(255,255,255);
g2.setColor(co);
int fontSize = 10;
g2.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
g2.drawString("String " + str,20,40);
if(bubs.size()!=0){
for(int i = 0; i < bubs.nItems; i++){
Bubbles b = (Bubbles)bubs.get(i);
b.x += b.xSpeed;
b.y += b.ySpeed;
g2.setColor(b.color);
g2.fillOval(b.x,b.y,b.rad,b.rad);
int max = 255;
int min = 0;
if(b.x>800-b.rad-3 || b.x<0+3){
b.xSpeed *= -1;
b.color = new Color((int)(Math.random() * ((max - min) + 1)) + min,(int)(Math.random() * ((max - min) + 1)) + min,(int)(Math.random() * ((max - min) + 1)) + min);
System.out.println("test1");
}
if(b.y<0+3 || b.y>800-b.rad-3){
System.out.println("test3");
b.ySpeed *= -1;
b.color = new Color((int)(Math.random() * ((max - min) + 1)) + min,(int)(Math.random() * ((max - min) + 1)) + min,(int)(Math.random() * ((max - min) + 1)) + min);
}
for(int j = i+1; j < bubs.nItems; j++){
Bubbles temp = (Bubbles)bubs.get(j);
int dx = temp.x + temp.rad/2 - b.x - b.rad/2;
int dy = temp.y + temp.rad/2 - b.y - b.rad/2;
int dist = temp.rad/2 + b.rad/2;
if(dx * dx + dy * dy <= dist * dist){
/*
if(temp.rad >= b.rad){
bubs.removeAt(i);
temp.rad += b.rad;
}else{
bubs.removeAt(j);
b.rad += temp.rad;
}
*/
//Color k = temp.color;
//temp.color=b.color;
/*
b.color=k;
b.ySpeed *= -1;
b.xSpeed *= -1;
temp.ySpeed *= -1;
temp.xSpeed *= -1;
*/
if(temp.rad <= b.rad){
Bubbles newbub = new Bubbles((int)(Math.random() * 740) + 30,(int)(Math.random() * 740) + 30);
newbub.xSpeed = (int)(Math.random() * 30) - 15;
newbub.ySpeed = (int)(Math.random() * 30) - 15;
newbub.color= b.color;
bubs.insert(newbub);
}else{
Bubbles newbub = new Bubbles((int)(Math.random() * 740) + 30,(int)(Math.random() * 740) + 30);
newbub.xSpeed = (int)(Math.random() * 30) - 15;
newbub.ySpeed = (int)(Math.random() * 30) - 15;
newbub.color= temp.color;
bubs.insert(newbub);
}
}
}
/*
if(i == bubs.nItems-1){
for(int j = 0; j < bubs.nItems-1; j++){
Bubbles temp = (Bubbles)bubs.get(j);
int dx = temp.x + temp.rad/2 - b.x - b.rad/2;
int dy = temp.y + temp.rad/2 - b.y - b.rad/2;
int dist = temp.rad/2 + b.rad/2;
if(dx * dx + dy * dy <= dist * dist){
Color k = temp.color;
temp.color=b.color;
b.color=k;
}
}
}
*/
}
}
}
public int random (int a, int b){
int max=a;
int min=b;
int random=(int)(Math.random() * (max - min) + min);
return random;
}
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
str = x + " " + y;
Bubbles b = new Bubbles(x,y);
b.xSpeed = (int)(Math.random() * 30) - 15;
b.ySpeed = (int)(Math.random() * 30) - 15;
bubs.insert(b);
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
private class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e) {
int keyr = e.getKeyCode();
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
// String c = KeyEvent.getKeyText(e.getKeyCode());
// c = Character.toString((char) key);
}
}//end of adapter
public void run() {
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
int animationDelay = 37;
long time = System.currentTimeMillis();
while (true) {// infinite loop
// spriteManager.update();
repaint();
try {
time += animationDelay;
Thread.sleep(Math.max(0, time - System.currentTimeMillis()));
} catch (InterruptedException e) {
System.out.println(e);
} // end catch
} // end while loop
}// end of run
}//end of class
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment