1 Replies - 1810 Views - Last Post: 14 May 2010 - 02:43 PM Rate Topic: -----

#1 santwana  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-April 10

Chess board Draggin help..

Posted 13 May 2010 - 12:31 PM

Hello everyone! I have arrived at a problem with my Chessboard.. I have implemented a dragging logic to drag my piece on the chess board by calling the repaint() method with updated x and y coordinates all through the mouseDragged event.

The tentative rule I have here is that the move of the bishop is legal as far as it is not in the same row or column.
The rule works very well when I drag and drop the piece at a square.But when I click a square, the move is detected as illegal but the piece changes its position. I just want the piece not to move even when I click anywhere on the Board. It only has to respond to the dragging.

Kindly forgive my poor and roughly written code.. i had to put many statements just to know the squares, old and new positions.i am attaching the image also.

Also.. the piece is not displayed at first when loaded.you need to click on square 0,0. Then an error message comes( I need to do a lot of cleaning up from the next time so that the code is not annoying..)

import java.awt.*;
import java.awt.Graphics.*;
import java.awt.event.*;
import javax.swing.*;
//import javax.imageio.*;
//import java.io.*;
public class Checker extends javax.swing.JPanel {
    private int Xpos,Ypos;
   int initposX=0,initposY=0;
   int currposx=0,currposy=0;
     String position="start",old="start";
     Image img;
 int occupied[][]=new int[8][8];
 int oldX,oldY;
 int newX,newY;
 int numclicks;    
    boolean dragging,released,pressed=false;
public Checker() 
{
 initComponents();
 img=Toolkit.getDefaultToolkit().getImage("bb.png");
        setPreferredSize(new Dimension(400,400));
        setMinimumSize(getPreferredSize());
        numclicks=0;
        int w=getWidth();
        int h=getHeight();
         repaint();
        
       
        addMouseListener(
                new MouseAdapter()
        {
           public void mouseClicked(MouseEvent me)
            {
              int x=me.getX();
              int y=me.getY();
              getsquareX(x);
              getsquareY(y);
              newX=Xpos;
              newY=Ypos;
              mouseReleased(me);
           }
  //End MouseClicked
         
           public void mousePressed(MouseEvent me)
           {
            dragging=true;
            int x=me.getX();
            int y=me.getY();
            getsquareX(x);
            getsquareY(y);
            initposX=oldX;
            initposY=oldY;
            old="Your old position is"+oldX+","+oldY;
            oldX=Xpos;
            oldY=Ypos;
            
              
            position="Your mouse pressed at square ["+oldX+","+oldY+"]";
            
            
            //repaint(); 
          //JOptionPane.showMessageDialog(null,position);
           }//End MousePressed
 
          public void mouseReleased(MouseEvent me)
           {
             released=true;
             dragging=false;
            int x=me.getX();
            x+=10;
            int y=me.getY();
            y+=10;
            getsquareX(x);
            getsquareY(y);
            newX=Xpos;
            newY=Ypos;
            if(newX>=10000||newY>=10000)
            {
             newX=oldX;
             newY=oldY;
             }
            position="Your mouse released at square ["+newX+","+newY+"]";
             
             
            if(checkLegalMove(oldX,oldY,newX,newY))
             repaint();
            else
             {
             newX=oldX;
             newY=oldY;
             repaint();
            }
             
          JOptionPane.showMessageDialog(null,"Old Position"+oldX+" "+oldY+"New Position"+newX+" "+newY);
          }

           
        }//End MouseAdapter 
  );//End addMouseListener

   addMouseMotionListener(
            new MouseMotionAdapter()
 {
       public void mouseDragged(MouseEvent me)
      {
        pressed=true;
        int x=me.getX();
        int y=me.getY();
        currposx=x;
        currposy=y;
        position="Your mouse dragging at"+currposx+","+currposy;
       if(dragging==true)
        repaint();
      }//End mouseDragged
}//end MouseMotion Adapter
);

 }//End Constructor


