Results 1 to 10 of 10
- 02-23-2011, 08:38 PM #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:
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?Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.*; public class screen extends JFrame{ int x=10,y=15,radius=50,step=0; 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); g.fillOval(x,y,radius,radius); } 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{ Thread.sleep(10); }catch(Exception e){} } } }
Thanks in advance :)
- 02-23-2011, 08:43 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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, 08:44 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
Ok, I will try that out. Thank you, I will post back soon :)
-
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, 09:04 PM #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
Screen.classJava Code:public class Main{ public void start() { Screen screen = new Screen(); } public static void main(String[] args) { start(); } }
Is this how to start up a screen?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); } }
- 02-23-2011, 09:12 PM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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, 09:14 PM #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?
- 02-23-2011, 09:22 PM #8
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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 09:30 PM.
- 02-23-2011, 09:28 PM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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, 10:21 PM #10
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
newbie Game question
By MrNiceGuy in forum New To JavaReplies: 2Last Post: 12-03-2010, 04:48 AM -
Newbie Loop Question
By MrNiceGuy in forum New To JavaReplies: 4Last Post: 11-14-2010, 02:27 PM -
Newbie question
By MrNiceGuy in forum New To JavaReplies: 8Last Post: 11-12-2010, 05:29 AM -
newbie question
By ronguilmet in forum New To JavaReplies: 2Last Post: 11-16-2009, 02:37 AM -
Newbie question; Vectors
By Kern in forum New To JavaReplies: 7Last Post: 08-03-2008, 06:59 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks