# Probably a newbie question.... :P

• 02-23-2011, 09:38 PM
gblax123
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:

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?

• 02-23-2011, 09:43 PM
sunde887
You should probably make a ball class which does all the moving stuff, from there simply creating more objEcts of type ball should work
• 02-23-2011, 09:44 PM
gblax123
Ok, I will try that out. Thank you, I will post back soon :)
• 02-23-2011, 09:45 PM
Fubarable
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.
• 02-23-2011, 10:04 PM
gblax123
To be honest... I have no idea what I am doing. I tried setting something up like this:

Main.class
Code:

public class Main{

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

public static void main(String[] args) {

start();

}
}

Screen.class
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?
• 02-23-2011, 10:12 PM
al_Marshy_1981
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.
• 02-23-2011, 10:14 PM
gblax123
Yeah, I have no idea what that means lol.... does that mean I should not call the main static? Or call the start static?
• 02-23-2011, 10:22 PM
al_Marshy_1981
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
• 02-23-2011, 10:28 PM
sunde887
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
• 02-23-2011, 11:21 PM
gblax123
Solved... At least the JPanel screen. I need to work on the class for the balls and such now :D