简体   繁体   English

在BufferedImage中操作像素

[英]Manipulating pixels in BufferedImage

I wanted to manipulate pixels in a bufferedimage by doing this: 我想通过这样做来操纵bufferedimage中的像素:

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

The problem is: It displays the image but without the right color. 问题是:它显示图像但没有正确的颜色。 There are no errors in the console. 控制台中没有错误。

Anyway, here's my main class: 无论如何,这是我的主要课程:

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

}

Here's the the Game class: 这是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();
    }

}

And, finally my ScreenRenderer class: 最后,我的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;
        }
    }

}

The other classes like Player don't matter. Player这样的其他类并不重要。 Only those 3 classes above do. 只有上面的3个班级。

You are not writing to the actual BufferedImage raster. 您没有写入实际的BufferedImage栅格。

Below is the code which handles that. 下面是处理它的代码。 I have made some other changes as well. 我也做了一些其他的改变。

Explorer.java 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 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 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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM