Frames and Gravity
in
Share your Work
•
3 years ago
here is the program i have been working on it experiments on windows shaped as circles with gravity also clicking inverts gravity
- PFont font;
- import java.awt.event.*;
- //import javax.swing.*;
- import java.awt.*;
- int pl = int(random(20,200));
- Planet[] planets = new Planet[pl];
- import com.sun.awt.AWTUtilities;
- import java.awt.GraphicsDevice.*; // PC only
- import java.awt.Shape;
- import java.awt.Robot;
- import java.awt.AWTException;
- import java.awt.event.InputEvent;
- import java.awt.MouseInfo;
- import java.awt.geom.*;
- //import javax.swing.*;
- Robot robby;
- int d =500;
- int y =d/2;
- int x = d/2;
- boolean press = false;
- public void init(){
- // to make a frame not displayable, you can
- // use frame.removeNotify()
- frame.removeNotify();
- frame.setUndecorated(true);
- // addNotify, here i am not sure if you have
- // to add notify again.
- //frame.addNotify();
- // frame.removeNotify();
- super.init();
- }
- void setup() {
- try
- {
- robby = new Robot();
- }
- catch (AWTException e)
- {
- println("Robot class not supported by your system!");
- exit();
- }
- size(d,d);
- background(255);
- //frame.setUndecorated(true);
- frame.setSize(int(d), int(d));
- noStroke();
- frame.setAlwaysOnTop(true);
- frame.setLocation(0,0);
- AWTUtilities.setWindowOpaque(frame, false);
- AWTUtilities.setWindowOpacity(frame, 0.01f);
- //frame.setBackground(new Color(0,0,0,255));
- //frame.setVisible(true);
- Shape shape = null;
- shape = new Ellipse2D.Float(0, 0, d, d);
- AWTUtilities.setWindowShape(frame, shape);
- for (int i = 0; i< planets.length; i++ ) {
- float x = random(1,1000);
- float y = random(1,1000);
- int xy = int (x*1);
- int yx= int (y*1);
- int co = int(random(1,255));
- int col = int(random(1,255));
- int colo = int(random(1,255));
- float dimeter = random(6,40);
- float velx1 = random(0,5);
- float vely1 = random(0,5);
- // float vely1 = -velx1;
- // if (i == planets.length-1){
- // xy = screen.width/2;
- // yx = screen.height/2;
- // dimeter = 250;
- // velx1 = 0;
- // vely1 = 0;
- // co =255;
- // col =255;
- // colo = 0;
- // }
- planets[i] = new Planet(dimeter,xy,yx,co,col,colo,i,velx1,vely1);
- }
- }
- void draw() {
- if(mousePressed == true){
- press = true;
- }
- else{
- press = false;
- }
- background(0);
- x = int(MouseInfo.getPointerInfo().getLocation().x)-d/2;
- y =int(MouseInfo.getPointerInfo().getLocation().y)-d/2;
- frame.setLocation(x,y);
- for (int i = 0; i < planets.length; i++ ) {
- // planets[i].display(i);
- planets[i].update();
- // planets[i].checkEdges();
- planets[i].daw(i);
- }
- //println(MouseInfo.getPointerInfo().getLocation() + " | ");
- }
- class Planet extends PApplet implements MouseListener {
- Frame frame;
- final static int SIZE = 20;
- int t;
- float xcc = 0;
- float ycc = 0;
- float xpos;
- float ypos;
- float diameter;
- int sunypos = 500;
- int sunxpos = 500;
- float gravmax = .1;
- int col1;
- int col2;
- int col3;
- int curplanet;
- int num;
- int i = 1;
- //public void init(){
- // addMouseListener(this);
- //}
- Planet(float diameter_,float xpos_,float ypos_,int co1,int co2,int co3,int planet,float velx,float vely) {
- diameter = diameter_;
- xpos = (xpos_);
- ypos = (ypos_);
- col1 = co1;
- col2 =co2;
- col3 =co3;
- curplanet = planet;//planet number
- xcc = velx;
- ycc= vely;
- frame = new Frame();
- // frame.getContentPane().add(new MyComponent());
- frame.setUndecorated(true);
- frame.setSize(int(diameter), int(diameter));
- frame.setLocation(floor(xpos), floor(ypos));
- frame.setAlwaysOnTop(true);
- frame.setBackground(new java.awt.Color(col1, col2, col3,255));//due to 1.1
- frame.setVisible(true);
- Shape shape = null;
- shape = new Ellipse2D.Float(0, 0, diameter, diameter);
- AWTUtilities.setWindowShape(frame, shape);
- // AWTUtilities.setWindowOpaque(this.frame, false);
- //AWTUtilities.setWindowOpacity(this.frame, 1f);
- //println(AWTUtilities.getWindowOpacity(frame));
- ypos = ypos-diameter/2;
- xpos = xpos-diameter/2;
- }
- void update() {
- // float area = (((diameter/2)*(diameter/2))*3.1415);
- for (int i = 0;i < planets.length; i++) {
- if(i == curplanet){
- continue;
- }
- float area1 = ((((planets[i].diameter/2))*(planets[i].diameter/2)*(planets[i].diameter/2))*3.1415);
- float disty = (planets[i].ypos-ypos);
- float distx = (planets[i].xpos-xpos);
- float distfull = sqrt(distx*distx+disty*disty);
- if (distfull < (planets[i].diameter/2)){
- continue;
- }
- float acc = (.01) *(area1/(distfull*distfull));
- // print(" dist x="+distx);
- // print(" dist y="+disty);
- // print(" full="+distfull);
- // println(" acc="+acc);
- //.0000000000667*distx
- xcc = xcc + (acc*distx/distfull);
- ycc = ycc + (acc*disty/distfull);
- }
- //if(curplanet!=planets.length-1){
- if(press ==true){
- float area1 = 20000;
- int x = int(MouseInfo.getPointerInfo().getLocation().x)-int(diameter/2);
- int y =int(MouseInfo.getPointerInfo().getLocation().y)-int(diameter/2);
- float disty = (y-ypos);
- float distx = (x-xpos);
- float distfull = sqrt(distx*distx+disty*disty);
- float acc = (-2) *(area1/(distfull*distfull));
- // print(" dist x="+distx);
- // print(" dist y="+disty);
- // print(" full="+distfull);
- // println(" acc="+acc);
- //.0000000000667*distx
- xcc = xcc + (acc*distx/distfull);
- ycc = ycc + (acc*disty/distfull);
- }
- // public void mousePressed(MouseEvent me){
- // float area1 = 20000;
- // int x = int(MouseInfo.getPointerInfo().getLocation().x)-int(diameter/2);
- //int y =int(MouseInfo.getPointerInfo().getLocation().y)-int(diameter/2);
- // float disty = (y-ypos);
- // float distx = (x-xpos);
- // float distfull = sqrt(distx*distx+disty*disty);
- //
- // float acc = (-2) *(area1/(distfull*distfull));
- // // print(" dist x="+distx);
- // // print(" dist y="+disty);
- // // print(" full="+distfull);
- // // println(" acc="+acc);
- // //.0000000000667*distx
- // xcc = xcc + (acc*distx/distfull);
- // ycc = ycc + (acc*disty/distfull);
- //
- // }
- xcc=xcc*.999999;
- ycc=ycc*.999999;
- xpos = xpos + xcc;
- ypos = ypos + ycc;
- //}
- /*if(curplanet== 0 && xpos < 625 && xpos > 375 && ypos < 625 && ypos > 375){
- background(255);
- fill (0);
- font = loadFont("BookAntiqua-Bold-150.vlw");
- textFont(font);
- text("THE END", 150, 500);
- delay(10000);
- if(1==1){
- exit();
- }
- }
- if(curplanet== 0 && xpos < 50 && xpos > 50 && ypos < 50 && ypos > 50){
- background(255);
- fill (0);
- font = loadFont("BookAntiqua-Bold-150.vlw");
- textFont(font);
- text("WIN", 150, 500);
- delay(10000);
- if(1==1){
- exit();
- }
- }*/
- }
- void daw(int ti) {
- // frame.setBackground(new java.awt.Color(col1, col2, col3,255));
- t = ti+1;
- if (xpos < 0) {
- xpos = 0;
- xcc *= -1;
- }
- if (xpos > screen.width - diameter) {
- xpos = screen.width - diameter;
- xcc *= -1;
- }
- if (ypos < 0) {
- ypos = 0;
- ycc *= -1;
- }
- if (ypos > screen.height - diameter) {
- ypos = screen.height - diameter;
- ycc *= -1;
- }
- //frame.setColor(new java.awt.Color(255,0,0));
- //stroke(255);
- //line(0,0,100,100);
- frame.setLocation(floor(xpos),floor(ypos));
- }
- // public void paint(Graphics g) {
- // Graphics2D g2 = (Graphics2D) g;
- // g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- // RenderingHints.VALUE_ANTIALIAS_ON);
- //
- // g2.setPaint(Color.white);
- // int x = 5;
- // int y = 7;
- //
- // g2.draw(new Line2D.Double(0, 0, 200, 200));
- // g2.drawString("Line2D", x, 250);
- //
- // }
- }
- void mouseReleased(){
- frame.setLocation(-500,-500);
- robby.mousePress(InputEvent.BUTTON1_MASK);
- delay(30);
- robby.mouseRelease(InputEvent.BUTTON1_MASK);
- frame.setLocation(x,y);
- }
1