如何在java.awt.geom.Area对象上使用Anti Aliasing?

时间:2017-04-07 10:52:17

标签: java graphics2d antialiasing smoothing

我想在我的小闲人游戏中应用Anti Aliasing。

在我第一次尝试时,我只使用g.fillRect创建了我的矩形,但抗锯齿功能并不起作用。然后我用Area评论了一些事情和startet。它似乎仍然不适用抗锯齿。唯一的副作用是由于使用了区域而变慢了。

enter image description here

private void render()
{
    BoardParameters MBP = myBoardParameters;

    BufferStrategy bs = myDisplay.getCanvas().getBufferStrategy();
    if (bs == null)
    {
        myDisplay.getCanvas().createBufferStrategy(2);
        return;
    }
    Graphics2D g2 = (Graphics2D)bs.getDrawGraphics();
    //Clear Screen
    g2.clearRect(0, 0, MBP.getWindowWidth(), MBP.getWindowHeight());
    //Draw Here!

    Area area[] = {new Area(),new Area(),new Area()};

    drawUnits(g2,area);


    int[][] cs = myBoardParameters.getColorScheme();
    g2.setColor(new Color(cs[0][0],cs[0][1],cs[0][2]));
    g2.fill(area[0]);
    g2.setColor(new Color(cs[1][0],cs[1][1],cs[1][2]));
    g2.fill(area[1]);
    g2.setColor(new Color(cs[2][0],cs[2][1],cs[2][2]));
    g2.fill(area[2]);

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

    //End Drawing!
    bs.show();
    g2.dispose();
}

private void drawUnits(Graphics g, Area[] area)
{
    BoardParameters MBP = myBoardParameters;

    for (int i = 0; i < MBP.getRows(); i++)
    {
        for (int j = 0; j < MBP.getCollumns(); j++)
        {
            Animal actAnimal = myBoardData.animals[j][i];
            int actualArea = 0;
            if (actAnimal != null)
            {
                int[][] cs = myBoardParameters.getColorScheme();
                switch (actAnimal.getClass().getSimpleName())
                {
                    case "Fish":
                        //g.setColor(new Color(cs[0][0],cs[0][1],cs[0][2]));
                        actualArea = 1;
                        break;
                    case "Shark":
                        //g.setColor(new Color(cs[1][0],cs[1][1],cs[1][2]));
                        actualArea = 2;
                        break;
                    case "Plankton":
                        //g.setColor(new Color(cs[2][0],cs[2][1],cs[2][2]));
                        actualArea = 3;
                        break;
                    default:
                        System.out.println("Unknown Unit found in 'myBoardParameters.units[" + j + "][" + i + "]': " + actAnimal);
                        break;
                }
            }else{
                //g.setColor(Color.cyan);
            }
            //g.fillRect(j * MBP.getCellWidth(), i * MBP.getCellHeight(), MBP.getCellWidth(), MBP.getCellHeight());
            Shape shape = new Rectangle2D.Double(j * MBP.getCellWidth(), i * MBP.getCellHeight(), MBP.getCellWidth(), MBP.getCellHeight());
            Area areaAdd = new Area(shape);
            switch (actualArea)
            {
                case 1:
                    area[0].add(areaAdd);
                    break;
                case 2:
                    area[1].add(areaAdd);
                    break;
                case 3:
                    area[2].add(areaAdd);
                    break;
            }
        }
    }
}

有没有人有想法,我能尝试什么?

(也许平滑会有更好的描述?)

0 个答案:

没有答案