我正在尝试使用 BufferStrategy 将单个像素绘制到屏幕上,但是当我运行程序时,所有像素都显示得很奇怪,它应该在左上角形成 1 个正方形,但它会产生多个在它们之间有黑线的正方形反而。
显示类
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
import graphics.Render;
public class Display extends Canvas implements Runnable {
public static final int HEIGHT = 600;
public static final int WIDTH = 800;
public static final String TITLE = "Minefront Pre-Alpha 0.01";
private Thread thread;
private Screen screen;
private BufferedImage img;
private Render render;
private int[] pixels;
public boolean running = false;
public Display()
{
screen = new Screen(WIDTH, HEIGHT);
img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
}
public void start() {
if (running)
return;
running = true;
thread = new Thread(this);
thread.start();
System.out.println("Working");
}
public void stop() {
if (!running)
return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
System.exit(0);
}
}
public static void main(String args[]) {
Display game = new Display();
JFrame frame = new JFrame();
frame.add(game);
frame.pack();
frame.setSize(WIDTH, HEIGHT);
frame.setTitle(TITLE);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
game.start();
}
@Override
public void run() {
while(running)
{
tick();
render();
}
}
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null)
{
createBufferStrategy(3);
return;
}
screen.render();
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();
}
private void tick() {
}
}
渲染类
public class Render {
public final int width;
public final int height;
public final int[] pixels;
public Render(int Height, int Width) {
this.height = Height;
this.width = Width;
pixels = new int[Height * Width];
}
public void draw(Render render, int xOffset, int yOffset) {
for (int y = 0; y < render.height; y++) {
int ypix = y + yOffset;
for (int x = 0; x < render.width; x++) {
int xpix = x + xOffset;
pixels[xpix + (ypix * width)] = render.pixels[x + (y * render.width)];
}
}
}
}
屏幕类
import graphics.Render;
public class Screen extends Render {
public int Size = 150;
public int Color = 0;
public int R = 0;
public int G = 0;
public int B = 0;
private Render test;
public Screen(int Height, int Width) {
super(Height, Width);
test = new Render(Size, Size);
for (int i = 0; i < Size * Size; i++) {
Color = RGBtoInt(R, G, B);
test.pixels[i] = Color;
R++;
if (i % 100 == 0 || i % 100 == 1 || i % 100 == 2 || i % 100 == 99 || i % 100 == 98) {
test.pixels[i] = 16777215;
} else if (i % 25 == 0 || i % 25 == 1 || i % 25 == 2 || i % 25 == 24 || i % 25 == 23) {
test.pixels[i] = 3328255;
} else if (R > 255) {
R = 0;
G++;
if (G > 255) {
G = 0;
B++;
if (B > 255) {
B = 0;
}
}
}
}
}
private int RGBtoInt(int R, int G, int B) {
R = (R << 16) & 0x00FF0000;
G = (G << 8) & 0x0000FF00;
B = B & 0x000000FF;
return 0xFF000000 | R | G | B;
}
public void render() {
draw(test,0,0);
}
}
如果你知道发生了什么,请告诉我,谢谢。