Results 1 to 10 of 10
  1. #1
    gblax123 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default 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{
    	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){}
    
    		}
    			
    	}	
    	
    }
    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?

    Thanks in advance :)

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #3
    gblax123 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Ok, I will try that out. Thank you, I will post back soon :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #5
    gblax123 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    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. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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. #7
    gblax123 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    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. #8
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #10
    gblax123 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    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-23-2011 at 11:46 PM.

Similar Threads

  1. newbie Game question
    By MrNiceGuy in forum New To Java
    Replies: 2
    Last Post: 12-03-2010, 04:48 AM
  2. Newbie Loop Question
    By MrNiceGuy in forum New To Java
    Replies: 4
    Last Post: 11-14-2010, 02:27 PM
  3. Newbie question
    By MrNiceGuy in forum New To Java
    Replies: 8
    Last Post: 11-12-2010, 05:29 AM
  4. newbie question
    By ronguilmet in forum New To Java
    Replies: 2
    Last Post: 11-16-2009, 02:37 AM
  5. Newbie question; Vectors
    By Kern in forum New To Java
    Replies: 7
    Last Post: 08-03-2008, 06:59 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
  •