Here i draw chessBoard and adjust affect of appearin blue rect when player press by mouse on the above two rows
but the pawn image not appear .

I made intializingIcoss() on class BoardGame then the
method appear but when the image not appear when i put the method on class Pieces ....
please any one guide me
The code
-----------------------

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;

public class MovePawnTest extends JFrame {

    private BoardGame board;
    private Pieces pieces;

    public MovePawnTest() {
        board = new BoardGame();
        pieces = new Pieces(board);
        board.add(pieces);
        getContentPane().add(board, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        MovePawnTest movePawnTest = new MovePawnTest();
        movePawnTest.setDefaultCloseOperation(MovePawnTest.EXIT_ON_CLOSE);
        movePawnTest.setSize(new Dimension(600, 300));
        movePawnTest.setLocationRelativeTo(null);
        movePawnTest.setVisible(true);
        movePawnTest.setExtendedState(MAXIMIZED_BOTH);

    }
}
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;

public class BoardGame extends JPanel {

    private final int ROW = 8, COL = 8, WIDTH_SQUARE = 70, HEIGHT_SQUARE = 70;
    private int x = 70, y = 70, xStart, yStart, xEnd, yEnd;
    private boolean affectColor = false, rangeXY = false;
    private MyMouse myMouse;

    public BoardGame() {
        myMouse = new MyMouse();
        addMouseListener(myMouse);
        addMouseMotionListener(myMouse);
    }

    public int getROW() {
        return ROW;
    }

    public int getWIDTH_SQUARE() {
        return WIDTH_SQUARE;
    }

    public int getCOL() {
        return COL;
    }

    public int getHEIGHT_SQUARE() {
        return HEIGHT_SQUARE;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        drawingBoard(g2d);
        coloreingBoard(g2d);
        if (affectColor) {
            Stroke stroke = new BasicStroke(5.0f);
            g2d.setColor(Color.BLUE);
            g2d.setStroke(stroke);
            if (rangeXY) {
                affectOnSquares(g2d, xStart, yStart);
            }
        }
    }

    public void affectOnSquares(Graphics2D g2d, int xStart, int yStart) {
        int col = (xStart - x) / WIDTH_SQUARE;
        int row = (yStart - y) / HEIGHT_SQUARE;
        g2d.drawRect(x + col * WIDTH_SQUARE, y + row * HEIGHT_SQUARE,
                WIDTH_SQUARE, HEIGHT_SQUARE);
    }

    private void drawingBoard(Graphics g) {
        for (int i = 0; i <= ROW; ++i) {
            for (int j = 0; j <= COL; ++j) {
                g.drawLine((x * i) + WIDTH_SQUARE, (y * j) + HEIGHT_SQUARE, (x * i) + WIDTH_SQUARE, y * 9);
                g.drawLine(x, (y * j) + HEIGHT_SQUARE, x * 9, (y * j) + HEIGHT_SQUARE);
            }
        }
    }

    private void coloreingBoard(Graphics g) {
        for (int row = 0; row < 8; row++) {
            for (int col = 0; col < 8; col++) {
                if ((row + col) % 2 == 0) {  //light square
                    g.setColor(Color.white);
                } else {                     //dark square
                    g.setColor(Color.black);
                }
                g.fillRect(x + col * WIDTH_SQUARE, y + row * HEIGHT_SQUARE,
                        WIDTH_SQUARE, HEIGHT_SQUARE);
            }
        }
    }

    private class MyMouse extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {
            xStart = e.getX();
            yStart = e.getY();
            if (xStart < x || xStart > (x + 8 * WIDTH_SQUARE) ||
                    yStart < y || yStart > (y + 2 * HEIGHT_SQUARE)) {
                rangeXY = false;
            } else {
                rangeXY = true;
            }
            affectColor = true;
            repaint();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            xEnd = e.getX();
            yEnd = e.getY();
            affectColor = false;
            repaint();
        }
    }
}
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Pieces extends JPanel {

    private String[] imageNames = {"bluePawn.gif"};
    private ImageIcon[] imageIcons;
    BoardGame b;

    public Pieces(BoardGame boardGame) {
        b = boardGame;
    }


    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        intializeingIcons((Graphics2D) g);
    }

    public void intializeingIcons(Graphics2D g2d) {
        int xPawn = b.getCOL() * b.getHEIGHT_SQUARE();
        int yPawn = b.getROW() * b.getHEIGHT_SQUARE();
        imageIcons = new ImageIcon[imageNames.length];
        for (int i = 0; i < imageNames.length; ++i) {
            imageIcons[i] = new ImageIcon(getClass().getResource(imageNames[i]));
            g2d.drawImage(imageIcons[i].getImage(), xPawn, yPawn - b.getHEIGHT_SQUARE(), null);
        }
    }
}

Thanks

Hi
let s focus on your intializeingIcons funtion;
let say s0 and s defined as follow
String s0=imageNames[0];
URL s=getClass().getResource(s0);
after too instructions s have a null value.
that is the source of the bug.
maybe the file you reffered to does not exist or it is not known.

So it is the best to put intializing method in calss BoardGame and delete this

pieces = new Pieces(board);
board.add(pieces);

from MovePawnTest class
if you mean thing or you can adjust this problem by putting intializeingIcons() in class pieces see us how you will do this ??

Thanks
---------------------

This article has been dead for over six months. Start a new discussion instead.