缩放后的BufferedImage黑白

时间:2016-02-21 15:29:26

标签: java bufferedimage image-scaling

在缩放国家旗帜的BufferdImages之后,大约50%的旗帜以黑白(不是灰度)显示,很少看起来很奇怪,如turky:

turky

这是一个黑白的例子 b&w

这里我设置了标志

protected void updateFlag(BufferedImage flag){
    int height = pnlFlag.getHeight();
    int width = (int)(1f * flag.getWidth() / flag.getHeight() * height);
    BufferedImage scaledFlag = new BufferedImage(width, height, flag.getType());

    Graphics2D g2d = scaledFlag.createGraphics();
    g2d.drawImage(flag, 0, 0, scaledFlag.getWidth(), scaledFlag.getHeight(), null);
    g2d.dispose();

    pnlFlag.flag = scaledFlag;
    pnlFlag.repaint();
}

我的JPanel

class FlagPanel extends JPanel{

    private BufferedImage flag;

    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        if(flag == null) return;

        g.drawImage(flag, 0, 0, null);
    }

}

0 个答案:

没有答案