Skip to content

Instantly share code, notes, and snippets.

@ra4king
Last active February 1, 2017 08:49
Show Gist options
  • Save ra4king/56ec4ca74e4a3b3fba09eb2bb04f37d8 to your computer and use it in GitHub Desktop.
Save ra4king/56ec4ca74e4a3b3fba09eb2bb04f37d8 to your computer and use it in GitHub Desktop.
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
/**
* @author Roi Atalla
*/
public class CircleTest {
public static void main(String[] args) {
JFrame frame = new JFrame("Circle Test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Canvas canvas = new Canvas();
canvas.setSize(800, 600);
frame.add(canvas);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
Graphics2D graphics = (Graphics2D)canvas.getGraphics();
int width = canvas.getWidth();
int height = canvas.getHeight();
Circle staticCircle = new Circle(100, 300, 50);
Circle movingCircle = new Circle(400, 200, 60);
double velX = -75;
double velY = 40;
long lastTime = System.nanoTime();
long lastNow = System.nanoTime();
int frameCount = 0;
while(true) {
long now = System.nanoTime();
if(now - lastTime >= 1e9) {
lastTime += 1e9;
System.out.println(frameCount);
frameCount = 0;
}
frameCount++;
graphics.clearRect(0, 0, width, height);
double delta = (now - lastNow) / 1e9;
double deltaX = velX * delta;
double deltaY = velY * delta;
movingCircle.x += deltaX;
movingCircle.y += deltaY;
double radiuses = movingCircle.radius + staticCircle.radius;
if(movingCircle.distanceFrom(staticCircle) < radiuses) {
Vector2 staticToMoving = new Vector2(movingCircle.x - staticCircle.x, movingCircle.y - staticCircle.y);
staticToMoving.normalize();
movingCircle.x = staticCircle.x + staticToMoving.x * radiuses;
movingCircle.y = staticCircle.y + staticToMoving.y * radiuses;
}
graphics.setColor(new Color(1, 0, 0, 0.5f));
graphics.fillOval((int)(staticCircle.x - staticCircle.radius), (int)(staticCircle.y - staticCircle.radius),
(int)staticCircle.radius * 2, (int)(staticCircle.radius * 2));
graphics.setColor(new Color(0, 0, 1, 0.5f));
graphics.fillOval((int)(movingCircle.x - movingCircle.radius), (int)(movingCircle.y - movingCircle.radius),
(int)movingCircle.radius * 2, (int)(movingCircle.radius * 2));
long diff = System.nanoTime() - now;
try {
long sleep = 10 * 1000000 - diff;
if(sleep > 0) {
long millis = sleep / 1000000;
int nanos = (int)(sleep % 1000000);
Thread.sleep(millis, nanos);
}
} catch(Exception exc) {
exc.printStackTrace();
}
lastNow = now;
}
}
static class Circle {
double x, y, radius;
Circle(double x, double y, double radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
double distanceFrom(Circle other) {
double diffX = this.x - other.x;
double diffY = this.y - other.y;
return Math.sqrt(diffX * diffX + diffY * diffY);
}
}
static class Vector2 {
double x, y;
Vector2(double x, double y) {
this.x = x;
this.y = y;
}
void normalize() {
double length = length();
x /= length;
y /= length;
}
double length() {
return Math.sqrt(x * x + y * y);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment