0

该程序假设绘制烟花的轨迹,但它没有打印出任何东西(也没有错误)。但是paintComponent绘制矩形和圆形,并且在特定t秒时x1,y1,x2和y2内部有值。任何建议都是有帮助的。谢谢。

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);


    }

}
4

1 回答 1

0

您已经覆盖了paintComponent(),但是您没有任何东西可以不断刷新它来显示动画。

您需要相当于一个游戏循环。没有一种方法可以做到这一点,但这里有一种选择。

在你的JComponent. 单击开始按钮时,通过更改animationState. 完成后改回来。

@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();
        }
    }
}

还要在您的JComponent. 通过不为字段输入任何值,我可以轻松地将其崩溃。

于 2019-11-18T22:51:09.360 回答