What ever happened to good old drawPixel(x,y,col)?
I'm having a hard time finding out how to make graphics that move on the screen. Basically, just two round ovals that move together based on the effects of gravity computations.
I have a CelestObject class extending JPanel which defines parameters for a ball (sun and planet) and includes a paint(g) and repaint(g) method. Then there is a Core class with main(args) that runs a for loop, which performs calculations and alters the coordinates of the two balls and instructs to repaint the two images.
When I run the program, only the planet shows up. It does appear animated, but is very choppy and sporatic. The sun doesn't show up at all, but it does still have an effect on the gravity of the planet.
I'd like for both ovals to show up and for the movement between them be a lot smoother. I've tried so many places online and texts, but they're all either way too simple, or way over my head. Please tell me what I'm doing wrong!
Here's the code (2 classes):
import java.awt.*; import javax.swing.*; public class Core { public static double deltaX; // to be used in grav calculations public static double deltaY; public static double deltaD; public static double fGravSP; public static void main(String[] args) { // create the two ovals CelestObject sun = new CelestObject(250, 250); sun.mass = 10000; sun.radius = 60; sun.vVelocity.size = 0.0; sun.vVelocity.setAngleD(0.0); CelestObject planet = new CelestObject(450, 450); planet.mass = 100; planet.radius = 6; planet.vVelocity.size = 0.0075; planet.vVelocity.setAngleD(-35.0); // make a new frame to hold the graphics JFrame f = new JFrame("Orbits"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(640, 960); f.setVisible(true); // add the two ovals to the frame (which should trigger paint(g)) f.add(sun); f.add(planet); for (int i = 1; i < 500; i++) { // run through the loop // do a lot of calculations (tested with dialog boxes and all seem to give the right answers when tested so no problems assumed here) ... ... f.setVisible(true); // if i move this, the planet doesn't appear at all! f.repaint(); } } } NEXT CLASS: import java.awt.Graphics; import javax.swing.JPanel; public class CelestObject extends JPanel { public static final double bigG = 6.67384 * (Math.pow(10, -11)); // gravitational constant public double mass; // in kgs public double radius; // in pixels (1 pixel = 1 mm, 1000 pixels = 1 m) for now public double x, y; // as pixels; (0,0) is top left corner; converted to int when drawn public Vector vVelocity; // the speed and direction of the object (speed in pixels/sec, direction in degrees) public Vector vAccel; // the acceleration and direction of the object public CelestObject(int xNew, int yNew) { x = (double)xNew; y = (double)yNew; mass = 0; radius = 1; Vector tempV = new Vector(0, 0); vVelocity = tempV; vAccel = tempV; } //public class CelestObjectImage extends JPanel { // failed attempt at sub-class public void paint(Graphics g) { super.paint(g); g.setColor(Color.BLACK); g.drawOval((int)x, (int)y, (int)radius, (int)radius); } public void repaint(Graphics g) { super.paint(g); g.drawOval((int)x, (int)y, (int)radius, (int)radius); } }
Any help or insights anyone can give me would be greatly appreciated!