Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

A Little Help

Thread view: 
raverocker@hotmail.com - 02 Sep 2007 03:28 GMT
Hey guys! I'm new here. And I need your help.

IMPROVE TargetPractice.java to allow the user to do the following:
1.    add any number of targets he/she wishes to "shoot-at" by inputting
the top-left coordinate of target and its scale
2.    specify the flight of the projectile by inputting its velocity and
angle of elevation

PROGRAMMING HINTS:
Your program should declare an ArrayList of "targets", since the
target is a Polygon, the declaration of the list should be something
like this...
ArrayList<Polygon> myList;

To create a target, use the createTarget method in the program...
Polygon target = createTarget(200, 300, 4);
// creates a target at coordinates 200, 300 in the applet, the
// third argument specifies that scale which is 4-times larger
// that the original size of the target

and to add this target to list myList, do this...
myList.add(target);

To access each target in myList, do this...

    for (int i=0; i<myList.size(); i++) {
// process "target" after this statement
Polygon target = myList.get(i);

// process "target" here...
}

THE CODE IS HERE

FEEL FREE TO EDIT IT OR ADD TO IT

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*; // imports "Graphics2D" class
import javax.swing.*;   // imports "JApplet"
import javax.swing.border.*;

// Java treat TargetPractice as a JApplet class
public class TargetPractice extends JApplet implements ActionListener
{
    final int WIDTH = 800; // PIXELS
    final int HEIGHT = 600;
   boolean initialized, firstTime;

      Polygon target;

    /***
    * Just a constructor...
    */
   public TargetPractice() {
       super(); // invokes the JApplet constructor
       initialized = false;
       firstTime = true;
   }

   public void init() {
       initializeFrame();
   }

   public void initializeFrame() {
       JFrame mainFrame;
       JPanel firingRangePanel, menuPanel, mainPanel;
       JApplet firingRangeApplet;
       JButton fireButton;
       Font bigFont = new Font("Lucida Console", Font.BOLD, 18);

       fireButton = new JButton("Fire!!!");
       fireButton.setFont(bigFont);
       fireButton.addActionListener(this);
       fireButton.setMnemonic(KeyEvent.VK_F); // VK: "Virtual Key"
                                             // Shortcut key: "ALT-F"

        firingRangeApplet = this;
        firingRangeApplet.setPreferredSize(new Dimension(WIDTH, HEIGHT));

        firingRangePanel = new JPanel();
        firingRangePanel.setLayout(new BorderLayout());
        firingRangePanel.setBorder(new EtchedBorder(EtchedBorder.RAISED));
        firingRangePanel.add(firingRangeApplet);

        menuPanel = new JPanel();
        menuPanel.setBorder(new EtchedBorder(EtchedBorder.RAISED));
        menuPanel.add(fireButton);

        mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.add(menuPanel);
        mainPanel.add(firingRangePanel);

       mainFrame = new JFrame("Target Practice");
       mainFrame.setContentPane(mainPanel);
       mainFrame.pack();
       mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       mainFrame.setResizable(false);
       mainFrame.setLocationRelativeTo(null);
       mainFrame.setAlwaysOnTop(true);
       mainFrame.setVisible(true);

       initialized = true;
       firstTime = true;

       repaint(); // invokes the "paint" method
   }

    /***
    * Decorate the applet...
    */
   public void paint(Graphics g) {
        if (initialized==false) {
            return;
        }

       Graphics2D canvas = (Graphics2D)g;
/*
       //left(x), top(y), width,height
       canvas.draw(new Rectangle2D.Double(100, 200, 100, 50));

       canvas.setColor(Color.RED);
       //left(x), top(y), width,height
       canvas.fill(new Ellipse2D.Double(200, 100, 200, 150));
 */

        if (firstTime==true) {
            // draw the TARGET the FIRST TIME the paint method was invoked
(with "repaint()")

           target = createTarget(200, 200, 5); // arguments are: left
coordinate, top coordinate, scale

           canvas.setColor(Color.GREEN);   // draw the target
           canvas.fill(target);

           canvas.setColor(Color.BLACK);   // draw the "outline" of the
target
           canvas.draw(target);
        } else {
            // draw the PROJECTILE the "NEXT TIME" the paint method was
invoked (by clicking the FIRE BUTTON)
            double velocity = 100; // meters per second
             double elevation = 70; // (degrees) angle of elevation
             final double G = 9.8; // meters per second^2
             double x=0, y=0;

             elevation = Math.toRadians(elevation);

             double time = 0;
             double offsetX = 0;
             Rectangle2D.Double eraser = new Rectangle2D.Double(0, 0, 7, 7);
             Rectangle2D.Double projectile = new Rectangle2D.Double(0, 0, 5,
5);

             do {
                 eraser.x = projectile.x-1;
                 eraser.y = projectile.y-1;

                 x = velocity*time*Math.cos(elevation);
                 y = velocity*time*Math.sin(elevation)-G*time*time/2;

                 projectile.x = offsetX + x;
                 projectile.y = HEIGHT - y;

                // a better alternative than:
                // if (target.contains(projectile.x, projectile.y))
                 if (target!=null && target.intersects(projectile)) {

                     canvas.setColor(Color.WHITE);  // "remove" the target on
the ...
                     canvas.fill(target);           // ... "canvas" by drawing it
white
                     canvas.setColor(Color.WHITE);
                     canvas.draw(target);

                     target=null; // physically remove the target
                }

                 canvas.setColor(Color.WHITE);
                 canvas.fill(eraser);

                 canvas.setColor(Color.BLUE);
                 canvas.fill(projectile);

                 time += 0.01;
                 try {
                     Thread.sleep(1); // milliseconds
                 } catch(Exception e) {
                     // do nothing
                 }

            /*
                  if (y<0) {
                     offsetX+=x;
                     time=0;
                     x=0;
                 }
            */
             } while (y>=0);

             canvas.setColor(Color.WHITE);
             canvas.fill(eraser);

        } // end of IF-FIRSTTIME
   }

    public Polygon createTarget(int left, int top, double scale) {
       int[] xpoints={4,6,8,12,8,12,8,6,4,0,4,0};
          int[] ypoints={0,4,0,4,6,8,12,8,12,8,6,4};

          for (int i=0; i<xpoints.length; i++) {
              xpoints[i]=(int)(xpoints[i]*scale); // set the scale
              ypoints[i]=(int)(ypoints[i]*scale);

           xpoints[i]+=left;  // set the position
              ypoints[i]+=top;
          }

       return new Polygon(xpoints, ypoints, xpoints.length);
    }

   /***
    * invoked by the fireButton
    */
   public void actionPerformed(ActionEvent ae) {
        firstTime = false;
        repaint();
    }

   public static void main(String[] args) {
       new TargetPractice().init();
   }
}

ANY HELP WOULD BE REALLY APPRECIATED
Andrew Thompson - 02 Sep 2007 04:26 GMT
>Hey guys! I'm new here. And I need your help.
...
>ANY HELP WOULD BE REALLY APPRECIATED

I will offer this advice..
- Use a specific and relevant Subject line for the post
- Try to avoid sounding 'needy'
- Ask a specific question
- Refrain from SHOUTING at your audience
..and mostly..
- Don't ask people to do your homework.

Other advice.
- Crack the textbooks and get reading.
- If you get stuck, check this page for tips..
<http://home.earthlink.net/~patricia_shanahan/beginner.html>

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Hunter Gratzner - 02 Sep 2007 08:03 GMT
On Sep 2, 4:28 am, raveroc...@hotmail.com wrote:
> Hey guys! I'm new here. And I need your help.
>
[quoted text clipped - 3 lines]
> 2.      specify the flight of the projectile by inputting its velocity and
> angle of elevation

Try this for a start http://tinyurl.com/3dh2bx
Lothar Kimmeringer - 02 Sep 2007 11:43 GMT
> Hey guys! I'm new here. And I need your help.
>
> [homework-description
>
> ANY HELP WOULD BE REALLY APPRECIATED

So what exactly is your problem? I haven't seen any question
of yours.

Regards, Lothar
Signature

Lothar Kimmeringer                E-Mail: spamfang@kimmeringer.de
              PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                questions!

Roedy Green - 02 Sep 2007 12:38 GMT
>ANY HELP WOULD BE REALLY APPRECIATED

see http://mindprod.com/jgloss/homework.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.