java – 在BufferedImage中处理像素

我想通过这样做来操纵bufferedimage中的像素:

BufferedImage screen = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
int[] pixels = ((DataBufferInt)screen.getRaster().getDataBuffer()).getData();

问题是:它显示图像但没有正确的颜色.控制台中没有错误.

无论如何,这是我的主要课程:

package net.explorer.explorer;

import javax.swing.JFrame;

public class Explorer extends JFrame {

    private static final long serialVersionUID = 1L;

    public static int WIDTH = 1280;
    public static int HEIGHT = 800;

    public Explorer() {
        super("Explorer");
        add(new Game(WIDTH, HEIGHT));
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Explorer();
    }

}

这是Game类:

package net.explorer.explorer;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.Timer;

import net.explorer.entities.Entity;
import net.explorer.entities.Player;
import net.explorer.render.ScreenRenderer;

public class Game extends JPanel implements ActionListener {

    private static final long serialVersionUID = 1L;

    public int width, height;

    public BufferedImage screen;
    public int[] pixels;
    public ScreenRenderer screenRenderer;

    private Timer timer;

    public static ArrayList<Entity> entities;
    public Player player;

    public Game(int width, int height) {
        this.width = width;
        this.height = height;
        setPreferredSize(new Dimension(width, height));
        screen = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        pixels = ((DataBufferInt)screen.getRaster().getDataBuffer()).getData();
        screenRenderer = new ScreenRenderer(width, height);
        timer = new Timer(1, this);
        timer.start();
        entities = new ArrayList<Entity>();
        player = new Player();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        this.render(g);
    }

    private void render(Graphics g) {
        screenRenderer.renderScreen(screenRenderer);

        for(int i = 0; i < width * height; i++) {
            screenRenderer.pixels[i] = this.pixels[i];
        }

        g.drawImage(screen, 0, 0, this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
    }

}

最后,我的ScreenRenderer类:

package net.explorer.render;

public class ScreenRenderer {

    public int width, height;
    public int[] pixels;

    public ScreenRenderer(int width, int height) {
        this.width = width;
        this.height = height;
        this.pixels = new int[width * height];
    }

    public void renderScreen(ScreenRenderer screen) {
        for(int i = 0; i < screen.width*screen.height; i++) {
            pixels[i] = 0x000000;
        }
    }

}

像Player这样的其他类并不重要.只有上面的3个班级.

最佳答案 您没有写入实际的BufferedImage栅格.

下面是处理它的代码.我也做了一些其他的改变.

Explorer.java

package image;

import javax.swing.JFrame;

public class Explorer extends JFrame {

    private static final long serialVersionUID = 1L;

    public static int WIDTH = 400;
    public static int HEIGHT = 400;

    public Explorer() {
        super("Explorer");
        add(new Game(WIDTH, HEIGHT));
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Explorer();
    }

}

ScreenRenderer

package image;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.util.Random;

public class ScreenRenderer {

    private int width, height;
    /*public int[] pixels;*/
    private WritableRaster raster;
    private int[] pixels;
    private Random rand = new Random();

    public ScreenRenderer(BufferedImage image) {
        /*this.width = width;
        this.height = height;*/
        //this.pixels = new int[width * height];
        this.raster = image.getRaster();
        this.width = raster.getWidth();
        this.height = raster.getHeight();

        pixels = new int[raster.getWidth() * raster.getHeight() * raster.getNumBands()];
        System.out.printf("%d %d %d", width, height, pixels.length);
    }

    public void renderScreen() {
        int seed = rand.nextInt();
        for (int i = 0; i < pixels.length; i++) {
            pixels[i] = i * seed;
        }
        raster.setPixels(0, 0, width, height, pixels);
    }

}

Game.java

package image;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.Timer;

/*import net.explorer.entities.Entity;
import net.explorer.entities.Player;
import net.explorer.render.ScreenRenderer;*/

public class Game extends JPanel implements ActionListener {

    private static final long serialVersionUID = 1L;

    public int width, height;

    public BufferedImage screen;
    public int[] pixels;
    public ScreenRenderer screenRenderer;

    private Timer timer;

    /*public static ArrayList<Entity> entities;
    public Player player;*/

    public Game(int width, int height) {
        this.width = width;
        this.height = height;
        setPreferredSize(new Dimension(width, height));
        screen = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        pixels = ((DataBufferInt) screen.getRaster().getDataBuffer()).getData();
        screenRenderer = new ScreenRenderer(screen);
        timer = new Timer(1, this);
        timer.start();
        /*entities = new ArrayList<Entity>();
        player = new Player();*/
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        this.render(g);
    }

    private void render(Graphics g) {
        screenRenderer.renderScreen();

        /*for (int i = 0; i < width * height; i++) {
            screenRenderer.pixels[i] = this.pixels[i];
        }*/

        g.drawImage(screen, 0, 0, this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
    }

}
点赞