0

I want to apply Anti Aliasing on my little idleGame.

On my first try I just used g.fillRect to create my rectangles but the Anti Aliasing didn't work. Then I commented some things out and startet using Area. It still seem to not apply Anti Aliasing. The only side effect is that it got slower due to the use of Areas.

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

Does any one have an idea, what I could try?

(Maybe smoothing would be a better description?)

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • Rendering hints - [for example](http://stackoverflow.com/questions/16288833/antialias-height-map-edge-using-java-2d/16290892#16290892), [example](http://stackoverflow.com/questions/32532038/java-graphics2d-is-drawing-one-pixel-off-rounding-error/32532371#32532371), [example](http://stackoverflow.com/questions/13906687/how-can-i-smooth-my-jframe-shape/13906713#13906713) – MadProgrammer Apr 07 '17 at 10:53
  • For more information see [Controlling Rendering Quality](https://docs.oracle.com/javase/tutorial/2d/advanced/quality.html) – MadProgrammer Apr 07 '17 at 10:55
  • hm that don't does what I want. –  Apr 07 '17 at 12:32
  • I want to show the areas as curved blobs, maybe this describes it better? –  Apr 07 '17 at 12:34
  • Use a different shape, rather then rectangle – MadProgrammer Apr 07 '17 at 21:09
  • Which one you mean??? –  Apr 11 '17 at 09:41
  • Depends on what shape you're trying to achieve – MadProgrammer Apr 11 '17 at 09:52
  • Maybe [RoundRectangle2D](https://docs.oracle.com/javase/8/docs/api/java/awt/geom/RoundRectangle2D.html) – MadProgrammer Apr 11 '17 at 09:53
  • I already tried to tell you what I'm trying to achieve. Just tell me to use a different shape isn't helping me in any way. –  Apr 12 '17 at 11:06
  • with RoundRectangle2D I would have many holes on the field –  Apr 12 '17 at 11:07
  • Antialiasing isn't really going to do much on a square object, it works well on lines that aren't 90 degrees. You could try "faking" it by rendering the original output at 4x the size you need and scaling it down, but the would depend on the scaling algorithm – MadProgrammer Apr 12 '17 at 11:15

0 Answers0