Results 1 to 2 of 2
  1. #1
    torlejv123 is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Smile java jgrasp pingpong

    Hi i was wondering if someone had the time and will to finish my applet (in jgrasp). Its an assignment for school that i really need to finish this weekend. Its the typical pong game, Two boards and a ball bouncing from side to side. I've managed to get the ball bounce around in a the box and I can move the two blocks up and down. Now to the problem, I can't figure out how to get the ball to bounce when it hits the blocks. The applet should also include a point-counting system thats counts every time the ball passes one of the blocks.


    any ways here is the two parted applet:


    Java Code:
    /* Interaktivitet - lyssnare*/
    import java.awt.*;
    import java.applet.*;
    // behövs för lyssnare
    import java.awt.event.*;
    import java.awt.Choice;
    //implementerar lyssnare - actionlistener - för knappar
    public class StartGUI extends Applet implements ActionListener
    {
         Button startButton;
    	  Button pauseButton;
         StartCanvas pc;
    	  Choice level = null;
    	  Choice bc = null;
    	  Panel kn=new Panel();
    	  public void init() 
         {
    	 	   pc=new StartCanvas(this);
             startButton = new Button("Start game!");
    			pauseButton = new Button("Pause game!");
    			level = new Choice();
    			//lägg till nivå
    			level.add("1");
    			level.add("2");
    			level.add("3");
    			level.add("4");
    			 bc = new Choice();
    			//lägg till bakgrundsfärg
    			bc.add("Yellow");
    			bc.add("Green");
    		   bc.add("Gray");
    			bc.add("White");
    		     // lägg till lyssnare
              startButton.addActionListener(this);
        		 pauseButton.addActionListener(this);
    	        			 
    			  setLayout(new GridBagLayout());
    			  GridBagConstraints c = new GridBagConstraints();
    	        //fyll allt
    			  c.fill = GridBagConstraints.BOTH;
    	        //position    
    			  c.gridx = 0;
    	        c.gridy = 0;
    			  //storlek på y
    			  c.ipady = getHeight()-25;
    			  add(pc, c);
    			  //fyll panell först
    	        kn.setLayout(new GridLayout(1,4,30,10));  
    			  kn.add(startButton);
    			  kn.add(pauseButton);
    			  kn.add(bc);
    	        kn.add(level);
    			  //och till sist hela applet
    			  c.ipady = 25;
    			  c.gridx = 0;
    			  c.gridy = 1;
    	        add(kn, c);
    			 
    			 
    			 
             }
    			
    			public void enableAll(boolean b)
    			{
    				level.setEnabled(b);
    				bc.setEnabled(b);
    				startButton.setEnabled(b);
    
    
    			}
    			
    			
    			
    			
    			
    			
                   //utförs när någon knapp trycks 
            public void actionPerformed(ActionEvent evt) 
             {
                  //om det är ok knapp, rita om
                  if (evt.getSource() == startButton) 
    				  {
    				       	pc.setVel(level.getSelectedItem());
    				         pc.setPause(false);
    							pc.setBackgroundCol(bc.getSelectedItem());
    							enableAll(false);
    
                       
    				  }
    				  if(evt.getSource() ==pauseButton) 
    				  {
    				   pc.setPause(true);
    				  	enableAll(true);
    
    				  }
    
           } 
    } 
    
    
    /*trådar - animering*/
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.awt.event.KeyEvent;
    //Implementera Runnable för trådar
    public class StartCanvas extends Canvas implements Runnable,KeyListener
    {
             public StartGUI sp;
             //tråd
    			final int rx=10;
    			final int ry=30;
    
             private Thread bollThread;
             //bållens x,y - koordinater      
    			private int x=50,y=100;
    			//bollens hastighet x och y koordinater
             private int vx=5,vy=5; 
    			int ryh=150;
    			int ryv=150;   
    		   //true - om tråden körs , annars falsk
    		   boolean running = true;
    			//för att pausa tråden
    			boolean pause = true;
    			//bollens radie
     			private final int BR=10;
    			//hur fort bilden uppdateras - konstant - nivå 1
    			private final int VEL=100;
           	//hur fort bilden uppdateras
    			private int vel=40;
    			//bakgrundsfärg
    			Color  bgColor=Color.white;
    		   public StartCanvas(StartGUI p)
             {
    			      sp=p;
    			 	   //skapa och starta tråden
    		         bollThread= new Thread(this);
                   bollThread.start();
    			 		addKeyListener( this );
             }
    			
    			
    			public void setVel(String nr)
    			{
    				vel=VEL/Integer.parseInt(nr);
    			}
    			
    		
           			
    				public void update(Graphics g) {
    						Graphics offgc;
    						//skapa en buffer för att lagra bilden 
    						Image	offscreen = createImage(getWidth(), getWidth());
    						offgc = offscreen.getGraphics();
    						// tömm hela ytan = fyll allt med bakgrundsfärg
    						offgc.setColor(getBackground());
    						offgc.fillRect(0, 0,getWidth(), getWidth());
    						offgc.setColor(getForeground());
    						//omritning - lagra hela bilden i buffer
    						paint(offgc);
    						//när hela bilden blir klar skicka den till skärmen 
    						g.drawImage(offscreen, 0, 0, this);
             }
    	
             //stoppa tråden , när appleten avslutas
    		   public void destroy()
             {
                  // stoppa tråden
    				    running = false;
                    bollThread = null;
             }
             public void paint(Graphics g)
    			{
    			    setBackground(bgColor);
    			    //kolla X gränser 
    			    if (checkXBoundaries())
                       vx=-vx;
    						 
    	          //kolla Y gränser 
    				 if (checkYBoundaries())
                  {    
    						 
    						 vy=-vy;
    				}
    				
    				
    					 
    							 
    				 //uppdatera x koordinater
    			    
    				 x+=vx;
    				 y+=vy;
    				 g.setColor(new Color(0,0,255));
    				 g.fillArc(x,y, BR, BR,0,360); //rita  en röd cirkel
    					 
    				 g.fillRect(10,ryv,rx,ry);
    				 g.fillRect(360,ryh,rx,ry);
    				 
    
    			
     
    			}
    			
    		   //bestäm färgen	
    			public void setBackgroundCol(String col)
    			{
    				if(col.equals("Green"))
    					bgColor=Color.green;
    				if(col.equals("Yellow"))
    					bgColor=Color.yellow;
    				if(col.equals("Gray"))
    					bgColor=Color.gray;
    				if(col.equals("White"))
    					bgColor=Color.white;
    
    				
    			}
    			
    			
    			//ha en paus
    			public void setPause(boolean p)
    			{
    				pause=p;
    			}
    			
    			 public void keyPressed( KeyEvent e ){}
    			public void keyReleased( KeyEvent e ) { }
    			public void keyTyped( KeyEvent e ) {
    			char c = e.getKeyChar();
          	if ( c == 'w' && ryv>5) 
    					ryv=ryv-5;
    			if (c== 'd' && ryv+ry < getHeight()-5)
    				ryv=ryv+5;
    			
    			if (c== 'o' && ryh >5)
    					ryh=ryh-5;
    			
    			if (c== 'k' && ryh+ry < getHeight()-5)
    						ryh=ryh+5;
    		 
    			repaint();
    			e.consume();
    			
    			
    		
    			
    				
    				
    		}
    		
          
    			
    			
    	
    			
    			
    			
    			
    			//kolla x och y gränser
    			private boolean checkYBoundaries()
    			{
    				return (y<0 && BR<=ry || y+BR>getHeight());
    			}
    			
    			private  boolean checkXBoundaries()
    			{
    			   return (x<0 && BR<=rx|| x+BR>getWidth());
    
    			 }
    		  //omritningen händer i denna tråd
    		  public void run()
    	     {
    	          //lopp där uppdateringen (omritningen) skers
    	          while (running)
    	          {
    				   if(!pause)
    					{
    			               try
    			               {
    			                     // vänta vel ms
    			                     bollThread.sleep(vel);
    										repaint();
    			               }
    			               catch (InterruptedException e)
    			               {
    			                    System.out.println(e);
    			                }
    					 }
    	           }
    	     }
    }

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

    Default

    Quote Originally Posted by torlejv123 View Post
    Hi i was wondering if someone had the time and will to finish my applet (in jgrasp).
    Please, this is not a homework finishing forum.

    If you want our help, and are willing to do the work yourself, then please ask us specific questions about bugs in your code or concepts you're not clear on and we'll be more than happy to help you. Best of luck.

    edit: Also consider creating more classes including a Ball class and a Paddle class. Then giving the game a "model" -- a non-gui class that runs the game and that the GUI portion uses to paint itself with. This assignment will be a lot easier if you can divide out your concepts so you can work on the ball bouncing logic in isolation of the GUI.
    Last edited by Fubarable; 11-27-2010 at 12:10 AM.

Similar Threads

  1. Need hmwk help with Arrays in jgrasp
    By cmps280CD in forum New To Java
    Replies: 9
    Last Post: 09-14-2010, 03:23 AM
  2. jGRASP debugger
    By TsAmE in forum New To Java
    Replies: 2
    Last Post: 02-23-2010, 09:35 PM
  3. Replies: 0
    Last Post: 11-24-2009, 04:49 PM
  4. JGrasp simple exercise?
    By Boomer1 in forum Other IDEs
    Replies: 2
    Last Post: 11-02-2009, 04:23 AM
  5. GUI with jGRASP
    By Matt Sakko in forum Other IDEs
    Replies: 0
    Last Post: 03-14-2009, 10:50 AM

Tags for this Thread

Posting Permissions

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