捕获的棋子的BufferedImage不会显示在我的JPanel上 [英] BufferedImage for captured chess pieces does not display on my JPanel

查看:72
本文介绍了捕获的棋子的BufferedImage不会显示在我的JPanel上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请使用Java开发国际象棋游戏。我在尝试显示捕获的棋子时遇到问题,这些棋子都保存在bufferedImage中。当我运行并打印bufferedImage的变量时,它不是null并且不会在JPanel上显示图像。



另外我的捕获的棋子列表存储在List capturePiece中并且工作正常,图像的路径也是正确的。唯一的问题是存储在bufferedImage上的图像不显示。



这是我的代码



BrownPlayerPanel类:

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:

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类:






ImageFactory class:

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);
}
}

推荐答案

无法访问演示程序,我无法确定原因是什么你的问题是。



您可以尝试以下方法:尽量减少您的国际象棋程序,并再次最大化它。如果捕获片段显示正确,那么你的问题只是你需要设置你正在扩展的JPanel的边界 - 简单地调用repaint()方法不足以保证正确绘制图像。



您可以选择阅读我关于创建Java™Swing JImageComponent的文章,以获得更多信息。你可能会找到一些可以帮助你摆脱困境的东西。



[创建Swing JImageComponent ]
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]


这篇关于捕获的棋子的BufferedImage不会显示在我的JPanel上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