Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

[java] New Problem:Connect 4

Started by BlackReign 7 replies 1.3k views
Original Post
BlackReign
BlackReign
I've implemented double buffering and I want it so that when I hit enter it triggers the function dropPiece in order to draw the piece in it's proper place and then call repaint to update it, but it doesn't seem to be calling dropPiece at all when hit enter. Code:


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;



public class Culminating extends Applet implements KeyListener {
	

	int playerTurn = 1;	
    int selectorX=65;
    final int selectorY=0;
    int selectorPos = 1;
    int gameEnd =0;
    int positionofLastChip = 0;
    int i = 0;
    int dropLevel = 0;
    int x = 0;
    int count = 0;
    
    Graphics g;
    Image image;
    Dimension dim;
    Graphics bufferGraphics;
    Image offscreen;
    Color playerColor = Color.red;
    
    int [] col1 = {0,0,0,0,0,0,0};
    int [] col2 = {0,0,0,0,0,0,0};
    int [] col3 = {0,0,0,0,0,0,0};
    int [] col4 = {0,0,0,0,0,0,0};
    int [] col5 = {0,0,0,0,0,0,0};
    int [] col6 = {0,0,0,0,0,0,0};
    int [] col7 = {0,0,0,0,0,0,0};
    
   

    public void keyPressed(KeyEvent ke) {
       
      switch (ke.getKeyCode()) {
         
        case KeyEvent.VK_LEFT:  // move x coordinate left
          
          bufferGraphics.setColor(Color.white); 
          bufferGraphics.fillOval(selectorX,selectorY,50,50);
          
          if (selectorPos != 1) {
             
             selectorX -= 60;
             selectorPos -= 1;
          }
          
          bufferGraphics.setColor(playerColor); 
          bufferGraphics.fillOval(selectorX,selectorY,50,50);            
        
             
          break;
          
        case KeyEvent.VK_RIGHT: // move x coordinate right
          
          bufferGraphics.setColor(Color.white);   
          bufferGraphics.fillOval(selectorX,selectorY,50,50); 
          
          if (selectorPos != 7) {
             
             selectorX += 60;
             selectorPos += 1;
          }
          
          bufferGraphics.setColor(playerColor);
          bufferGraphics.fillOval(selectorX,selectorY,50,50);        
          
          
          break;
          
        case KeyEvent.VK_ENTER:  // place piece when pressed
            
            
            dropPiece(g);
            break;
      }
      
      repaint();
      
    }   		
    
    public void keyTyped(KeyEvent event) {}
    public void keyReleased(KeyEvent event){}
    
    
    
    public void dropPiece(Graphics g) {
      
      
      
      
      
      if ( selectorPos == 1) {
        
        count = 0;
        i = 0;
        positionofLastChip = 0;
        
        
          while (i!=1) {
          	
          	i = col1[count++];
          	positionofLastChip = count ;
          	dropLevel=positionofLastChip + 1;
          	if (count > 6) {
          	  dropLevel= 1;
            }
          }
            
          x=445-(dropLevel*60);
          bufferGraphics.fillOval(65,x,50,50);
          
      } 
      else if ( selectorPos == 2) {
        
        count = 0;
        i = 0;
        positionofLastChip = 0;
        
        
          while (i!=1) {
          	
          	i = col1[count++];
          	positionofLastChip = count ;
          	dropLevel=positionofLastChip + 1;
          	if (count > 6) {
          	  dropLevel= 1;
            }
          }
            
          x=445-(dropLevel*60);
          bufferGraphics.fillOval(125,x,50,50);
      } 
      else if ( selectorPos == 3) {
        
        count = 0;
        i = 0;
        positionofLastChip = 0;
        
        
          while (i!=1) {
          	
          	i = col1[count++];
          	positionofLastChip = count ;
          	dropLevel=positionofLastChip + 1;
          	if (count > 6) {
          	  dropLevel= 1;
            }
          }
            
          x=445-(dropLevel*60);
          bufferGraphics.fillOval(185,x,50,50);
      }   
      else if ( selectorPos == 4) {
        
        count = 0;
        i = 0;
        positionofLastChip = 0;
        
        
          while (i!=1) {
          	
          	i = col1[count++];
          	positionofLastChip = count ;
          	dropLevel=positionofLastChip + 1;
          	if (count > 6) {
          	  dropLevel= 1;
            }
          }
            
          x=445-(dropLevel*60);
          bufferGraphics.fillOval(245,x,50,50);
      }     
      else if ( selectorPos == 5) {
        
        count = 0;
        i = 0;
        positionofLastChip = 0;
        
        
          while (i!=1) {
          	
          	i = col1[count++];
          	positionofLastChip = count ;
          	dropLevel=positionofLastChip + 1;
          	if (count > 6) {
          	  dropLevel= 1;
            }
          }
            
          x=445-(dropLevel*60);
          bufferGraphics.fillOval(305,x,50,50);
      }    
      else if ( selectorPos == 6) {
        
        count = 0;
        i = 0;
        positionofLastChip = 0;
        
        
          while (i!=1) {
          	
          	i = col1[count++];
          	positionofLastChip = count ;
          	dropLevel=positionofLastChip + 1;
          	if (count > 6) {
          	  dropLevel= 1;
            }
          }
            
          x=445-(dropLevel*60);
          bufferGraphics.fillOval(365,x,50,50);
      }   
      else if ( selectorPos == 7) {
        
        count = 0;
        i = 0;
        positionofLastChip = 0;
        
        
          while (i!=1) {
          	
          	i = col1[count++];
          	positionofLastChip = count ;
          	dropLevel=positionofLastChip + 1;
          	if (count > 6) {
          	  dropLevel= 1;
            }
          }
            
          x=445-(dropLevel*60);
          bufferGraphics.fillOval(425,x,50,50);
      }   
      
      if (playerTurn == 1) {                  //select colour to paint circlebased on which players turn it is   
         playerColor = Color.red;
         playerTurn=2;
      }
      else {
      
         bufferGraphics.setColor(Color.black); 
         playerTurn = 1;
      }
      
      
      repaint();
        
       
      
    	
    }	

    

