首页 文章

BufferedImage为什么会这样?

提问于
浏览
1

我要发布代码 . 它不是太复杂或太长 . 我只是不明白它为什么会起作用 .

在下面的代码中,图像在渲染方法中呈现,但在游戏循环中不会更新 . 我无法弄清楚图像是如何更新的 . 代码运行,输出显示更改的移动颜色 . 我看到tick方法更新了 pixels[] 数组,但即使在循环之外,像素也被设置为等于来自图像的数据 . 如何更改 pixels[] 更改图像 . 请帮我理解这段关系 .

我'm sorry if I' m发布错误 . 我做了搜索,但大多数人似乎有一个问题,它没有't work. Mine works fine. I just need to understand why. Image is not getting updated in the tick method. pixels is. So why is image changing as if it'以某种方式连接到 pixels[] ???

My Code Below :

package com.channelsplace.game;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;

import com.channelsplace.game.gfx.SpriteSheet;

public class Game extends Canvas implements Runnable
{
    private boolean running;
    private static final long serialVersionUID = 1L;
    public static final int WIDTH = 160;
    public static final int HEIGHT = WIDTH / 12*9;
    public static final int SCALE = 3;
    public static final String NAME = "Game";
    private JFrame frame;
    public int tickCount = 0;
/**********************************************************************
The next two lines are part of what I don't get.
below this you'll see a tick method that updates the pixels array
and a render method that renders image. but no overt flow of information 
from pixels to image.
**********************************************************************/ 
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
    private SpriteSheet spriteSheet = new SpriteSheet("/sprite_sheet.png");

    public Game()
    {
        setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
        setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
        setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
        frame = new JFrame(NAME);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(this, BorderLayout.CENTER);
        frame.pack();
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

    public synchronized void start()
    {
        running = true;
        new Thread(this).start();
    }
    public synchronized void stop()
    {
        running = false;
    }

    public void run() 
    {
        long lastTime = System.nanoTime();
        double nsPerTick = 1000000000D/60D;
        int ticks = 0;
        int frames = 0;
        long lastTimer = System.currentTimeMillis();
        double delta = 0;
        boolean shouldRender = true;        
        while(running)
        {
            long now = System.nanoTime();
            delta += (now - lastTime) / nsPerTick;
            lastTime = now;

            if (delta >= 1)             
            {
                ticks++;
                tick();
                delta -= 1;
                shouldRender = true;
            }

            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (shouldRender)
            {
                frames++;
                render();
                shouldRender = false;
            }


            if (System.currentTimeMillis()-lastTimer>=1000)
            {
                lastTimer += 1000;
                System.out.println(frames+" , "+ticks);
                frames = 0;
                ticks = 0;
            }
        }

    }

    public void tick()
    {
        tickCount++;
        for (int i = 0; i < pixels.length; i++)
        {
            pixels[i] = i + tickCount;
        }
    }

    public void render()
    {
        BufferStrategy bs = getBufferStrategy();
        if (bs == null)
        {
            createBufferStrategy(3);
            return;
        }
        Graphics g = bs.getDrawGraphics();
        g.drawImage(image, 0, 0, getWidth(), getHeight(),null);
        g.dispose();
        bs.show();

    }
    public static void main(String[] args) 
    {
        new Game().start();

    }
}

//thanks for reading

2 回答

  • 0

    但它没有在游戏循环中更新 .

    image DOES 在游戏循环中得到更新:

    public void tick()
    {
        tickCount++;
        for (int i = 0; i < pixels.length; i++)
        {
            // where the image gets updated
            // pixels[i] = i + tickCount; // <-- it does exactly the same as the alternative below
            image.setRBG(i/WIDTH, i%WIDTH, i + tickCount);
        }
    }
    

    听起来很明显但像素[]是声明的int []而不是图像的一部分 .

    我看到了混乱所在 .

    什么是图像?图像只是一些像素,存储在int数组 pixels[] 中 .

    假设您创建一个20x30大小的 BufferedImage ,在内部创建一个大小为20 * 30或600的int数组 . 数组中的每个int表示1个像素,在RGB颜色模型中,即每个int值是3个组件的组合R(红)/ G(绿)/ B(蓝色) . 每个组件占用int的8位(32位) . 如果支持,剩余的8位可用于Alpha通道(透明度) .

    (如果你查看 BufferedImage 的源代码,它不会自己创建int数组,它使用 RasterWriter ,它使用 DataBuffer ,这是实现细节,我会把它留下来以避免更多的混淆 . )

    BufferedImage 提供了许多方法来访问(即读/写)数组中的值 .

    tick() 中你直接写入数组 pixels[] ,你也可以调用方法 setRGB(x, y, rgb) 来写入数组,无论哪种方式,你都在修改同一个数组,即所谓的"image" .

    render() 中,它绘制"image",即它从图像中包含的数组中读取值,并在屏幕上绘制像素 .

    希望这更清楚一点 .

  • 0

    谢谢大家 . 我终于弄清楚,当像素[]被创建时,它指向图像的数据 . 我觉得它是一个单独的int []并且创建了像素[],数据被复制到'=' . 但事实并非如此 . 它指向图像中的数据 . 这不是一个深刻的副本 . 像素和图像数据共享相同的存储器地址 . 感谢您的耐心等待 . 渠道

相关问题