Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please am developing a chess game using Java. I am having problems trying to display captured chess pieces, this pieces are held in a bufferedImage. when i run and print the variable of the bufferedImage it isn't null and doesn't display the image on the JPanel.

In addition my list of captured chess piece stored in a "List capturedPiece" and is working fine, also the paths to the images are correct. The only problem is that the image stored on the bufferedImage doesn't displaying.

Here is my code

BrownPlayerPanel class:
Java
public class BrownPlayerPanel extends JPanel {

private Rectangle yellowPawnRect, yellowKnightRect, yellowBishopRect,
        yellowRookRect, yellowQueenRect;
private Rectangle brownPawnRect, brownKnightRect, brownBishopRect,
        brownRookRect, brownQueenRect;
private List<Piece> capturedPieces;
BufferedImage figurineLayer, counterLayer;
boolean firstPaint = true;
private ImageFactory fact;

BufferedImage piecetest;

public BrownPlayerPanel() {
    initComponents();
    fact = new ImageFactory();

}

public void setCapturedPieces(List<Piece> piece) {
    capturedPieces = piece;

    System.out.println(capturedPieces);


    if (counterLayer != null) {
        clearBufferedImage(counterLayer);
    }

    if (figurineLayer != null) {
        clearBufferedImage(figurineLayer);
    }

    updateLayers();
    repaint();
}

private void initRects(int width, int height) {
    int gridWidth = width / 5;
    int gridHeight = height / 2;

    yellowPawnRect = new Rectangle(0, 0, gridWidth, gridHeight);
    yellowKnightRect = new Rectangle(gridWidth, 0, gridWidth, gridHeight);
    yellowBishopRect = new Rectangle(gridWidth * 2, 0, gridWidth, gridHeight);
    yellowRookRect = new Rectangle(gridWidth * 3, 0, gridWidth, gridHeight);
    yellowQueenRect = new Rectangle(gridWidth * 4, 0, gridWidth, gridHeight);

    brownPawnRect = new Rectangle(0, gridHeight, gridWidth, gridHeight);
    brownKnightRect = new Rectangle(gridWidth, gridHeight, gridWidth,gridHeight);
    brownBishopRect = new Rectangle(gridWidth * 2, gridHeight, gridWidth,gridHeight);
    brownRookRect = new Rectangle(gridWidth * 3, gridHeight, gridWidth,gridHeight);
    brownQueenRect = new Rectangle(gridWidth * 4, gridHeight, gridWidth,gridHeight);

}

public void clearBufferedImage(BufferedImage image) {
    Graphics2D g2d = image.createGraphics();
    g2d.setComposite(AlphaComposite.Src);
    g2d.setColor(new Color(0, 0, 0, 0));
    g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
    g2d.dispose();
}

private void updateLayers() {
    int yPC = 0, yNC = 0, yBC = 0, yRC = 0, yQC = 0;
    int bPC = 0, bNC = 0, bBC = 0, bRC = 0, bQC = 0;

    if (firstPaint) {
        int width = this.getWidth();
        int height = this.getHeight();

        figurineLayer = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
        counterLayer = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);

        capturedPieces = new ArrayList<Piece>();

        initRects(width, height);

        firstPaint = false;
    }

    int width = this.getWidth();
    int height = this.getHeight();
    int gridWidth = width / 5;

    for (Piece piece : capturedPieces) {
        if (piece.getColor() == Piece.YELLOW_COLOR) {
            if (piece.getType() == Piece.TYPE_PAWN) {
                drawImageIntoLayer(figurineLayer, ImageFactory.getFigurineImage(Piece.YELLOW_COLOR, Piece.TYPE_PAWN, gridWidth),yellowPawnRect);
                yPC++;
            }

            if (piece.getType() == Piece.TYPE_KNIGHT) {
                drawImageIntoLayer(figurineLayer, ImageFactory.getFigurineImage(Piece.YELLOW_COLOR,Piece.TYPE_KNIGHT, gridWidth),yellowKnightRect);
                yNC++;
            }

            if (piece.getType() == Piece.TYPE_BISHOP) {
                drawImageIntoLayer(figurineLayer, ImageFactory.getFigurineImage(Piece.YELLOW_COLOR,Piece.TYPE_BISHOP, gridWidth),yellowBishopRect);
                yBC++;
            }

            if (piece.getType() == Piece.TYPE_ROOK) {
                drawImageIntoLayer(figurineLayer, ImageFactory.getFigurineImage(Piece.YELLOW_COLOR, Piece.TYPE_ROOK, gridWidth), yellowRookRect);
                yRC++;
            }

            if (piece.getType() == Piece.TYPE_QUEEN) {
                drawImageIntoLayer(figurineLayer, ImageFactory.getFigurineImage(Piece.YELLOW_COLOR,Piece.TYPE_QUEEN, gridWidth), yellowQueenRect);
                yQC++;
            }
        }



        if (piece.getColor() == Piece.BROWN_COLOR) {
            if (piece.getType() == Piece.TYPE_PAWN) {
                drawImageIntoLayer(figurineLayer,ImageFactory.getFigurineImage(Piece.BROWN_COLOR, Piece.TYPE_PAWN, gridWidth),brownPawnRect);
                bPC++;
            }

            if (piece.getType() == Piece.TYPE_KNIGHT) {
                drawImageIntoLayer(figurineLayer, ImageFactory.getFigurineImage(Piece.BROWN_COLOR,Piece.TYPE_KNIGHT, gridWidth),brownKnightRect);
                bNC++;
            }

            if (piece.getType() == Piece.TYPE_BISHOP) {
                drawImageIntoLayer(figurineLayer, ImageFactory.getFigurineImage(Piece.BROWN_COLOR,Piece.TYPE_BISHOP, gridWidth),brownBishopRect);
                bBC++;
            }

            if (piece.getType() == Piece.TYPE_ROOK) {
                drawImageIntoLayer(figurineLayer, ImageFactory.getFigurineImage(Piece.BROWN_COLOR, Piece.TYPE_ROOK, gridWidth),brownRookRect);
                bRC++;
            }

            if (piece.getType() == Piece.TYPE_QUEEN) {
                drawImageIntoLayer(figurineLayer, ImageFactory.getFigurineImage(Piece.BROWN_COLOR,Piece.TYPE_QUEEN, gridWidth), brownQueenRect);
                bQC++;
            }
        }
    }