    public void init() {
    	
      addKeyListener(this);
      requestFocus();
      
      setBackground(Color.white);
      dim = getSize(); 

      offscreen = createImage(dim.width,dim.height); 
      bufferGraphics = offscreen.getGraphics(); 

    }
    
    public void start() {
      
      
    	
      

      
        
    } 

    public void paint (Graphics g) {
    	
      

      //left/right stand
    	
       bufferGraphics.setColor(Color.blue);
       bufferGraphics.fillRect(40,60,10,420);
       bufferGraphics.fillRect(490,60,10,420);
       bufferGraphics.fillRect(50,450,440,2);
    	
       //middle
    	
       bufferGraphics.setColor(Color.yellow); 	
       bufferGraphics.fillRect(50,70,440,380);
    	
   	   bufferGraphics.setColor(Color.white); 	
   	   //circles
     	
       for (int x = 0; x < 7; x++)     //seven columns
         for (int y = 0; y < 6; y++)   //six rows
           bufferGraphics.fillOval(65 + x * 60, 85 + y * 60, 50, 50); 

       
       bufferGraphics.setColor(Color.red); 
       bufferGraphics.fillOval(selectorX,selectorY,50,50);
       

    	
    	
    	
   	 // gridlines
   	//  setColor(Color.black); 	
    //	bufferGraphics.drawLine(50,100,480,100); 
    //	bufferGraphics.drawLine(50,160,480,160);
    //	bufferGraphics.drawLine(50,220,480,220); 
    //	bufferGraphics.drawLine(50,280,480,280);
    //	bufferGraphics.drawLine(50,340,480,340);
    //	
    //	bufferGraphics.drawLine(120,40,120,400); 
    //	bufferGraphics.drawLine(180,40,180,400); 
    //  bufferGraphics.drawLine(240,40,240,400); 
    //	bufferGraphics.drawLine(300,40,300,400); 
    //	bufferGraphics.drawLine(360,40,360,400); 
    //	bufferGraphics.drawLine(420,40,420,400); 
    
         g.drawImage(offscreen,0,0,540,500,null);
       
    
    }
     
    public void update(Graphics g) {
    
       
       g.drawImage(offscreen,0,0,540,500,null);
       
     

    
            
    	
    
    }
    
    public void stop() {
    	
    }

    public void destroy () {
    	
    
    }

     
  }
  

Any help is appreciated!!!!! [Edited by - BlackReign on January 12, 2005 6:41:58 AM]
BlackReign
BlackReign
And I know about the incredible inefficiency of my dropPiece method, I'll fix it later, unless the problem lies within it, which I don't think it is.
paulecoyote
paulecoyote
BlackReign > You wouldn't be going to Southampton Institute would you?
Edit> don't worry, I'm not a lecturer, just that code looks familar from way back when I was there and given this code like this as a project to fix...

