1

我正在尝试制作随机像素的图像。我写了这段代码,但没有 用 LadderSnack.java

import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.*;

public class LadderSnack extends Canvas implements Runnable {
    public static JFrame frame = new JFrame("EmiloLadderSnack v. 1.0");
    public static int width = Toolkit.getDefaultToolkit().getScreenSize().width, height = Toolkit.getDefaultToolkit().getScreenSize().height;
    public boolean run = false;
    public Thread thread;
    public BufferedImage img;
    public int[] pixels;
    public Screen screen;

    public LadderSnack() {
        screen = new Screen(width, height);
        img = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
        pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
    }

    public void start() {
        if (run)
            return;
        run = true;
        thread = new Thread(this);
        thread.start();
    }

    public void stop() {
        if (!run)
            return;
        try {
            thread.join();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
    }

    public void run() {
        while (run) {
            trick();
            render();
        }
    }

    private void trick() {
    }

    private void render() {
        screen = new Screen(width, height);
        BufferStrategy bs = this.getBufferStrategy();
        if (bs == null) {
            createBufferStrategy(3);
            return;
        }
        for (int i = 0; i < width * height; i++)
            pixels[i] = screen.pixels[i];
        Graphics g = bs.getDrawGraphics();
        g.drawImage(img, 0, 0, width, height, null);
        g.dispose();
        bs.show();
    }

    public static void main(String[] args) {
        LadderSnack ladderSnack = new LadderSnack();

        frame.setSize(width, height);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.add(ladderSnack);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        ladderSnack.start();
    }
}

渲染.java

public class Render {

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

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

    public void draw(Render render, int xOffset, int yOffset) {
        int xPixel, yPixel, y, x;
        for (x = 0; x < width; x++) {
            xPixel = x + xOffset;
            for (y = 0; y < height; y++) {
                yPixel = y + yOffset;
                pixels[xPixel + yPixel * width] = render.pixels[xPixel + yPixel * width];
            }
        }
    }
}

屏幕.java

import java.awt.Toolkit;
import java.util.Random;

public class Screen extends Render {

    private Render test;

    public Screen(int width, int height) {
        super(width, height);
        int i;
        Random rand = new Random();
        test = new Render(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);
        for (i = 0; i < width * height; i++)
            pixels[i] = rand.nextInt();
    }

    public void render() {
        draw(test, 0, 0);
    }
}

在运行时

public void render() {
    draw(test, 50, 50);
}

在 Screen.java 中永远不会执行移动图像我希望图像在框架中移动,作为制作动画和动画随机像素图像的步骤。请帮我。

4

3 回答 3

2

您可能想使用我的 api:http ://www.threadox.com/projects/random-image-api/ 您收到一个缓冲图像,然后您只需将其绘制到画布上。

于 2013-07-02T16:32:43.410 回答
1

你的代码一团糟。以下是您可能需要注意的内容:在 Java 中绘制像素图像

以下是问题:

  • private void LadderSnack()我认为这应该是构造函数所以应该写private LadderSnack()

  • Runnable应该重新考虑您的实施。避免使用while(true){}而是使用while(true) {Thread.sleep(xxx)}以避免您的应用程序冻结。

  • 您创建一个pixels随机值数组,然后使用pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();数据缓冲区中的任何内容覆盖您的值。

  • 你永远不会使用你的pixels数组。

  • 我认为你应该回顾整个概念。
于 2012-07-13T09:07:39.497 回答
0

LadderSnack.java

import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.*;

public class LadderSnack extends Canvas implements Runnable {
    public static JFrame frame = new JFrame("EmiloLadderSnack v. 1.0");
    public static int width = Toolkit.getDefaultToolkit().getScreenSize().width, height = Toolkit.getDefaultToolkit().getScreenSize().height;
    public boolean run = false;
    public Thread thread;
    public BufferedImage img;
    public int[] pixels;
    public Screen screen;

    public LadderSnack() {
        screen = new Screen(width, height);
        img = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
        pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
    }

    public void start() {
        if (run)
            return;
        run = true;
        thread = new Thread(this);
        thread.start();
    }

    public void stop() {
        if (!run)
            return;
        try {
            thread.join();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
    }

    public void run() {
        while (run) {
            trick();
            render();
        }
    }

    private void trick() {
    }

    private void render() {
        BufferStrategy bs = this.getBufferStrategy();
        if (bs == null) {
            createBufferStrategy(3);
            return;
        }
        for (int i = 0; i < width * height; i++)
            pixels[i] = screen.pixels[i];
        Graphics g = bs.getDrawGraphics();
        g.drawImage(img, 0, 0, width, height, null);
        g.dispose();
        bs.show();
    }

    public static void main(String[] args) {
        LadderSnack ladderSnack = new LadderSnack();

        frame.setSize(width, height);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.add(ladderSnack);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        ladderSnack.start();
    }
}

渲染.java

public class Render {


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

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

    public void draw(Render render, int xOffset, int yOffset) {
        int xPixel, yPixel, y, x;
        for (x = 0; x < render.width; x++) {
            xPixel = x + xOffset;
            for (y = 0; y < render.height; y++) {
                yPixel = y + yOffset;
                pixels[xPixel + yPixel * width] = render.pixels[xPixel-xOffset + (yPixel-yOffset) * render.width];
            }
        }
    }
}

屏幕.java

import java.awt.Toolkit;
import java.util.Random;

public class Screen extends Render {
    private Render test;

    public Screen(int width, int height) {
        super(width, height);
        int i;
        Random rand = new Random();
        test = new Render(333,333);//Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);
        for (i = 0; i < 333 * 333; i++)
            test.pixels[i] = rand.nextInt();
        render();
    }

    public void render() {
        draw(test, 50, 50);
    }
}
于 2012-07-14T08:05:23.650 回答