Hello, i'm pretty new to java... anyway i've been trying to make a particle system so far i've made one class that can simulate one particle with simple collisions but i decided i'd need to move into using multiple classes. I have one class for simulation/updating and one for rendering how can I plug a variables constantly updating value into the x and y position values of a fillRect in my graphical class? heres the code if it helps... I might be going about this all wrong as i'm newish to java:
Simulation(Sorta) class:
import java.awt.Component; import javax.swing.*; public class Particle { public static int Xpos = 50; //makes a int of the x position public static int Ypos = 50; //makes a int of the y position public float Xvel = 10; // makes a float of the x velocity public float Yvel = -20; // makes a float of the y velocity public int gravity = 2; //sets gravity to 2 public static void main(String[] args) { JFrame f = new JFrame("Fluid"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Renderer r = new Renderer(); f.add(r); f.setSize(500,500); f.setVisible(true); f.setResizable(false); } public void simulate(){ Yvel += gravity; Ypos += Yvel; if (Ypos >= 190 || Ypos <= 0) { Yvel = -Yvel/2; } Xpos += Xvel; if (Xpos >= 200 || Xpos <= 0) { Xvel = -Xvel/2; } Yvel--; } }
Rendering Class:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Renderer extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); this.setBackground(Color.WHITE); g.setColor(Color.CYAN); g.fillRect(Particle.Xpos, Particle.Ypos, 3, 3); } }
Any help is appreciated also and help with making a emitter or making multiple particles would be very much appreciated as that is the next thing I will start working on and undoubtedly have trouble with.