Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  projectile
Language: JAVA
Code:
// Rohit Falor

// 6/20/12

// ScreenSaver.java




import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;

import java.awt.event.*;




public class Projectile extends JApplet {

     private JPanel panel3;

     private CardLayout cards;

     private Container c;

     Timer moveBall;

     private JButton fire;

     private double initialangle, initialvelocity;

     private JTextField velocity, angle;

     private double xpos, ypos;

     private long startTime, timenow;




     public Projectile() {

          moveBall = new Timer(0, actionListener);

          xpos = 50;

          ypos = 800;

     }




     public void init() { // Set panels

          c = this.getContentPane();

          cards = new CardLayout();

          c.setLayout(cards);




          this.setSize(new Dimension(1000, 800));




          panel3 = new Screen();

          panel3.setBorder(BorderFactory.createLineBorder(Color.green, 7));

          c.add(panel3, "Panel 3");

     }




     public class Screen extends JPanel implements ActionListener {




          public Screen() { // Constructor

               this.setLayout(null);




               fire = new JButton("FIRE");

               fire.addActionListener(this);

               this.add(fire);

               fire.setBounds(7, 7, 500, 50);




               velocity = new JTextField("");

               velocity.addActionListener(this);

               this.add(velocity);

               velocity.setBounds(320, 110, 500, 50);




               angle = new JTextField("");

               angle.addActionListener(this);

               this.add(angle);

               angle.setBounds(320, 210, 500, 50);

          }




          public void paintComponent(Graphics g) {

               g.setColor(Color.black);

               g.fillRect(0, 0, 1580, 800);

               g.setFont(new Font("Comic Sans", Font.BOLD, 45));




               g.setColor(Color.BLUE);

               g.drawString("Initial Velocity", 7, 150);

               g.drawString("Initial Angle", 7, 250);




               timenow = System.currentTimeMillis() - startTime;

               timenow = timenow / 1000;

               ypos = -4.9 * (int) timenow * (int) timenow

                         + (initialvelocity * (int) timenow);

               ypos = 600 - ypos;

               xpos += .15;

               g.setColor(Color.red);

               g.fillOval((int) xpos, (int) ypos, 30, 30);




          }




          public void actionPerformed(ActionEvent e) {

               String command = e.getActionCommand();




               String velocity1 = velocity.getText();

               initialvelocity = 70;




               String angle1 = angle.getText();

               initialangle = 35;




               if (command.equals("FIRE")) {

                    startTime = System.currentTimeMillis();

                    c.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));

                    moveBall.start();

                    repaint();

               }

          }

     }




     ActionListener actionListener = new ActionListener() {

          public void actionPerformed(ActionEvent actionEvent) {

               if (moveBall.isRunning())

                    ypos = -4.9 * (int) timenow * (int) timenow

                              + (initialvelocity * (int) timenow);

               ypos = 600 - ypos;

               repaint();

          }

     };

}
Comments: