Java中的图形生成变得缓慢

时间:2015-09-17 10:08:40

标签: java android graphics

我试图找出如何创建一个六角平铺环境来构建游戏的好方法,我不确定这是否适合这类问题,所以请轻轻地问我如果我在错误的地方,请删除它。

我已经创建了一个处理和生成地图的方法的骨架但是我不必在需要很长时间才能加载它之前使地图特别大,所以也许你们可以帮我找到我的内容愚蠢的。

我猜在课程资源存储中有冗余代码,但我对图像处理知之甚少。

另外,我在这里建造的是一条死胡同吗?我应该使用JavaFX或某种类型的库吗? (我希望它能在android上工作)

http://pastebin.com/gWXcyPHP

公共类WorldView扩展了Canvas {     私人地图;

//Graphics related
private BufferStrategy buffer;
private int scale;

public WorldView(Map map, int scale)
{
    this.map = map;
    this.scale = 35;

    JFrame container = new JFrame("Stroll");
    container.setDefaultCloseOperation(3);
    JPanel panel = (JPanel)container.getContentPane();
    panel.setPreferredSize(new Dimension(800, 600));
    panel.setLayout(null);

    this.setBounds(0,0,800,600);
    panel.add(this);

    this.setIgnoreRepaint(true);

    container.pack();
    container.setResizable(false);
    container.setVisible(true);

    this.createBufferStrategy(2);
    buffer = getBufferStrategy();

    refreshWindow();
}

public void refreshWindow()
{
    Graphics2D g = (Graphics2D) buffer.getDrawGraphics();
    g.setColor(Color.LIGHT_GRAY);
    g.fillRect(0, 0, 800, 600);

    for (Map.Entry<HexPoint, Tile> entry: map.entrySet())
    {
        Point pixelPoint = calcCoordsToPixels(entry.getKey(), scale);
        entry.getValue().draw(g, pixelPoint.x, pixelPoint.y, scale);
    }

    g.dispose();
    buffer.show();
} 

}

公共类Tile {     私人地形地形;

//Graphics
private int scale = 0;
private BufferedImage image;
private boolean needsRepaint;

public Tile(Terrain terrain)
{
    this.terrain = terrain;
    needsRepaint = true;
}

public void draw(Graphics g, int x, int y, int scale)
{
    if (this.scale != scale)
    {
        image = ResourceStore.get().getImage(terrain, scale);
        this.scale = scale;
        needsRepaint = true;
    }
    if (needsRepaint)
    {
        g.drawImage(image, x, y, null);
        needsRepaint = false;
    }

    //also calls other objects who resides on this tile for repainting
}

}

公共类ResourceStore {

private static ResourceStore single = new ResourceStore();
private int scale = 0;

public static ResourceStore get()
{
    return single;
}

private HashMap<Terrain, BufferedImage> images = new HashMap();

public BufferedImage getImage(Terrain terrain, int scale)
{
    if (images.get(terrain) != null && this.scale == scale)
    {
        return images.get(terrain);
    }

    if (images.get(terrain) != null)
    {
        return resize(images.get(terrain), scale, false);
    }

    BufferedImage sourceImage = null;

    try
    {
        URL url = this.getClass().getClassLoader().getResource("main/resources/" + terrain.getName());

        if (url == null)
        {
            System.out.println("Can't load image");
        }

        sourceImage = ImageIO.read(url);
    } catch (IOException ex)
    {
        System.out.println("Can't load image");
    }

    sourceImage = resize(sourceImage, scale, false);

    GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
    BufferedImage image = gc.createCompatibleImage(sourceImage.getWidth(), sourceImage.getHeight(), Transparency.BITMASK);

    image.getGraphics().drawImage(sourceImage, 0, 0, null);

    images.put(terrain, image);
    return image;
}

public BufferedImage resize(BufferedImage image, int scale, boolean transparent)
{
    int width = scale * 2;
    int height = (int) Math.round(Math.sqrt(3) / 2 * width);

    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT); //maybe change?
    Graphics2D g2d = bi.createGraphics();
    if (!transparent)
    {
        g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
    } else
    {
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        Composite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) 0.5);
        g2d.setComposite(comp);
    }
    g2d.drawImage(image, 0, 0, width, height, null);
    g2d.dispose();
    return bi;
}

}

0 个答案:

没有答案