mirror of
https://github.com/opelly27/Java-N-Body-Simulation.git
synced 2026-05-20 01:37:36 +00:00
.
This commit is contained in:
+23
-13
@@ -14,18 +14,13 @@ public class Canvas {
|
||||
|
||||
public static void main(String[] args){
|
||||
Canvas test = new Canvas(500, 500, 255, 255, 255, 255);
|
||||
int radius = 100;
|
||||
int radius = 5;
|
||||
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();
|
||||
}
|
||||
test.drawCircle(x, y, radius, 255, 255, 255, 255);
|
||||
test.display();
|
||||
}
|
||||
|
||||
public Canvas(int height, int width, int r, int g, int b, int a){
|
||||
@@ -50,14 +45,17 @@ public class Canvas {
|
||||
}
|
||||
|
||||
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);
|
||||
if(x < 0 || y < 0 || y >= this.width || x >= this.height){
|
||||
return;
|
||||
}
|
||||
else{
|
||||
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){
|
||||
if(x > this.height -15 || x < 15 || y > this.width - 15 || y < 15){
|
||||
return;
|
||||
}
|
||||
|
||||
for(int sign = 0; sign < 4; sign ++){
|
||||
for(int offsetY = 0; offsetY < radius; offsetY ++){
|
||||
for(int offsetX = 0; offsetX < radius; offsetX ++ ){
|
||||
@@ -77,4 +75,16 @@ public class Canvas {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void drawCircle(int radius, int x, int y, int r, int g, int b, int a){
|
||||
double i, angle, x1, y1;
|
||||
|
||||
for(i = 0; i < 360; i += 0.1)
|
||||
{
|
||||
angle = i;
|
||||
x1 = radius * Math.cos(angle * Math.PI / 180);
|
||||
y1 = radius * Math.sin(angle * Math.PI / 180);
|
||||
this.setPixel((int)(x + x1), (int)(y + y1), 255,255,255,255);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user