Reputation: 9
This program supposes to draw a trajectory of the firework, but it doesn't print out anything (nor an error). But the paintComponent draws rectangle and circle, and there are values inside of x1, y1, x2 and y2 at specific t seconds. Any advice is helpful. Thanks.
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Firework_Animation extends JComponent implements ActionListener {
protected JButton fire;
protected JLabel click;
protected JButton start_button;
JTextField speed_text;
JTextField angle_text;
JTextField time_text;
JTextField explosion_text;
JLabel speed_label;
JLabel angle_label;
JLabel time_label;
JLabel explosion_label;
int count;
String a;
String s;
String t;
String u;
String v;
double speed;
int angle;
int time;
int explosion;
int x1;
int y1;
int x2;
int y2;
public Firework_Animation () {
setLayout (new FlowLayout ());
speed_label = new JLabel ("What is the speed?");
add (speed_label);
speed_text = new JTextField (3);
speed_text.addActionListener(this);
add(speed_text);
angle_label = new JLabel ("What is the angle");
add (angle_label);
angle_text = new JTextField (3);
angle_text.addActionListener(this);
add(angle_text);
time_label = new JLabel ("How long do you want the explosion to happen for?");
add (time_label);
time_text = new JTextField (3);
time_text.addActionListener(this);
add(time_text);
explosion_label = new JLabel ("Enter between 1-5 to test different kinds of explosions! ");
add (explosion_label);
explosion_text = new JTextField (3);
explosion_text.addActionListener(this);
add (explosion_text);
start_button = new JButton ("Start");
start_button.addActionListener(this);
add (start_button);
}
//getgraphics in actionperformed, use that
public void actionPerformed (ActionEvent a) {
if(a.getSource() == start_button) {
a.getActionCommand();
s = speed_text.getText();
speed = (Double.valueOf(s))-1;
t = angle_text.getText();
angle = (Integer.valueOf(t))-1;
u = time_text.getText();
time = (Integer.valueOf(u))-1;
v = explosion_text.getText();
explosion = (Integer.valueOf(v))-1;
}
repaint();
}
public void paintComponent (Graphics g) {
g.drawRect(200, 200, 40, 40);//check paint component works
g.drawOval(300, 300, 30, 30);
for (int i = 0; i < time; i++) {
x1 = (int) (1.0 * (speed) * (Math.cos(angle)) * i);
y1 = (int) ((1.0 * (speed) * (Math.sin(angle)) * i) - ((0.5) * (9.8) * (i*i)));
x2 = (int) (1.0 * (speed) * (Math.cos(angle)) * (i+1));
y2 = (int) ((1.0 * (speed) * (Math.sin(angle)) * i+1) - ((0.5) * (9.8) * ((i+1)*(i+1))));
g.setColor (Color.ORANGE);
g.drawLine(x1,y1, x2, y2);
}
}
public static void main(String[] args) {
Firework_Animation fa = new Firework_Animation();
JFrame frame = new JFrame("Fireworks");
frame.add(fa);
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Upvotes: 0
Views: 116
Reputation: 1692
You've overriden the paintComponent(), but you don't have anything to keep refreshing it to show the animation.
You need the equivalent of a game loop. There's no one way to do it, but here's one option.
Implement Runnable in your JComponent
. Kick off the animation when your start button is clicked by changing the animationState
. Change it back when you're done.
@Override
public void run() {
while( true ) {
if( getAnimationState() == AnimationState.RUNNING ) {
// do an animation
repaint();
// turn it off if we're done
if( animationIsDone() ) {
transitionState( animationIsDone );
}
}
// pause a tad
try {
Thread.sleep( 250 );
} catch (Exception e) {
System.out.println( "Sorry, don't know what happened: " + e.toString() );
e.printStackTrace();
}
}
}
Also implement some error-checking in your JComponent
. I was able to crash it easily by not entering any values for the fields.
Upvotes: 0