      public void getsquareX(int x)
      {
        final int stepx=50;
           if(x>0&&x<stepx)
               Xpos=0;
           else if(x>stepx&&x<(2*stepx))
               Xpos=1;
           else if(x>(2*stepx)&&x<(3*stepx))
               Xpos=2;
          else if(x>(3*stepx)&&x<(4*stepx))
               Xpos=3;
          else if(x>(4*stepx)&&x<(5*stepx))
               Xpos=4;
           else if(x>(5*stepx)&&x<(6*stepx))
               Xpos=5;
           else if(x>(6*stepx)&&x<(7*stepx))
               Xpos=6;
           else if(x>(7*stepx)&&x<(8*stepx))
                Xpos=7;
           else Xpos=10000;
     }
     public void getsquareY(int y)
     {
        final int stepy=50;
           if(y>0&&y<=stepy)
               Ypos=0;
           else if(y>stepy&&y<(2*stepy))
               Ypos=1;
           else if(y>(2*stepy)&&y<(3*stepy))
               Ypos=2;
           else if(y>(3*stepy)&&y<(4*stepy))
               Ypos=3;
           else if(y>(4*stepy)&&y<(5*stepy))
               Ypos=4;
           else if(y>(5*stepy)&&y<(6*stepy))
               Ypos=5;
           else if(y>(6*stepy)&&y<(7*stepy))
               Ypos=6;
           else if(y>(7*stepy)&&y<(8*stepy))
                Ypos=7;
           else Ypos=10000;
      }

public boolean checkLegalMove(int x1,int y1,int x2,int y2)
{
 
  if(!(x1==x2||y1==y2))
   {
    newX=x2;
    newY=y2;
    return true;
   }
   else
   {
     JOptionPane.showMessageDialog(this,"Illegal move, Bishop Can move only in diagonal direction");
     newX=oldX;
     newY=oldY;
     repaint();
     return false;
    
   }
 }
    
    
 @Override
public void paintComponent(Graphics g)
{
     
   super.paintComponent(g);
   
    int w=getWidth();
        int h=getHeight();
    int divx=w/8;
        int divy=h/8;
        

    g.setColor(Color.RED);
    for(int i=0;i<8;i=i+2)
        for(int j=0;j<8;j=j+2)
        {
            g.fillRect(j*divx,i*divy,divx,divy);
            g.fillRect((1+j)*divx,(1+i)*divy,divx,divy);
        }
    if(dragging) 
    g.drawImage(img,currposx,currposy,50,50,this);
   else if(released==true)
    g.drawImage(img,newX*50,newY*50,50,50,this);
   g.setColor(Color.BLACK);
   g.drawString(position,currposx,currposy);
   g.drawString(old,currposx+10,currposy+10);
     
    

}
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>                        


    // Variables declaration - do not modify                     
    // End of variables declaration                   

}



the TestChecker code
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author swan
 */
import java.awt.*;
import javax.swing.*;
public class TestChecker {
public static void main(String args[])
{
    JFrame chess=new JFrame("The Checker board for chess game");
    
    Checker c=new Checker();
    
    
    
    c.setBackground(Color.WHITE);
   
    chess.add(c);
    chess.pack();
    chess.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    
    chess.setVisible(true);
}
}



Please help me.. I have no instructor at college to help with my code, that is the reason why I am posting so many questions about this project of mine. :dontgetit:

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Chess board Draggin help..

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: Chess board Draggin help..

Posted 14 May 2010 - 02:43 PM

I haven't looked the rest of your code yet, but I do know exactly what's wrong with your image. If you load it this way:
 img=Toolkit.getDefaultToolkit().getImage("bb.png");



Then you also have to prepare the image for display (repaint() does this the first time around if not done already, thus why it takes 2 repaints to show). Use the prepareImage() method in the java.awt.Component class:
prepareImage()

 img=Toolkit.getDefaultToolkit().getImage("bb.png");
 if (!this.prepareImage(img, null))
     System.err.println("Image not successfully loaded");


This post has been edited by Dogstopper: 14 May 2010 - 02:45 PM
Reason for edit:: double post/...

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1