Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Fixing my Chess Program setup

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Fixing my Chess Program setup

    Hi there, I am making my chess game in Java. I have designed the Board and have all the pieces placed and squares colored, but I need to figure out how to do two things, firstly how do I get the mouse to drag and move all of my Sprites/Images, then how do I make my code more efficient? I am individually coloring the Black and White tiles but I am pretty sure there is a more efficient way to do that.

    Thanks Totara

    Board Class

    import java.awt.Color;
    import java.awt.Graphics;
     
    import javax.swing.JComponent;
     
    import com.teamfission.chess.References;
     
    public class Board extends JComponent {
     
    	public void paint(Graphics g) {
    		References ref = new References();
    		Color blackTile = new Color(210, 180, 140);
    		Color whiteTile = new Color(255, 255, 255);
     
    		g.setColor(Color.gray);
    		g.fillRect(0, 0, ref.width, ref.height);
     
    		if (ref.debugMode) {
    			g.setColor(Color.green);
    			g.drawRect(0, 0, 60, 60);
    			g.drawRect(540, 0, 60, 60);
    			g.drawRect(540, 540, 60, 60);
    			g.drawRect(0, 540, 60, 60);
    		}
     
    		/** Board Squares */
    		// White
    		g.setColor(whiteTile);
    		// Row 1
    		g.fillRect(60, 60, 60, 60);
    		g.fillRect(180, 60, 60, 60);
    		g.fillRect(300, 60, 60, 60);
    		g.fillRect(420, 60, 60, 60);
    		// Row 2
    		g.fillRect(120, 120, 60, 60);
    		g.fillRect(240, 120, 60, 60);
    		g.fillRect(360, 120, 60, 60);
    		g.fillRect(480, 120, 60, 60);
    		// Row 3
    		g.fillRect(60, 180, 60, 60);
    		g.fillRect(180, 180, 60, 60);
    		g.fillRect(300, 180, 60, 60);
    		g.fillRect(420, 180, 60, 60);
    		// Row 4
    		g.fillRect(120, 240, 60, 60);
    		g.fillRect(240, 240, 60, 60);
    		g.fillRect(360, 240, 60, 60);
    		g.fillRect(480, 240, 60, 60);
    		// Row 5
    		g.fillRect(60, 300, 60, 60);
    		g.fillRect(180, 300, 60, 60);
    		g.fillRect(300, 300, 60, 60);
    		g.fillRect(420, 300, 60, 60);
    		// Row 6
    		g.fillRect(120, 360, 60, 60);
    		g.fillRect(240, 360, 60, 60);
    		g.fillRect(360, 360, 60, 60);
    		g.fillRect(480, 360, 60, 60);
    		// Row 7
    		g.fillRect(60, 420, 60, 60);
    		g.fillRect(180, 420, 60, 60);
    		g.fillRect(300, 420, 60, 60);
    		g.fillRect(420, 420, 60, 60);
    		// Row 8
    		g.fillRect(120, 480, 60, 60);
    		g.fillRect(240, 480, 60, 60);
    		g.fillRect(360, 480, 60, 60);
    		g.fillRect(480, 480, 60, 60);
     
    		// Black
    		g.setColor(blackTile);
    		// Row 1
    		g.fillRect(120, 60, 60, 60);
    		g.fillRect(240, 60, 60, 60);
    		g.fillRect(360, 60, 60, 60);
    		g.fillRect(480, 60, 60, 60);
    		// Row 2
    		g.fillRect(60, 120, 60, 60);
    		g.fillRect(180, 120, 60, 60);
    		g.fillRect(300, 120, 60, 60);
    		g.fillRect(420, 120, 60, 60);
    		// Row 3
    		g.fillRect(120, 180, 60, 60);
    		g.fillRect(240, 180, 60, 60);
    		g.fillRect(360, 180, 60, 60);
    		g.fillRect(480, 180, 60, 60);
    		// Row 4
    		g.fillRect(60, 240, 60, 60);
    		g.fillRect(180, 240, 60, 60);
    		g.fillRect(300, 240, 60, 60);
    		g.fillRect(420, 240, 60, 60);
    		// Row 5
    		g.fillRect(120, 300, 60, 60);
    		g.fillRect(240, 300, 60, 60);
    		g.fillRect(360, 300, 60, 60);
    		g.fillRect(480, 300, 60, 60);
    		// Row 6
    		g.fillRect(60, 360, 60, 60);
    		g.fillRect(180, 360, 60, 60);
    		g.fillRect(300, 360, 60, 60);
    		g.fillRect(420, 360, 60, 60);
    		// Row 7
    		g.fillRect(120, 420, 60, 60);
    		g.fillRect(240, 420, 60, 60);
    		g.fillRect(360, 420, 60, 60);
    		g.fillRect(480, 420, 60, 60);
    		// Row 8
    		g.fillRect(60, 480, 60, 60);
    		g.fillRect(180, 480, 60, 60);
    		g.fillRect(300, 480, 60, 60);
    		g.fillRect(420, 480, 60, 60);
     
    		g.setColor(Color.black);
    		for (int x = 0; x < 8; x++) {
    			for (int y = 0; y < 8; y++) {
    				g.drawRect(60 + (x * 60), 60 + (y * 60), 60, 60);
    			}
    		}
     
    		/** Pieces */
    		// White
    		g.drawImage(SpriteLoader.rook, 60, 60, 60, 60, null);
    		g.drawImage(SpriteLoader.rook, 480, 60, 60, 60, null);
    		g.drawImage(SpriteLoader.horse, 120, 60, 60, 60, null);
    		g.drawImage(SpriteLoader.horse, 420, 60, 60, 60, null);
    		g.drawImage(SpriteLoader.bishop, 180, 60, 60, 60, null);
    		g.drawImage(SpriteLoader.bishop, 360, 60, 60, 60, null);
    		g.drawImage(SpriteLoader.king, 240, 60, 60, 60, null);
    		g.drawImage(SpriteLoader.queen, 300, 60, 60, 60, null);
     
    		// Black
    		g.drawImage(SpriteLoader.rookBlack, 60, 480, 60, 60, null);
    		g.drawImage(SpriteLoader.rookBlack, 480, 480, 60, 60, null);
    		g.drawImage(SpriteLoader.horseBlack, 120, 480, 60, 60, null);
    		g.drawImage(SpriteLoader.horseBlack, 420, 480, 60, 60, null);
    		g.drawImage(SpriteLoader.bishopBlack, 180, 480, 60, 60, null);
    		g.drawImage(SpriteLoader.bishopBlack, 360, 480, 60, 60, null);
    		g.drawImage(SpriteLoader.kingBlack, 240, 480, 60, 60, null);
    		g.drawImage(SpriteLoader.queenBlack, 300, 480, 60, 60, null);
     
    		for (int x = 0; x < 8; x++) {
    			g.drawImage(SpriteLoader.pawn, 60 + (x * 60), 120, 60, 60, null);
    			g.drawImage(SpriteLoader.pawnBlack, 60 + (x * 60), 420, 60, 60, null);
    		}
    	}
    }

    SpriteLoader Class

    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
     
    import com.teamfission.chess.References;
     
    public class SpriteLoader {
     
    	/** White Pieces */
    	public static BufferedImage pawn;
    	public static BufferedImage rook;
    	public static BufferedImage horse;
    	public static BufferedImage bishop;
    	public static BufferedImage king;
    	public static BufferedImage queen;
     
    	/** Black Pieces */
    	public static BufferedImage pawnBlack;
    	public static BufferedImage rookBlack;
    	public static BufferedImage horseBlack;
    	public static BufferedImage bishopBlack;
    	public static BufferedImage kingBlack;
    	public static BufferedImage queenBlack;
     
    	public static void loadImages() {
    		References ref = new References();
    		try {
    			pawn = ImageIO.read(new File("res/Pawn.png"));
    			rook = ImageIO.read(new File("res/Rook.png"));
    			horse = ImageIO.read(new File("res/Horse.png"));
    			bishop = ImageIO.read(new File("res/Bishop.png"));
    			king = ImageIO.read(new File("res/King.png"));
    			queen = ImageIO.read(new File("res/Queen.png"));
     
    			pawnBlack = ImageIO.read(new File("res/PawnBlack.png"));
    			rookBlack = ImageIO.read(new File("res/RookBlack.png"));
    			horseBlack = ImageIO.read(new File("res/HorseBlack.png"));
    			bishopBlack = ImageIO.read(new File("res/BishopBlack.png"));
    			kingBlack = ImageIO.read(new File("res/KingBlack.png"));
    			queenBlack = ImageIO.read(new File("res/QueenBlack.png"));
     
    		} catch (IOException e) {
    			System.out.println(ref.consolePrefix + "Sprite couldn't be loaded");
    		}
     
    	}
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,122
    My Mood
    Mellow
    Thanks
    186
    Thanked 658 Times in 645 Posts

    Default Re: Fixing my Chess Program setup

    Yes, there are more efficient ways. Do you know arrays or any other collection that could be iterated?

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Fixing my Chess Program setup

    Quote Originally Posted by GregBrannon View Post
    Do you know arrays or any other collection that could be iterated?
    I have a basic understanding of arrays, but I'm not sure how they could be iterated. Could you please explain how I would iterate it? Or what method is the best way?

    Thanks,

    Totara

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Fixing my Chess Program setup

    Also, does anyone know how I can make the .png's move, and according to where you placed them, snap to the grid squares?

Similar Threads

  1. Chess Program
    By gokuball in forum Java Theory & Questions
    Replies: 218
    Last Post: August 19th, 2013, 04:47 PM
  2. Fixing this grade finder program?
    By Stinger25 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: December 4th, 2012, 07:10 AM
  3. Simple chess program (no AI) - Need help with structuring my code
    By MineeMo in forum Object Oriented Programming
    Replies: 6
    Last Post: June 18th, 2012, 10:12 AM
  4. Help fixing my first program
    By javadude in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2012, 05:52 PM
  5. Simple Chess program
    By x3rubiachica3x in forum What's Wrong With My Code?
    Replies: 23
    Last Post: September 22nd, 2010, 11:12 AM