gpt4 book ai didi

java - 随机像素的图像

转载 作者:行者123 更新时间:2023-11-29 05:57:36 24 4
gpt4 key购买 nike

我正在尝试制作随机像素的图像。我写了这段代码,但没有用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();
}
}

Render.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 中永远不会执行移动图像我希望图像在框架中移动,作为制作动画和动画随机像素图像的步骤。请帮助我。

最佳答案

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

关于java - 随机像素的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11467082/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com