Skip to content

Instantly share code, notes, and snippets.

@shah-smit
Last active October 19, 2017 12:34
Show Gist options
  • Save shah-smit/da74f1b5fe9b48732387a1dc8cac5cc1 to your computer and use it in GitHub Desktop.
Save shah-smit/da74f1b5fe9b48732387a1dc8cac5cc1 to your computer and use it in GitHub Desktop.
package referencetypes;
import java.awt.Graphics;
import java.awt.Toolkit
import java.util.Random;
import goo.Goo;
public class GooDrops extends Goo {
private Drop[] drops;
private int numDrops, maxSize = 9, maxVel = 9;
private Random random;
private GreenDrop greenDrop;
public GooDrops(int w, int h, int nd) {
super(w, h);
numDrops = nd;
drops = new Drop[numDrops];
random = new Random(1962);
for (int i = 0; i < numDrops; i++) {
int xpos = random.nextInt(w);
int ypos = random.nextInt(h);
int xvel = 1 + random.nextInt(maxVel);
int yvel = 1 + random.nextInt(maxVel);
int size = 1 + random.nextInt(maxSize);
drops[i] = makeDrop(xpos, ypos, xvel, yvel, size);
}
greenDrop = new GreenDrop(10,10,10,10,30);
}
public Drop makeDrop(int xpos, int ypos,int xvel,int yvel,
int size){
return new Drop(xpos, ypos, xvel, yvel, size);
}
public void draw(Graphics g) {
greenDrop.move(getWidth(), getHeight());
greenDrop.draw(g);
for (int i = 0; i < numDrops; i++) {
drops[i].move(getWidth(), getHeight());
drops[i].draw(g);
if(collision(greenDrop, drops[i])){
Toolkit.getDefaultToolKit().beep();
System.out.println("happen");
}
else{
System.out.println("not happen");
}
}
}
private boolean collision(Drop a, Drop b){
System.out.println(a.getX());
System.out.println(b.getX());
if((a.getX() == b.getX()) && (a.getY() == b.getY())){
return true;
}
else{
return false;
}
}
public Drop[] getDrops(){
return drops;
}
public Random getRandom(){
return random;
}
public static void main(String[] args) {
int width = 800;
int height = 500;
int numDrops = 5;
GooDrops gd = new GooDrops(width, height, numDrops);
gd.smooth();
gd.go();
}
}
package referencetypes;
import java.awt.Color;
import java.awt.Graphics;
public class GreenDrop extends Drop{
Color color = Color.GREEN;
public GreenDrop(int x, int y, int vx, int vy, int sz) {
super(x, y, vx, vy, sz);
}
public void move(int width,int height){
xpos = xpos + xvel;
ypos = ypos + yvel;
if ((ypos) > height || ypos < 0) {
yvel *= -1;
}
if((xpos) > width || xpos < 0){
xvel *= -1;
}
}
public void draw(Graphics g){
g.setColor(color);
g.fillOval(xpos, ypos, size, size);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment