我正在尝试为自己制作的“烟花”发射器绘制轨迹,但是我遇到了一个问题,即我的轨迹仅在初始窗口大小的范围内重新绘制。调整窗口大小时,一切都会重新绘制,将左下角设置为原点,但最终轨迹被抹掉了。我附上了一些我的意思的图片。我认为这可能与在main()中绘制轨迹的for循环有关,但我不确定如何解决。随附图片示例。 https://i.imgur.com/ymcSE7N.png
package splode;
import javax.swing.JComponent;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Graphics;
public class Fuse extends JComponent {
private static final long serialVersionUID = 1L;
double x;
double y;
static JFrame frame = new JFrame("Fireworst");
int key;
public Fuse(int v, int a, double t, int key) {
frame.setSize(1000, 548);
this.x = v*Math.cos(Math.toRadians(a))*t;
this.y = v*Math.sin(Math.toRadians(a))*t-.5*9.8*(Math.pow(t, 2));
this.key = key;
}
public void paintComponent(Graphics g) {
if (key == 0) {
g.setColor(Color.RED);
g.fillOval((int)x, frame.getHeight()-(int)y-50, 2, 2);
}
else if (key == 1) {
for(int j=0; j<=340; j+=20) {
g.setColor(Color.RED);
g.fillArc((int)x-75, frame.getHeight()-(int)y-100, 150, 100, j+10, 3);
g.fillArc((int)x-50, frame.getHeight()-(int) y-125, 100, 150, j, 3);
}
}
}
public static void main(String[] args) {
for (double i=0; i<=25; i+=.1) {
Fuse canvas = new Fuse(100, 45, i, 10);
frame.add(canvas);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
frame.setVisible(true);
frame.getContentPane().setBackground( Color.BLACK );
Fuse fuse = new Fuse(100, 45, 7, 2);
frame.add(fuse);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
System.out.println(fuse.x);
System.out.println(fuse.y);
}
}