fork download
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import static java.lang.Math.*;
  5.  
  6. class MyPanel extends JPanel {
  7. int w, h, h1, hh;
  8. int x0, y0, a, b;
  9.  
  10. public MyPanel() {
  11. this.w = 500;
  12. this.h = 250;
  13. this.h1 = 25;
  14. this.hh = 50;
  15. setPreferredSize(new Dimension(w, h));
  16. }
  17.  
  18. private void _throw(Graphics g) {
  19. double y = y0;
  20. for(int tt=0; y>0; tt++) {
  21. double t = tt / 50.d;
  22. double x = x0 + t * a;
  23. y = y0 + t * b - t * t * 9.8 / 2;
  24. g.fillOval(w/2+(int)x, h-hh-(int)y, 2, 2);
  25. }
  26. }
  27.  
  28. @Override public void paint(Graphics g) {
  29. super.paint(g);
  30. g.drawLine(0, h-hh, w, h-hh);
  31. g.drawLine(w/2, h1, w/2, h-hh);
  32. _throw(g);
  33. }
  34. }
  35.  
  36. class MyFrame extends JFrame implements ActionListener {
  37. JTextField txtX0, txtY0, txtA, txtB;
  38. JButton btnThrow;
  39. MyPanel p;
  40.  
  41. public MyFrame() {
  42. this.setSize(600, 300);
  43. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  44. this.setResizable(false);
  45. JLabel l1 = new JLabel("初期位置");
  46. JLabel l2 = new JLabel("X0");
  47. this.txtX0 = new JTextField(3);
  48. JLabel l3 = new JLabel("Y0");
  49. this.txtY0 = new JTextField(3);
  50. JLabel l4 = new JLabel("初速度");
  51. JLabel l5 = new JLabel("A");
  52. this.txtA = new JTextField(3);
  53. JLabel l6 = new JLabel("B");
  54. this.txtB = new JTextField(3);
  55. this.btnThrow = new JButton("投げる");
  56. this.txtX0.setText("150");
  57. this.txtY0.setText("100");
  58. this.txtA.setText("-20");
  59. this.txtB.setText("10");
  60. this.p = new MyPanel();
  61. JPanel p1 = new JPanel();
  62. p1.add(l1);
  63. p1.add(l2);
  64. p1.add(txtX0);
  65. p1.add(l3);
  66. p1.add(txtY0);
  67. p1.add(l4);
  68. p1.add(l5);
  69. p1.add(txtA);
  70. p1.add(l6);
  71. p1.add(txtB);
  72. p1.add(btnThrow);
  73. p1.add(p);
  74. getContentPane().add(p1);
  75. this.btnThrow.addActionListener(this);
  76. }
  77.  
  78. @Override public void actionPerformed(ActionEvent e) {
  79. p.x0 = Integer.parseInt(txtX0.getText());
  80. p.y0 = Integer.parseInt(txtY0.getText());
  81. p.a = Integer.parseInt(txtA.getText());
  82. p.b = Integer.parseInt(txtB.getText());
  83. p.repaint();
  84. }
  85. }
  86.  
  87. public class P72_019 {
  88. public static void main(String[] args) {
  89. new MyFrame().setVisible(true);
  90. }
  91. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty