# Thread: Probably a newbie question.... :P

1. Member
Join Date
Feb 2011
Posts
6
Rep Power
0

## Probably a newbie question.... :P

Hi everyone I am new here to the forums, and I have been working on learning basic things in java. I have here a ball physics engine (in progress) and it basically is just a ball bouncing around a screen with gravity and such. Here is the code:

Java Code:
```import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;

public class screen extends JFrame{
double xdir=1,ydir=1;
double yvelocity=-1;
double energy=4;

public screen() {
setLocation(100,100);
setSize(600,700);
setResizable(false);
setVisible(true);
setTitle("First Application");
}

public void paint(Graphics g){
move();
g.setColor(Color.white);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.black);
//g.drawString("Height: " + (190-y), 50, 50);

//g.drawString("Velocity: " + yvelocity, 50, 65);
g.translate(x,y);
g.setColor(Color.red);

}

public void move(){

if (y>320){
energy*=.9;
yvelocity=energy;
}
if (x>275) {
xdir=-1;
}
if (x<0) {

xdir=1;
}
if ((step % 2)==0) {
x+=xdir*2;
}
yvelocity=Math.max(-4, yvelocity-.04);
y-=yvelocity*1.1;
step++;

}

public static void main(String[] args){

screen screen = new screen();

while(true){

screen.repaint();

try{
}catch(Exception e){}

}

}

}```
Yes, this is all in one class because we are just dealing with one ball.... what I want to know how to do is make this be able to ADD balls... how would I go about doing this?

2. You should probably make a ball class which does all the moving stuff, from there simply creating more objEcts of type ball should work

3. Member
Join Date
Feb 2011
Posts
6
Rep Power
0
Ok, I will try that out. Thank you, I will post back soon :)

4. Some suggestions:
• Create a Ball class that encapsulates the idea of "ballness"
• Then in the animation class have an list of balls (actually, an ArrayList<Ball>).
• Do your drawing in the paintComponent method of a JPanel, not the paint method of a JFrame.
• Read the Java Swing graphics tutorials and they'll tell you the same thing (among other things)
• Do not have any program logic (such as your move() method) within the paint or paintComponent method. You do not have full control of when or even if this method will be called.
• Use a Swing Timer to drive the animation. It will iterate through the ball list, moving the balls and then call repaint() on the drawing JPanel.

5. Member
Join Date
Feb 2011
Posts
6
Rep Power
0
To be honest... I have no idea what I am doing. I tried setting something up like this:

Main.class
Java Code:
```public class Main{

public void start() {
Screen screen = new Screen();
}

public static void main(String[] args) {

start();

}
}```
Screen.class
Java Code:
```import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;

public class Screen extends JPanel{

public Screen(){

setLocation(100,100);
setSize(600,700);
setVisible(true);

}

}```
Is this how to start up a screen?

6. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
8
Have you tried to compile this code? If you have then you would know that there are errors in your Main class. Trying to call a non static method from a static code block.

7. Member
Join Date
Feb 2011
Posts
6
Rep Power
0
Yeah, I have no idea what that means lol.... does that mean I should not call the main static? Or call the start static?

8. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
8
Why do you need the start method()? Just run a new Screen() from static void main. void main must always be static. http://java.sun.com/developer/online...ava1/prog.html
Last edited by al_Marshy_1981; 02-23-2011 at 10:30 PM.

9. You are gonna have to research a lot about the basics to figure this code out. You want to create class called screen and ball, create an instance of screen in main, then as many instances of balls as you want on the screen, the screen should be placed on a frame and the balls should all have the proper methods to move around. It's no easy task if you don't understand the basics, it is however possible, take some time and think when you get stuck, figure out what goes wrong and where with print statements etc, always try to compile and run before asking for more help

10. Member
Join Date
Feb 2011
Posts
6
Rep Power
0
Solved... At least the JPanel screen. I need to work on the class for the balls and such now :D
Last edited by gblax123; 02-24-2011 at 12:46 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
•