0

I am trying to build a java program that allows a user to play checkers. Right now I'm having trouble creating my 24 pieces & getting them to move from place to place. I want to be able to drag them from an orange square to another orange square. If the square isn't orange, I want it the program to reject the move. How can I go about doing this? Any & all help is appreciated!

import java.awt.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class Board extends JPanel{

public void paint(Graphics g){

    g.setColor(new Color(234, 106,32));
    g.fillRect(0, 0, 400, 400);
    for(int i = 0; i <= 350; i+=100){
        for(int j = 0; j <= 350; j+=100){
            g.clearRect(i, j, 50, 50);
        }
    }

    for(int i = 50; i <= 400; i+=100){
        for(int j = 50; j <= 400; j+=100){
            g.clearRect(i, j, 50, 50);
        }
    }
}
public static void main(String[] args){
    JFrame frame = new JFrame();
    frame.setSize(400,420);
    frame.getContentPane().add(new Board());
    frame.setTitle("Java Chip Checkers");
    frame.setLocationRelativeTo(null);
 /* frame.setBackground(new Color(234, 106, 32)); */
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
 }
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
joshman
  • 11
  • 2
  • You're question is not simple in that its solution involves many steps, and the best solution is to break down the bigger steps into smaller steps and then try to solve each small step, one at a time, often in isolation of all the other steps. You can also borrow ideas from other similar questions and answers, such as [my answer here](http://stackoverflow.com/a/4687759/522444). – Hovercraft Full Of Eels Sep 16 '15 at 01:45
  • As a side note, you'll want to read the tutorial on Swing graphics since you're not doing it right. You would want to override paintComponent and to not forget to call the super method, but having said that, I wouldn't use paint or paintComponent but rather ImageIcons held by JLabels as per my example above. – Hovercraft Full Of Eels Sep 16 '15 at 01:46
  • Also see [this similar answer](http://stackoverflow.com/a/4894516/522444). – Hovercraft Full Of Eels Sep 16 '15 at 01:48
  • Here is a simple example that shows how to drag labels containing an image around a chessboard: http://stackoverflow.com/questions/6811247/drawing-in-jlayeredpane-over-exising-jpanels/6811800#6811800 – camickr Sep 16 '15 at 03:36

1 Answers1

0

If it is not a training project to study JPanel, i recommend using a framework specifically designed for games. (libgdx eg) Otherwise, you have to write a lot of unnecessary interactions.

You need a binary matrix and translation of the mouse coordinates in the matrix. Then you will be easy to calculate the actions of the user.

But your question is not very suitable for stackoverflow.

user2413972
  • 1,355
  • 2
  • 9
  • 25