    if (yPC > 1) {
        drawCounterIntoLayer(counterLayer, yPC, yellowPawnRect);
    }
    if (yNC > 1) {
        drawCounterIntoLayer(counterLayer, yNC, yellowKnightRect);
    }
    if (yBC > 1) {
        drawCounterIntoLayer(counterLayer, yBC, yellowBishopRect);
    }
    if (yRC > 1) {
        drawCounterIntoLayer(counterLayer, yRC, yellowRookRect);
    }
    if (yQC > 1) {
        drawCounterIntoLayer(counterLayer, yQC, yellowQueenRect);
    }

    if (bPC > 1) {
        drawCounterIntoLayer(counterLayer, bPC, brownPawnRect);
    }
    if (bNC > 1) {
        drawCounterIntoLayer(counterLayer, bNC, brownKnightRect);
    }
    if (bBC > 1) {
        drawCounterIntoLayer(counterLayer, bBC, brownBishopRect);
    }
    if (bRC > 1) {
        drawCounterIntoLayer(counterLayer, bRC, brownRookRect);
    }
    if (bQC > 1) {
        drawCounterIntoLayer(counterLayer, bQC, brownQueenRect);
    }
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(figurineLayer, 10,10, this);
    g2d.drawImage(counterLayer, null, this);

}

private void drawCounterIntoLayer(BufferedImage canvas, int count,Rectangle whereToDraw) {
    Graphics2D g2d = canvas.createGraphics();
    g2d.setFont(new Font("Arial", Font.BOLD, 12));
    g2d.setColor(Color.black);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    FontMetrics textMetrics = g2d.getFontMetrics();
    String countString = Integer.toString(count);
    int xCoord = whereToDraw.x + whereToDraw.width - textMetrics.stringWidth(countString);
    int yCoord = whereToDraw.y + whereToDraw.height;
    g2d.drawString(countString, xCoord, yCoord);
    g2d.dispose();
}

private void drawImageIntoLayer(BufferedImage canvas, BufferedImage item,Rectangle r) {
    Graphics2D g2d = canvas.createGraphics();
    g2d.drawImage(item, r.x, r.y, r.width, r.height, this);
    g2d.dispose();



}




private void initComponents() {
    setName("Form");
    setBounds(0, 0, 700, 450);
    setBorder(new LineBorder(new Color(139, 69, 19)));
    GroupLayout groupLayout = new GroupLayout(this);
    groupLayout.setHorizontalGroup(
        groupLayout.createParallelGroup(Alignment.LEADING)
            .addGap(0, 698, Short.MAX_VALUE)
    );
    groupLayout.setVerticalGroup(
        groupLayout.createParallelGroup(Alignment.LEADING)
            .addGap(0, 448, Short.MAX_VALUE)
    );
    setLayout(groupLayout);

    //setBackground(Color.black);

}



ImageFactory class:

