diff --git a/Body.java b/Body.java index 1068e6d..3d9a405 100644 --- a/Body.java +++ b/Body.java @@ -2,7 +2,7 @@ public class Body { - public static final double G = 6.673e-11; + public static final double G = 6.673e-5; private double mass; private double px; private double py; @@ -50,33 +50,35 @@ public class Body { double m1 = this.getMass(); double m2 = otherBody.getMass(); - double force = G * ((m1 * m2) / distance); + double force = G * ((m1 * m2) / (distance * distance)); return force; } + public double getAngle(Body otherBody){ + + double delta_x = otherBody.getXpos() - this.getXpos(); + double delta_y = otherBody.getYpos() - this.getYpos(); + double theta_radians = Math.atan2(delta_y, delta_x); + + return theta_radians; + } + public double getXforce(Body otherBody){ - double distance = otherBody.getXpos() - this.getXpos(); - double m1 = this.getMass(); - double m2 = otherBody.getMass(); - - double force = G * ((m1 * m2) / distance); - - return force; - + double scalarForce = this.getForceScalar(otherBody); + double angleDegrees = this.getAngle(otherBody); + double returnDouble = scalarForce * Math.cos(angleDegrees); + return returnDouble; } public double getYforce(Body otherBody){ - - double distance = otherBody.getYpos() - this.getYpos(); - double m1 = this.getMass(); - double m2 = otherBody.getMass(); - double force = G * ((m1 * m2) / distance); - - return force; + double scalarForce = this.getForceScalar(otherBody); + double angleDegrees = this.getAngle(otherBody); + double returnDouble = scalarForce * Math.sin(angleDegrees); + return returnDouble; } public void calcNetForce(Body[] bodies){ @@ -94,12 +96,15 @@ public class Body { } public void updateVelocity(double timeslice){ - // TODO: Implament updateVelocity - + + this.vx = this.vx + ((this.fx / this.mass) * timeslice); + this.vy = this.vy + ((this.fy / this.mass) * timeslice); } public void updatePosition(double timeslice){ - // TODO: Implament updatePosition + + this.px = this.px + (this.vx * timeslice); + this.py = this.py + (this.vy * timeslice); } diff --git a/Canvas.java b/Canvas.java new file mode 100644 index 0000000..9ba17f7 --- /dev/null +++ b/Canvas.java @@ -0,0 +1,77 @@ +// A wrapper class for the buffered image object that adds functionality to +// draw shapes. +import java.awt.image.*; +import javax.swing.*; +import java.awt.*; + + +public class Canvas { + int height; + int width; + BufferedImage image; + private static JFrame frame; + private static JLabel label; + + public static void main(String[] args){ + Canvas test = new Canvas(500, 500, 255, 255, 255, 255); + int radius = 100; + int x = 250; + int y = 250; + int a = 255; + + for(int i = 0; i < 50000; i++){ + int r = (int) (Math.random() * 256); + int g = (int) (Math.random() * 256); + int b = (int) (Math.random() * 256); + test.drawSquare(radius, x, y, r, g, b, a); + test.display(); + } + } + + public Canvas(int height, int width, int r, int g, int b, int a){ + this.height = height; + this.width = width; + this.image = new BufferedImage(height, width, BufferedImage.TYPE_INT_ARGB); + } + + public void display(){ + if(frame==null){ + frame=new JFrame(); + frame.setTitle("stained_image"); + frame.setSize(image.getWidth(), image.getHeight()); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + label=new JLabel(); + label.setIcon(new ImageIcon(image)); + frame.getContentPane().add(label,BorderLayout.CENTER); + frame.setLocationRelativeTo(null); + frame.pack(); + frame.setVisible(true); + }else label.setIcon(new ImageIcon(this.image)); + } + + public void setPixel(int x, int y, int r, int g, int b, int a){ + int p = (a<<24) | (r<<16) | (g<<8) | b; + this.image.setRGB(x, y, p); + } + + public void drawSquare(int radius, int x, int y, int r, int g, int b, int a){ + for(int sign = 0; sign < 4; sign ++){ + for(int offsetY = 0; offsetY < radius; offsetY ++){ + for(int offsetX = 0; offsetX < radius; offsetX ++ ){ + int xPoint = offsetX; + int yPoint = offsetY; + switch(sign){ + case 0: xPoint = xPoint * - 1; + break; + case 1: yPoint = yPoint * -1; + break; + case 2: xPoint = xPoint * -1; + yPoint = yPoint * -1; + break; + } + this.setPixel(x + xPoint, y + yPoint, r, g, b, a); + } + } + } + } +} diff --git a/ImageProcessing.java b/ImageProcessing.java new file mode 100644 index 0000000..837cc57 --- /dev/null +++ b/ImageProcessing.java @@ -0,0 +1,41 @@ +import java.awt.image.*; +import javax.swing.*; +import java.awt.*; + +public class ImageProcessing { + private static JFrame frame; + private static JLabel label; + + + public static void main(String[] args){ + + } + + + public static void display(BufferedImage image){ + if(frame==null){ + frame=new JFrame(); + frame.setTitle("stained_image"); + frame.setSize(image.getWidth(), image.getHeight()); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + label=new JLabel(); + label.setIcon(new ImageIcon(image)); + frame.getContentPane().add(label,BorderLayout.CENTER); + frame.setLocationRelativeTo(null); + frame.pack(); + frame.setVisible(true); + }else label.setIcon(new ImageIcon(image)); + } + + public static BufferedImage resize(BufferedImage img, int newW, int newH) { + Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH); + BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB); + + Graphics2D g2d = dimg.createGraphics(); + g2d.drawImage(tmp, 0, 0, null); + g2d.dispose(); + + return dimg; + } + +} diff --git a/Space.java b/Space.java index 2271346..ece1e01 100644 --- a/Space.java +++ b/Space.java @@ -1,17 +1,21 @@ +import java.awt.image.*; + public class Space { - Body[] bodies; - double height; - double width; + Body[] bodies; // List of body objects that make up the simulation + double height; // The height of the spacial region being simulated + double width; // The width of the spacial region being simulated + double timeslice; // The time in sections between each tick of the simulation - public Space(Body[] bodies, double height, double width){ + public Space(Body[] bodies, int height, int width, double timeslice){ this.bodies = bodies; this.height = height; this.width = width; + this.timeslice = timeslice; } - public void calculateForces(){ + public void updateForces(){ //double runningTotal = 0; for(int i = 0; i < this.bodies.length; i ++){ @@ -19,32 +23,103 @@ public class Space { } } + public void updateVelocities(){ + for(int i = 0; i < this.bodies.length; i ++){ + this.bodies[i].updateVelocity(this.timeslice); + } + } + + public void updatePositions(){ + for(int i = 0; i < this.bodies.length; i ++){ + this.bodies[i].updatePosition(this.timeslice); + } + } + + public void draw(){ + + Canvas canvas = new Canvas( (int) this.height, (int) this.width, 255,255,255,255); + for(int i = 0; i < this.bodies.length; i ++){ + // switch(i){ + // case 0: canvas.drawSquare(5, (int) this.bodies[i].getXpos(), (int) this.bodies[i].getYpos(), 0, 0, 0, 255); + // case 1: canvas.drawSquare(5, (int) this.bodies[i].getXpos(), (int) this.bodies[i].getYpos(), 255, 0, 0, 255); + // case 2: canvas.drawSquare(5, (int) this.bodies[i].getXpos(), (int) this.bodies[i].getYpos(), 0, 0, 255, 255); + // case 3: canvas.drawSquare(5, (int) this.bodies[i].getXpos(), (int) this.bodies[i].getYpos(), 0, 255, 0, 255); + // } + canvas.drawSquare(5, (int) this.bodies[i].getXpos(), (int) this.bodies[i].getYpos(), 0, 0, 0, 255); + + } + canvas.display(); + } + public static void main(String args[]){ - Body testBody = new Body(15000, 0, 0, 10, 10); - Body testBody2 = new Body(27000, 2, 1, 10, 10); - Body testBody3 = new Body(29530, 4, 2, 10, 10); - Body testBody4 = new Body(29530000, -4, 4, 10, 10); - Body[] bodies = new Body[4]; + Body testBody = new Body(2000000, 375, 375, 0, 0); + Body testBody2 = new Body(2, 500, 400, 0, -.5); + Body testBody3 = new Body(2, 200, 400, 0, .5); + Body testBody4 = new Body(2, 200, 500, .5, 0); + Body testBody5 = new Body(2, 200, 200, -.5, 0); + Body[] bodies = new Body[5]; bodies[0] = testBody; bodies[1] = testBody2; bodies[2] = testBody3; bodies[3] = testBody4; + bodies[4] = testBody5; - Space space = new Space(bodies, 500, 500); - space.calculateForces(); + Space space = new Space(bodies, 750, 750, .02); - System.out.println(space.bodies[0].getNetXforce()); - System.out.println(space.bodies[0].getNetYforce()); + while(true){ + space.updateForces(); + space.updateVelocities(); + space.updatePositions(); + //System.out.println(space); + space.draw(); + } + + } - System.out.println(space.bodies[1].getNetXforce()); - System.out.println(space.bodies[1].getNetYforce()); + @Override + public String toString() { + String returnString = String.format("Width: " + this.width + " Height: " + this.height + "\n\n\n"); + for(int i = 0; i < this.bodies.length; i ++){ + int b = i +1; + returnString += String.format("Body #" + b + "\n\n"); + returnString += String.format("Position: X = " + this.bodies[i].getXpos() + " "); + returnString += String.format("Position: Y = " + this.bodies[i].getYpos() + "\n"); + returnString += String.format("Velosity: X = " + this.bodies[i].getXvelo() + " "); + returnString += String.format("Velosity: Y = " + this.bodies[i].getYvelo() + "\n\n"); - System.out.println(space.bodies[2].getNetXforce()); - System.out.println(space.bodies[2].getNetYforce()); + } - System.out.println(space.bodies[3].getNetXforce()); - System.out.println(space.bodies[3].getNetYforce()); + return returnString; } } + + + + +// public BufferedImage generateImage(){ + + // BufferedImage img = new BufferedImage((int)this.height, (int)this.width, BufferedImage.TYPE_INT_ARGB); + + // for(int y = 0; y < this.height; y ++){ + // for (int x = 0; x < this.width; x ++){ + // for (int body = 0; body < this.bodies.length; body ++){ + // if((int)this.bodies[body].getXpos() == x){ + // if((int) this.bodies[body].getYpos() == y){ + // int a = 255; + // int r = 0; + // int g = 0; + // int b = 0; + + // int p = (a<<24) | (r<<16) | (g<<8) | b; + + // img.setRGB(x, y, p); + // } + // } + // } + // } + // } + + // return img; + // } diff --git a/Test.java b/Test.java new file mode 100644 index 0000000..2383c8e --- /dev/null +++ b/Test.java @@ -0,0 +1,59 @@ +import java.awt.*; +import java.awt.image.*; +import javax.swing.*; + +public class Test { + public static void main(String[] args){ + + for(int i = 0; i < 30; i ++){ + display(resize(getRandomFrame(),500,500)); + } + + + } + + private static JFrame frame; + private static JLabel label; + public static void display(BufferedImage image){ + if(frame==null){ + frame=new JFrame(); + frame.setTitle("stained_image"); + frame.setSize(image.getWidth(), image.getHeight()); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + label=new JLabel(); + label.setIcon(new ImageIcon(image)); + frame.getContentPane().add(label,BorderLayout.CENTER); + frame.setLocationRelativeTo(null); + frame.pack(); + frame.setVisible(true); + }else label.setIcon(new ImageIcon(image)); + } + + public static BufferedImage getRandomFrame(){ + BufferedImage img = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB); + + for(int y = 0; y < 50; y ++){ + for(int x = 0; x < 50; x ++){ + int a = 255; + int r = (int) (Math.random() * 256); + int g = (int) (Math.random() * 256); + int b = (int) (Math.random() * 256); + + int p = (a<<24) | (r<<16) | (g<<8) | b; + + img.setRGB(x, y, p); + } + } + return img; + } + public static BufferedImage resize(BufferedImage img, int newW, int newH) { + Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH); + BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB); + + Graphics2D g2d = dimg.createGraphics(); + g2d.drawImage(tmp, 0, 0, null); + g2d.dispose(); + + return dimg; + } +}