Results 1 to 5 of 5
  1. #1
    aalsaqri is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default Help drawing images for pong game

    Hi, my friend and I are working on our final project for the end of the year in Java. We really haven't worked outside of the topics in regular AP Computer Science A, so we need a little help. We want to create a basic Pong game with basic functionality. Right now we are trying to get the images to show up in our window, everything compiles correctly, but we are unable to get the paddle nor the ball drawn into our window. Any suggestions would be greatly appreciated. This is what we have so far:

    Java Code:
    //Ball Class
    import java.awt.*;
    
    public class Ball  
    {
    	 protected int x = 400;
         protected int y = 300;
         protected int height = 20;
         protected int width = 20;
         protected int xdir = 2;
         protected int ydir = 0;
    	
    	public Ball()
        {
        }
    
        public void draw(Graphics g)
        {
            g.setColor(Color.GREEN);
            g.fillOval(x,y,width,height);
        }
    
        public void move()
        {
            x +=xdir;
            y +=ydir;
    
            if(x > 570 ){
                 setXDir(-8);
                 setYDir(randomNum());
    
            }
            if(x < 0 ){
                 setXDir(8);
                 setYDir(randomNum());
            }
    
    
            if(y < 2){
                setYDir(8);
                setXDir(randomNum());
            }
    
            if(y > 520){
                setYDir(-8);
                setXDir(randomNum());
    
            }
        }
    
        public int randomNum()
         {
     		double r = Math.random();
     		int myNumber = (int)(r*2f);
     		int num = 0;
     		if(myNumber==0)num = -8;
     		if(myNumber==1)num =  8;
    		return num;
         }
    
    
        public void resetState()
        {
            x =400;
            y = 300;
            xdir = 0;
            ydir = 0;
        }
    
        public void setYDir(int y)
        {
            ydir = y;
        }
    
        public void setXDir(int x)
        {
            xdir = x;
        }
    
        public int getYDir()
        {
            return ydir;
        }
    
        public int getXDir()
        {
            return xdir;
        }
    }
    
    
     
     
    //GamePaddle class.
    import java.awt.*;
    
    public class GamePaddle
    {
    	protected int x = 575;
        protected int y = 300;
        protected int height = 110;
        protected int width = 15;
        int num;
    
        public GamePaddle ()
        {
        }
        
        public void moveRight()
      	{
           if (x > 500)
           {
             x += 0;
           }
           
           else x+=40;
        }
        
        public void moveLeft()
        {
            if (x < 15)
            {
                x -= 0;
            }
            
            else
           		x-=40;
        }
    
        public Rectangle getRect()
        {
        	return new Rectangle(x, y,width, height);
        }
    
        public void draw(Graphics block)
        {
            block.setColor(Color.YELLOW);
            block.fillRect(x,y,width,height);
        }
    }
    
    //Pong Class
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    public class Pong extends JFrame
    {
    	private JPanel jContentPane = null;
            // This is the panel of the game class
    	private PongGame panel = null;
    
    	private PongGame getPanel()
        {
      		if (panel == null) 
            {
            // This creates a new game panel
       		panel = new PongGame(); 
      		}
      		return panel;
     	}
     	
     	/**
      	* This is the default constructor
      	*/
      
    	public Pong()
      	{
      		initialize();
      		
      		// Listens for the keyboard buttons and controls
            this.addKeyListener(new KeyAdapter()
            {
             	
             //The button that is pressed
             @Override
             public void keyPressed(KeyEvent evt)
             {
                formKeyPressed(evt);
             }
             
             // the button that is released
             @Override
             public void keyReleased(KeyEvent evt)
             {
                formKeyReleased(evt);
             }
            });
      	}  
      		   
      	public void formKeyPressed(KeyEvent evt)
        {
           panel.keyPressed(evt);
        }
    
        public void formKeyReleased(KeyEvent evt)
        {
           panel.keyReleased(evt);
        }
      
    
        // Sends which keys were pressed and release to the game class
     	//This creates the frame and its dimensions
     	
     	private void initialize()
            {
      		this.setResizable(false);
      		this.setSize(300, 300);
      		this.setContentPane(getJContentPane());
      		this.setTitle("Pong");
     		}
    
     	//intialize jContentPane
     	private JPanel getJContentPane()
        {
      		if (jContentPane == null)
            {
       			jContentPane = new JPanel();
       			jContentPane.setLayout(new BorderLayout());
    			jContentPane.add(getPanel(), BorderLayout.CENTER);
      		}
      		return jContentPane;
     	}
    
     	public static void main(String[] args)
            {
      		SwingUtilities.invokeLater(new Runnable()
            	{
       			public void run()
                    {
        			Pong thisClass = new Pong();
        			thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        			thisClass.setVisible(true); 
       				}
      				});
     		}
    }
    
    
    //PongGame Class 
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.event.KeyEvent.*;
    
    public class PongGame 
    {
    	private Ball ball;
    	private GamePaddle Player1;
    	private GamePaddle Player2;
    	
    	public PongGame() 
    	{
    	Ball ball = new Ball();
    	GamePaddle Player1 = new GamePaddle();
    	GamePaddle Player2 = new GamePaddle();
    	}
    	
    	public void keyPressed(KeyEvent evt)
    	{
    		if(evt.getKeyChar() == VK_A)
    			Player1.moveLeft();
    		else if(evt.getKeyChar() == VK_D)
    			Player1.moveRight(); 
    		else if(evt.getKeyChar() == VK_KP_LEFT)
    			Player2.moveLeft();
    		else if(evt.getKeyChar() == VK_KP_RIGHT)
    			Player2.moveRight();	 
    	}
    }

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

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    I've found many problems before being able to see the paddle and ball.

    First you need to get the program to compile.

    Suggestion on use of literals (300, 500, etc) for sizes and initial displacements and offsets etc. Don't hardcode values deep in the code. Its better to use variables defined at the start of the program that are easy to see and change.

    Make the frame large enough so the images are shown. 300 is too small for some.

    How does the program draw? It needs to use the what I call the AWT engine to get access to the Graphics object to draw with. That's done by overriding a paint method somewhere and then calling your draw methods.

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

    Default

    Agrees with Norm. Also, since it appears that you're using Swing, you should instead override a JPanel's paintComponent method and do drawing in there.

  5. #5
    aalsaqri is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default

    thanks a ton for the advice, we are working on getting it to compile right now, and we doubled the size of the frame to 600, and we are also working on the other advice you gave. Thanks, and we will keep you updated. :)

Similar Threads

  1. Pong game
    By hannes in forum Threads and Synchronization
    Replies: 4
    Last Post: 02-03-2011, 11:52 AM
  2. Ping pong game
    By adam405 in forum New To Java
    Replies: 8
    Last Post: 10-20-2010, 04:52 PM
  3. The bug in the Pong game
    By Phoeenix in forum New To Java
    Replies: 4
    Last Post: 07-21-2009, 08:13 PM
  4. drawing images
    By diggitydoggz in forum New To Java
    Replies: 4
    Last Post: 01-02-2009, 04:15 AM
  5. Help with pong game
    By Eric in forum New To Java
    Replies: 2
    Last Post: 07-03-2007, 08:02 PM

Posting Permissions

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