# Thread: Help with JFrame function!!!!

1. Member
Join Date
Nov 2011
Posts
4
Rep Power
0

## Help with JFrame function!!!!

Hi, I'm doing a project for school that's due tomorrow and I have the code figured out but whenever I run it and try to maximize the window, the circles I drew disappear. 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;

}

/**
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.setVisible(true);
}
}```

2. ## Re: Help with JFrame function!!!!

You've got program logic in the paintComponent method mainly due to this line: while(ball.getY() >= 0). That will tie up your painting and can make your GUI unresponsive and make graphics fail. Solution: don't do this.

Other suggestions: improve your code formatting a bit so that it's more consistent, especially if you want volunteers to help you with it, since you want to make it as easy as possible for them to read.

3. Member
Join Date
Nov 2011
Posts
4
Rep Power
0

## Re: Help with JFrame function!!!!

Thanks but my teacher actually wrote that part out for the class. Sorry about the poor formatting, I was in a hurry to finish and compile the program but that's no excuse

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

## Re: Help with JFrame function!!!!

whenever I run it and try to maximize the window, the circles I drew disappear
You are not actually trying to animate the cannon ball are you? If all you are doing is plotting the ball at a bunch of times, then I don't think the while loop should prove a problem.

What *will* cause a problem - for any repaint, not just when you maximize the screen - is the position of the cannon ball when you do the repaint. What I mean is this: you do the initial paint moving the ball by a whole bunch of deltas until it hits the ground again. But when you do any subsequent repaint the ball has already hit the ground so nothing gets painted.

One solution might be not creating the ball in the component's constructor, but, rather, creating a new ball each time you have to repaint the component.

 altered wording of the last sentence, because there are options: give the ball a reset method, or compute the circle centers in the component's constructor (as in the paint method) and have the paint method just run through the array/list of centers and draw the circles.
Last edited by pbrockway2; 11-08-2011 at 06:58 AM.

#### Posting Permissions

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