Last active
October 28, 2018 15:24
-
-
Save ijcook/082313aac37ff3c6ee98fb49dbbc67bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.util.*; | |
import java.awt.event.*; | |
import javax.swing.event.MouseInputAdapter; | |
@SuppressWarnings("serial") | |
public class Main extends JPanel implements ActionListener { | |
public static final double XSPEED = .0000001; | |
public static final double YSPEED = .0000001; | |
int startX, startY, endX, endY, adding=0; | |
boolean dragging=false, | |
still=false, //determines whether the planet moves via gravity or not. | |
vShow=false, | |
aShow=false, | |
pause=false; | |
double nextMass=100000; | |
ArrayList<Orbital> planets = new ArrayList<Orbital>(); | |
public Main() { | |
//initialization of listeners | |
MooseMover mouse=new MooseMover(); | |
this.addMouseListener(mouse); | |
this.addMouseMotionListener(mouse); | |
KeyListener listener = new MyKeyListener(); | |
addKeyListener(listener); | |
setFocusable(true); | |
} | |
public class MyKeyListener implements KeyListener { | |
@Override | |
public void keyTyped(KeyEvent e) { | |
} | |
@Override | |
public void keyPressed(KeyEvent e) { | |
//System.out.println(e.getKeyCode()); | |
if(e.getKeyCode()==32/*space key*/){ | |
still=!still; | |
} | |
if(e.getKeyCode()==10/*enter*/){ | |
nextMass=Integer.parseInt(JOptionPane.showInputDialog("What is the next Mass? (Standard is 100000)")); | |
} | |
if(e.getKeyCode()==65/*a*/){ | |
aShow=!aShow; | |
} | |
if(e.getKeyCode()==86/*v*/){ | |
pause=!pause; | |
} | |
} | |
@Override | |
public void keyReleased(KeyEvent e) { | |
} | |
} | |
public class MooseMover extends MouseInputAdapter{ | |
public void mousePressed(MouseEvent e){ | |
startX=e.getX(); | |
startY=e.getY(); | |
endX=e.getX(); | |
endY=e.getY(); | |
} | |
//calculates initial velocity via drag length | |
public void mouseDragged(MouseEvent e){ | |
endX=e.getX(); | |
endY=e.getY(); | |
} | |
public void mouseReleased(MouseEvent e){ | |
endX=e.getX(); | |
endY=e.getY(); | |
planets.add(new Orbital(nextMass,startX,startY,(endX-startX)/10,(endY-startY)/10,still)); | |
} | |
} | |
@Override | |
public void paintComponent(Graphics g){ | |
if(!pause) { | |
g.setColor(Color.WHITE); | |
g.fillRect(0,0,this.getWidth(),this.getHeight()); | |
g.setColor(Color.RED); | |
//instructions | |
g.drawString("(Press Enter key to change) Weight: "+nextMass,0,10); | |
g.drawString("(Press Space key to change) Stable: "+still,0,20); | |
g.drawString("xAcceleration(Blue) yAcceleration(Green)",0,30); | |
g.drawString("Press a to show/hide acceleration vectors",0,40); | |
g.drawString("Press v to pause/unpause",0,50); | |
g.drawLine((int)startX,(int)startY,(int)endX,(int)endY); | |
for(int i=0;i<planets.size();i++){ | |
double xAccel=0,yAccel=0; | |
for(int j=0;j<planets.size();j++){ | |
//calculates planet gravitational pull magnitude and direction | |
double theta = Math.atan((planets.get(j).y-planets.get(i).y)/(planets.get(j).x-planets.get(i).x)), | |
magnitude=6.67*Math.pow(10,-6)*planets.get(i).mass*planets.get(j).mass/Math.sqrt(Math.pow(planets.get(i).x-planets.get(j).x,2)+Math.pow(planets.get(i).y-planets.get(j).y,2)); | |
try{ | |
if(planets.get(i).x<planets.get(j).x){ | |
xAccel+=Math.cos(theta)*magnitude/planets.get(i).mass; | |
} | |
}catch(Exception e){} | |
try{ | |
if(planets.get(i).x>planets.get(j).x){ | |
xAccel-=Math.cos(theta)*magnitude/planets.get(i).mass; | |
} | |
}catch(Exception e){} | |
try{ | |
if(planets.get(i).x>planets.get(j).x){ | |
if(planets.get(i).y<planets.get(j).y){ | |
yAccel-=Math.sin(theta)*magnitude/planets.get(i).mass; | |
} | |
} | |
if(planets.get(i).x<planets.get(j).x){ | |
if(planets.get(i).y<planets.get(j).y){ | |
yAccel+=Math.sin(theta)*magnitude/planets.get(i).mass; | |
} | |
} | |
}catch(Exception e){} | |
try{ | |
if(planets.get(i).x<planets.get(j).x){ | |
if(planets.get(i).y>planets.get(j).y){ | |
yAccel+=Math.sin(theta)*magnitude/planets.get(i).mass; | |
} | |
} | |
if(planets.get(i).x>planets.get(j).x){ | |
if(planets.get(i).y>planets.get(j).y){ | |
yAccel-=Math.sin(theta)*magnitude/planets.get(i).mass; | |
} | |
} | |
}catch(Exception e){} | |
} | |
//changes planet velocity | |
planets.get(i).acceleration(xAccel,yAccel); | |
} | |
for(Orbital a:planets){ | |
a.move(g); | |
g.fillOval((int)a.x-5,(int)a.y-5,10,10); | |
} | |
//x-axis acceleration vector visibility | |
g.setColor(Color.BLUE); | |
if(aShow) | |
for(Orbital a:planets){ | |
g.drawLine((int)a.x,(int)a.y,(int)(a.x+(a.vx-a.vxt)*4000),(int)a.y); | |
} | |
//y-axis acceleration vector visibility | |
g.setColor(Color.GREEN); | |
if(aShow) | |
for(Orbital a:planets){ | |
g.drawLine((int)a.x,(int)a.y,(int)a.x,(int)(a.y+(a.vy-a.vyt)*4000)); | |
} | |
} | |
repaint(); | |
} | |
@Override | |
public void actionPerformed(ActionEvent e){ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.awt.*; | |
public class Orbital{ | |
double x,y,vx,vy,mass,vxt,vyt; | |
boolean stationary; | |
ArrayList<Double> xPoint= new ArrayList<Double>(); | |
ArrayList<Double> yPoint= new ArrayList<Double>(); | |
public Orbital(double Mass, int StartX, int StartY, int VelocityX, int VelocityY,boolean still){ | |
mass=Mass; | |
x=StartX; | |
y=StartY; | |
vx=VelocityX; | |
vy=VelocityY; | |
vxt=vx; | |
vyt=vy; | |
stationary=still; | |
} | |
public void acceleration(double ax, double ay){ | |
//changes the velocity according to the acceleration vectors | |
vxt=vx; | |
vyt=vy; | |
vx+=ax; | |
vy+=ay; | |
} | |
public void move(Graphics g){ | |
//point movement due to velocity | |
if(!stationary){ | |
xPoint.add(x); | |
if(xPoint.size()>75){ | |
xPoint.remove(0); | |
} | |
yPoint.add(y); | |
if(yPoint.size()>75){ | |
yPoint.remove(0); | |
} | |
x+=vx; | |
y+=vy; | |
for(int i=0; i<xPoint.size()-1;i++){ | |
g.drawLine((int)(double)xPoint.get(i),(int)(double)yPoint.get(i),(int)(double)xPoint.get(i+1),(int)(double)yPoint.get(i+1)); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
public class ProgramStart{ | |
public static void main(String[] args){ | |
JFrame Movie = new JFrame(); | |
Container MovieFrame = Movie.getContentPane(); | |
Main One = new Main(); | |
Movie.setTitle("FUN"); | |
Movie.setSize(676,718); | |
Movie.setLocation(0,0); | |
One.setBackground(Color.CYAN); | |
MovieFrame.add(One); | |
Movie.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment