Baffled by simple graphics
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):
Code Java:
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! :)
Re: Baffled by simple graphics
Some things I see that are bad:
You should override the paintComponent method not the paint
You should NOT override repaint
Your code does not compile. I get the following errors:
Code :
MovingObjects.java:30: cannot find symbol
symbol : variable size
location: class java.util.Vector
sun.vVelocity.size = 0.0;
^
MovingObjects.java:31: cannot find symbol
symbol : method setAngleD(double)
location: class java.util.Vector
sun.vVelocity.setAngleD(0.0);
^
MovingObjects.java:37: cannot find symbol
symbol : variable size
location: class java.util.Vector
planet.vVelocity.size = 0.0075;
^
MovingObjects.java:38: cannot find symbol
symbol : method setAngleD(double)
location: class java.util.Vector
planet.vVelocity.setAngleD(-35.0);
Re: Baffled by simple graphics
Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
How would a drawPixel() method help you? The logic would be the same, just more annoying to draw. You can use any of the following if you really think that drawing pixel by pixel would help you:
fillOval(x, y, 1, 1);
fillRectangle(x, y, 1, 1);
drawLine(x, y, x, y);
You could even use a BufferedImage and use setRGB().
But yeah, I'm mostly kidding with those. Read through the tutorial I linked, throw together an SSCCE, and we'll be happy to help. Painting is really simple once you get used to it.
Re: Baffled by simple graphics
Thank you both! I'll check out these hints and see what I can do :)
@norm - I didn't include my non-graphics class (Vector) so it won't compile for you. Tried to keep the code minimal. But I realize now it's probably easier to help if you can see (and copy) everything
@Kevin - thanks for the link! I've found it hard finding something similar online. I'd never dream of using drawPixel - just meant as a joke. I used to find graphics like this easy (think BASIC or old Turing coding) and would find the calculations the hard part. Thats reversed now; learning graphics in a OOP context isn't as straight-forward as my mind would like, but time for me to get with the times!
Re: Baffled by simple graphics
Quote:
my non-graphics class (Vector)
Suggestion: Don't use an existing Java class's name for your class.
A suggested approach:
Create a JPanel and override the paintComponent method. Have that method call a method in your celestial object classes that do NOT extend JPanel with a reference to the Graphics object. They can then draw themselves how they want.
Add the JPanel to the JFrame. It will be where the objects draw themselves.
In another method possibly on a Timer thread, compute the next location for the bodies and call repaint which will cause a call paintComponent which will call your objects' methods to draw themselves at the new locations.
Re: Baffled by simple graphics
Awesome, I got it to work finally and looks perfect! Had to combine basically all the resources you both gave me, but definitely learned a lot.
I didn't even realize there was already a Vector class haha
Thanks again!
Re: Baffled by simple graphics
Quote:
Originally Posted by
macantas
Awesome, I got it to work finally and looks perfect! Had to combine basically all the resources you both gave me, but definitely learned a lot.
I didn't even realize there was already a Vector class haha
Thanks again!
I'm glad you got it working. For what it's worth, it's pretty much universally accepted that ArrayLists should be used instead of Vectors.
Re: Baffled by simple graphics
The OPs Vector class was a custom class ala a physics vector: direction and speed
Re: Baffled by simple graphics
Quote:
Originally Posted by
Norm
The OPs Vector class was a custom class ala a physics vector: direction and speed
Ohh snap, good catch. Yeah, ArrayList probably wouldn't work for you too well then. :p
Although, this does point out why you shouldn't use pre-existing names for your classes! Sure, that's what I meant to do the whole time...