我正在尝试制作随机像素的图像。我写了这段代码,但没有 用 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 中永远不会执行移动图像我希望图像在框架中移动,作为制作动画和动画随机像素图像的步骤。请帮我。