Java
public class ImageFactory {

private static BufferedImage yellowPawnImage, yellowRookImage, yellowBishopImage, yellowKnightImage,
               yellowQueenImage, yellowKingImage;
private static BufferedImage brownPawnImage, brownRookImage, brownBishopImage, brownKnightImage,
               brownQueenImage, brownKingImage;


public ImageFactory(){
    try {
        yellowPawnImage = ImageIO.read(getClass().getResource("/chessgame/res/YellowP.png"));
        yellowRookImage = ImageIO.read(getClass().getResource("/chessgame/res/YellowR.png"));
        yellowBishopImage = ImageIO.read(getClass().getResource("/chessgame/res/YellowB.png"));
        yellowKnightImage = ImageIO.read(getClass().getResource("/chessgame/res/YellowN.png"));
        yellowQueenImage = ImageIO.read(getClass().getResource("/chessgame/res/YellowQ.png"));
        yellowKingImage = ImageIO.read(getClass().getResource("/chessgame/res/YellowK.png"));

        brownPawnImage = ImageIO.read(getClass().getResource("/chessgame/res/BrownP.png"));
        brownRookImage = ImageIO.read(getClass().getResource("/chessgame/res/BrownR.png"));
        brownBishopImage = ImageIO.read(getClass().getResource("/chessgame/res/BrownB.png"));
        brownKnightImage = ImageIO.read(getClass().getResource("/chessgame/res/BrownN.png"));
        brownQueenImage = ImageIO.read(getClass().getResource("/chessgame/res/BrownQ.png"));
        brownKingImage = ImageIO.read(getClass().getResource("/chessgame/res/BrownK.png"));


    } catch (IOException ex) {
        System.out.println("File Read Error");
    }
}

private static BufferedImage createCompatibleImage(BufferedImage image) {
    GraphicsConfiguration gc = BufferedImageGraphicsConfig.getConfig(image);
    int w = image.getWidth();
    int h = image.getHeight();
    BufferedImage result = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
    Graphics2D g2 = result.createGraphics();
    g2.drawRenderedImage(image, null);
    g2.dispose();
    return result;
}


private static BufferedImage resizeFigurineTrick(BufferedImage image, int width, int height) {
    image = createCompatibleImage(image);
    image = resize(image, 100, 100);
    image = resize(image, width, height);
    return image;
}

private static BufferedImage resize(BufferedImage image, int width, int height) {
    int type = image.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : image.getType();
    BufferedImage resizedImage = new BufferedImage(width, height, type);
    Graphics2D g = resizedImage.createGraphics();
    g.setComposite(AlphaComposite.Src);

    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BILINEAR);

    g.setRenderingHint(RenderingHints.KEY_RENDERING,
            RenderingHints.VALUE_RENDER_QUALITY);

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

    g.drawImage(image, 0, 0, width, height, null);
    g.dispose();
    return resizedImage;
}

public static BufferedImage getFigurineImage(int color, int type, int sideLength) {
    Piece p = new Piece(color, type);
    return getFigurineImage(p, sideLength);
}

public static BufferedImage getFigurineImage(Piece piece, int sideLength) {
    int type = piece.getType();
    int color = piece.getColor();
    BufferedImage imageToDraw = null;

    if (type == Piece.TYPE_PAWN) {
        if (color == Piece.YELLOW_COLOR) {
            imageToDraw = yellowPawnImage;
        }
        if (color == Piece.BROWN_COLOR) {
            imageToDraw = brownPawnImage;
        }
    }
    if (type == Piece.TYPE_ROOK) {
        if (color == Piece.YELLOW_COLOR) {
            imageToDraw = yellowRookImage;
        }
        if (color == Piece.BROWN_COLOR) {
            imageToDraw = brownRookImage;
        }
    }
    if (type == Piece.TYPE_BISHOP) {
        if (color == Piece.YELLOW_COLOR) {
            imageToDraw = yellowBishopImage;
        }
        if (color == Piece.BROWN_COLOR) {
            imageToDraw = brownBishopImage;
        }
    }
    if (type == Piece.TYPE_KNIGHT) {
        if (color == Piece.YELLOW_COLOR) {
            imageToDraw = yellowKnightImage;
        }
        if (color == Piece.BROWN_COLOR) {
            imageToDraw = brownKnightImage;
        }
    }
    if (type == Piece.TYPE_QUEEN) {
        if (color == Piece.YELLOW_COLOR) {
            imageToDraw = yellowQueenImage;
        }
        if (color == Piece.BROWN_COLOR) {
            imageToDraw = brownQueenImage;
        }
    }
    if (type == Piece.TYPE_KING) {
        if (color == Piece.YELLOW_COLOR) {
            imageToDraw = yellowKingImage;
        }
        if (color == Piece.BROWN_COLOR) {
            imageToDraw = brownKingImage;
        }
    }

    return resizeFigurineTrick(imageToDraw, sideLength, sideLength);
}
}
Posted

1 solution

Without access to a demo program, I cannot tell for sure what the cause of your problem is.

You can try the following: minimize your chess program, and the maximize it again. If the capture pieces show correctly, then your problem is simply that you need to set the bounds of the JPanel you are extending - simply calling the repaint() method isn't enough to guarantee the image is being drawn correctly.

You may choose to read my article on creating a Java™ Swing JImageComponent for some more insight. You may just find something that will help you out of a ditch later on.

[Creating a Swing JImageComponent]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900