Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (763)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (852)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: xxMrPHDxx (posted 2017-12-28 18:11:33, viewed 308 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136  
package com.chess;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.chess.engine.Alliance;
import com.chess.engine.board.Board;
import com.chess.engine.board.BoardUtils;
import com.chess.engine.pieces.Bishop;
import com.chess.engine.pieces.King;
import com.chess.engine.pieces.Knight;
import com.chess.engine.pieces.Pawn;
import com.chess.engine.pieces.Piece;
import com.chess.engine.pieces.Queen;
import com.chess.engine.pieces.Rook;

public class JChess extends JPanel implements MouseListener {

   private static final long serialVersionUID = 1L;
   
   public static final Map<Piece,BufferedImage> CACHE_IMAGE = loadImages();

   public static final Color LIGHT = Color.decode("#efefef");
   public static final Color DARK = Color.decode("#1f1f1f");

   public static final int WIDTH = 500, HEIGHT = 500;
   public static final int TILE_WIDTH = WIDTH / BoardUtils.NUM_TILES_PER_ROW;
   public static final int TILE_HEIGHT = HEIGHT / BoardUtils.NUM_TILES_PER_ROW;

   private JFrame frame = new JFrame("JChess");
   private Board board;

   private BufferedImage image;

   public JChess() {
      board = Board.createStandardBoard();

      setPreferredSize(new Dimension(WIDTH, HEIGHT));
      addMouseListener(this);
      frame.setResizable(false);
      frame.add(this);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      frame.pack();
      validate();

      image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
      image.createGraphics();

      while (true) {
         draw();
         drawToScreen();
      }
   }

   public static void main(String[] args) {
      new JChess();
   }

   private static Map<Piece,BufferedImage> loadImages() {
      final Map<Piece,BufferedImage> images = new HashMap<>();
      Arrays.asList(Alliance.values()).forEach(ally -> {
         Arrays.asList(new Rook(ally,-1),new Knight(ally,-1),new Bishop(ally,-1),new King(ally,-1),new Queen(ally,-1),new Pawn(ally,-1)).forEach(piece -> {
            final String filename = "art/pieces/plain/" + piece.getIconName() + ".gif";
            try {
               images.put(piece, ImageIO.read(new File(filename)));
            } catch (IOException e) {
               System.out.println(filename + " not found!");
            }
         });
      });
      return Collections.unmodifiableMap(images);
   }

   public void draw() {
      Graphics g = image.getGraphics();

      board.getAllTiles().forEach(tile -> {
         if (tile.getRow() % 2 == 0) {
            g.setColor(tile.getCol() % 2 == 0 ? LIGHT : DARK);
            g.fillRect(TILE_WIDTH * tile.getRow(), TILE_HEIGHT * tile.getCol(), TILE_WIDTH, TILE_HEIGHT);
         } else {
            g.setColor(tile.getCol() % 2 != 0 ? LIGHT : DARK);
            g.fillRect(TILE_WIDTH * tile.getRow(), TILE_HEIGHT * tile.getCol(), TILE_WIDTH, TILE_HEIGHT);
         }
         
         if(!tile.isOccupied()) return;
         
          final Piece piece = tile.getPiece();
          g.drawImage(CACHE_IMAGE.get(piece),TILE_WIDTH * tile.getRow(),
          TILE_HEIGHT * tile.getCol(),null);
      });

      g.drawImage(CACHE_IMAGE.get(new Rook(Alliance.BLACK, -1)), 100, 100, null);
   }

   public void drawToScreen() {
      Graphics g2 = getGraphics();

      g2.drawImage(image, 0, 0, null);

      g2.dispose();
   }

   public void mouseClicked(MouseEvent e) {

   }

   public void mouseEntered(MouseEvent e) {
   }

   public void mouseExited(MouseEvent e) {
   }

   public void mousePressed(MouseEvent e) {
   }

   public void mouseReleased(MouseEvent e) {
   }

}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
EgonOlsen (489 views)
2018-06-10 19:43:48

EgonOlsen (553 views)
2018-06-10 19:43:44

EgonOlsen (405 views)
2018-06-10 19:43:20

DesertCoockie (701 views)
2018-05-13 18:23:11

nelsongames (1013 views)
2018-04-24 18:15:36

nelsongames (979 views)
2018-04-24 18:14:32

ivj94 (1503 views)
2018-03-24 14:47:39

ivj94 (556 views)
2018-03-24 14:46:31

ivj94 (1244 views)
2018-03-24 14:43:53

Solater (559 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!