-
Notifications
You must be signed in to change notification settings - Fork 0
Render Scene with Java swing
tecno-master edited this page Aug 4, 2024
·
3 revisions
In order to display your Scene you need to Render it. In this example we will be using swing but the same principle is applicable for other graphic tools.
Step 1: Create a custom panel
In the constructor we will pass a Scene object. Remember a Scene stores all Physic Objects and Constraints.
public class VerletPanel extends JPanel {
public VerletPanel(Scene scene) {
this.scene = scene;
}
}Step 2: Override paintComponent
Override paintComponents and call the super.paintMethod(g) method
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
}Step 3: Loop all Spheres
Here we use the invokeSpheres method in order
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
container.invokeSpheres(sphere -> {
g.fillOval((int) (sphere.getX() - sphere.getRadius()), (int) (sphere.getY() - sphere.getRadius()), (int) (sphere.getRadius()*2), (int) (sphere.getRadius()*2);
}));
}Remember that this simple code now uses the JPanel coordinate system. If you spawn a Sphere at (0, 0) it will show up at the top left coordinate. You can also implement a way to render constraints.