Sorry if this question has already been asked but i'm having trouble understanding the existing answers and need to see it related to my code. (I'm new to java)
Also sorry if this similar code is already posted - I tried doing this with two panels on one frame and couldnt get it working so im trying this method.
When my button blackTrail is pressed on frame2, it changes the color of my projectile on frame1 to black.
Here's my code so far:
GUI Form Class ( Menu with buttons etc)
public class GUIForm1 extends JFrame{
//Initialise components
private JPanel MainPanel;
private JPanel MenuPanel;
private JButton whiteTrail;
private JButton blackTrail;
private JButton redTrail;
private JButton blueTrail;
private JButton greenTrail;
private JButton purpleTrail;
//irrelevant components
public GUIForm1() {
JFrame frame2 = new JFrame("Projectile Motion - Menu");
frame2.setContentPane(MainPanel);
frame2.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
frame2.pack();
frame2.setVisible(true);
}
}
Physics Sim Class (Main and graphics drawing)
public class PhysicsSim extends JComponent {
public static void main(String[] args) {
//building frame to fit correct screen size
JFrame frame1 = new JFrame("Projectile Motion - Animation");
PhysicsSim sim = new PhysicsSim();
frame1.add(sim);
frame1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame1.setLocationRelativeTo(null);
frame1.setVisible(true);
frame1.setExtendedState(frame1.MAXIMIZED_BOTH);
GUIForm1 guiForm1 = new GUIForm1();
}
public void paintComponent(Graphics g) {
//background
g.setColor(new Color(149, 179, 215));
g.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(g);
//axis
g.setColor(Color.BLACK);
g.fillRect(50, 0, 5, getHeight());
g.fillRect(0, getHeight() - 50, getWidth(), 5);
int axisY = 0;
int axisX = 0;
int axisYCounter = 1;
int axisXCounter = 1;
//axis markers
while (axisYCounter <= getHeight() / 10) {
g.fillRect(35, axisY, 20, 5);
axisY = axisY + 50;
axisYCounter++;
}
while (axisXCounter <= getWidth() / 10) {
g.fillRect(axisX, getHeight() - 50, 5, 20);
axisX = axisX + 50;
axisXCounter++;
}
//projectile
g.setColor(c);
g.fillOval(getWidth() - 1885, getHeight() - 60, 30, 30);
}
public Color c = Color.WHITE;
}