[Edited by - paulecoyote on January 12, 2005 5:24:23 AM]
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
BlackReign
BlackReign
Quote:
Original post by capn_midnight
please use the "source" tags instead of the "code" tags.


No problem


Quote:
Original post by paulecoyote
BlackReign > You wouldn't be going to Southampton Institute would you?
Edit> don't worry, I'm not a lecturer, just that code looks familar from way back when I was there and given this code like this as a project to fix...


No, this is a highschool project(I guess it's purely coincedence). I want a good mark so instead of doing console programming I did an applet, which just happens to be a bit above my level.

I don't need a code fix just a suggestion of how to make dropPiece work when I press enter. I guess I'll continue searching google
paulecoyote
paulecoyote
If you make a button on your applet called "drop piece" and have it as having default input focus it should activate when you press enter.

The way you are doing it should in theory be sound, but be wary that there is an "enter" key and a "return" key. Enter is on the keypad, carriage return is under backspace. The two are different because you are sinking events (virtual keys) rather then the eventual output that may different.

Try switching on VK_D or something just to test that your drop piece works.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
BlackReign
BlackReign
Yeah, it doesn't work with D either, I guess it is code. Thanks anyway for the help!

EDIT: I tested dropPiece in a separate file and I figured out a potential problem, once I get it fixed hopefully it will work.

[Edited by - BlackReign on January 12, 2005 9:09:00 AM]
capn_midnight
capn_midnight
you can't store a reference to a graphics object like that. It's not guaranteed to remain valid, and usually ends up invalid.

You pass a Graphics object "g" to the dropPiece method, but never actually use it.

Applet has a method getGraphics() that will return a graphics object, THAT is how you will get your graphics object, and you will have to do it for each method. (actually, you shouldn't try to do any painting outside the paint method, unless those other methods are called by the paint method only).

you can really trim down the dropPiece method. For instance, in each of your if and "else if" blocks, you assign "count = 0; i = 0; positionofLastChip = 0;". Just do it once at the beginning of the method before the if blocks, and you won't have to do it.

in fact, the only difference I can see in the if blocks is this line
"bufferGraphics.fillOval(65,x,50,50);" where the value 65 changes for each block. Really, this value is selectorPos*60 + 5, meaning you can completely elliminate the need for all those if blocks
//the parameter Graphics g was never usedpublic void dropPiece() {    Graphics graph = this.getGraphics();    count = 0;    i = 0;    positionofLastChip = 0;    while (i!=1) {        i = col1[count++];        positionofLastChip = count ;        dropLevel=positionofLastChip + 1;        if (count > 6) {            dropLevel= 1;        }    }    x=445-(dropLevel*60);    graph.fillOval(selectorPos*60 + 5,x,50,50);              if (playerTurn == 1) {                  //select colour to paint circlebased on which players turn it is           playerColor = Color.red;        playerTurn=2;    }    else {        graph.setColor(Color.black);         playerTurn = 1;    }    repaint();}	

Oh, but look, you call repaint at the end, so anything that you painted in the dropPiece method gets painted over. You really should do all painting in the paint method, which also elliminates the need for storing copies of the graphics context, or retrieving them from the applet.

comment your code. always. it's not optional. You've got 3 or 4, that's nowhere near enough.

"i = col1[count++];"
don't increment a variable while still using it in another expression. Sloppy code, and in many cases undefined results.
capn_midnight
capn_midnight
This is the proper way to do double buffering.
java.awt.image.BufferStrategy bs;/** * double buffering elliminates unsightly flicker. *   */private void setupDoubleBuffering(){	try	{		//initializing a front and back buffer, both with hardware		// acceleration when possible.		this.createBufferStrategy(2, new BufferCapabilities(				new ImageCapabilities(true), new imageCapabilities(true),				null)); //double buffering		//and save a pointer to the buffer strategy for later use		bs = this.getBufferStrategy();	}	catch (Exception e)	{		//Since it's so important, don't allow the program to continue if		// it can't be used.		JOptionPane.showMessageDialog(null,				"Double buffering not setup, dieing now...");		System.exit(-1);	}}/** * paint the applet, using double buffering */public void paint(Graphics gUnused){    super.paint(gUnused); //paint the background    Graphics g=bs.getDrawGraphics();  //get the backbuffer    //do whatever drawing with g    //...    //...    bs.show(); //flip the buffers}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.