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

Printable View

• 11-08-2011, 05:58 AM
achiu17
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:
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; }```
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;   }```
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!!!
• 11-08-2011, 06:11 AM
Fubarable
Re: My ellipses on my JFrame disappear every time i try to maximize the window!!!!!!!
looks like a duplicate post. Locking.
• 11-08-2011, 06:12 AM
pbrockway2
Re: My ellipses on my JFrame disappear every time i try to maximize the window!!!!!!!
Quote:

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.