Results 1 to 3 of 3
  1. #1
    achiu17 is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default My ellipses on my JFrame disappear every time i try to maximize the window!!!!!!!!

    Hi I'm doing a project for school and my homework program is due tomorrow here's my code:
    Java Code:
    /**
       This class simulates a cannonball fired at an angle.
    */
    public class Cannonball
    {
       /**
          Constructs a Cannonball
          @param ivel the initial velocity of the ball
          @param angle the angle at which the cannonball was launched
          (in degrees)
       */
       public Cannonball(double ivel, double angle)
       {
        velocity = ivel;
        angleDegrees = angle;
        posX = 0;
        posY = 0;    
        velocityX = velocity * Math.cos(Math.toRadians(angleDegrees));
        
        velocityY = velocity * Math.sin(Math.toRadians(angleDegrees));
     
       }
    
       /**
          Updates the position and velocity of this cannon ball
          after a given time interval.
          @param deltaT the time interval
       */
       public void move(double deltaT)
       {
        
    
        posX +=  velocityX * deltaT;
        posY +=  velocityY * deltaT; 
        velocityY -= G* deltaT; 
       }
    
       /**
          Gets the x position of this cannon ball.
          @return the horizontal position
       */
       public double getX()
       {
        return posX;
       }
    
       /**
          Gets the y position of this cannon ball.
          @return the vertical position
       */
       public double getY()
       {
        return posY;
       }
    
      private double angleDegrees;
      private double posY;
      private double posX;
      private double velocity;
      private double velocityX;
      private double velocityY;
      public static final double G = 9.81;
    
    }
    Java Code:
    import javax.swing.JComponent;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.Ellipse2D;
    import javax.swing.JPanel;
    
    /**
       Draws the trajectory of a cannonball.
    */
    public class CannonballComponent extends JComponent
    {
       /**
          Constructs a component that paints the flight of a cannonball 
          @param ivel the initial velocity of the ball
          @param ang the angle at which the cannonball was launched
       */
       public CannonballComponent(double ivel, double ang)
       {
    
       ball = new Cannonball(ivel, ang);
       }
    
       public void paintComponent(Graphics g)
       {
          Graphics2D g2 = (Graphics2D) g;
          
          while(ball.getY() >= 0)
          {
            for (int i = 1; i < 10; i++)
       { 
        ball.move(DELTA_T);
            }
             Ellipse2D.Double circle = new Ellipse2D.Double(300 - ball.getX(), 200 - ball.getY(), 5, 5);
             
             g2.draw(circle);
    
    
    
          }
       }
    
       private Cannonball ball;
       public static final double DELTA_T = 0.01;
    
       
    }
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    /**
       This is a driver for taking a screen snapshot 
       of the CannonballComponent class.
    */
    public class CannonballSnap
    {
       public static void main(String[] args)
       {
          JFrame frame = new JFrame();
    
          final int FRAME_WIDTH = 300;
          final int FRAME_HEIGHT = 300;
    
          frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
          frame.setTitle("CannonballViewer");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
          double ivel = 50;
    
          double ang = 45;
    
          CannonballComponent component = new CannonballComponent(ivel, ang);
          frame.add(component);
    
          frame.setVisible(true);
       }
    }
    Please tell me what is wrong with my code and how to fix it. Thanks in advance! Much appreciated!!!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: My ellipses on my JFrame disappear every time i try to maximize the window!!!!!!!

    looks like a duplicate post. Locking.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: My ellipses on my JFrame disappear every time i try to maximize the window!!!!!!!

    Please tell me what is wrong with my code and how to fix it.
    I'm not trying to be a smart@ss, but it really is up to you to tell us what is wrong with the code.

    Don't be panicked by the approaching deadline. Take your time and say whether the code compiles. If it does not and you can't understand the compiler's messages, copy and post those messages.

    If it does compile but at runtime does not do what you expect/intend/desire, then describe what it does do (including the full stack trace of any runtime exception if you get one) and describe what you expected/intended/desired.

Similar Threads

  1. How to Maximize the JFrame ?
    By MarMer in forum AWT / Swing
    Replies: 5
    Last Post: 06-07-2011, 06:19 AM
  2. Only one Jframe window will be opened at time
    By Zamioculcas in forum New To Java
    Replies: 4
    Last Post: 05-29-2011, 02:08 PM
  3. Disable Maximize button in JFrame?
    By Manish87 in forum AWT / Swing
    Replies: 0
    Last Post: 02-01-2011, 04:39 PM
  4. Replies: 6
    Last Post: 11-22-2010, 12:55 PM
  5. results to code disappear too fast for DOS window
    By dubdubdub in forum New To Java
    Replies: 3
    Last Post: 12-29-2007, 06:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •