我观看了有人制作MineCraft克隆的Youtube视频,该视频编译并运行时没有错误。但是当我完全复制它时,我收到了一个错误。
错误发生在Render类中:int pixels = new int(width * height);
渲染:
package com.mads.minecraft.graphics;
public class Render {
public final int width;
public final int height;
public int[] pixels;
public Render(int width, int height){
this.width = width;
this.height = height;
int pixels = new int(width * height);
}
public void draw(Render render, int xOffset, int yOffset){
for (int y = 0; y < render.height; y++){
int yPix = y + yOffset;
if(yPix < 0 || yPix >= height) continue;
for (int x = 0; y < render.width; x++){
int xPix = x + xOffset;
if(xPix < 0 || xPix >= width) continue;
pixels[xPix + yPix + width] = render.pixels[x + y * render.width];
}
}
}
}
显示:
package com.mads.minecraft;
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 com.mads.minecraft.graphics.Screen;
public class Display extends Canvas implements Runnable{
static final long serialVersionUID = 1L;
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final String TITLE = "Minecraft Alpha";
private Thread thread;
private boolean running = false;
private BufferedImage img;
private Screen screen;
private int[] pixels;
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();
}
public void stop(){
if(!running) return;
running = false;
try {
thread.join();
} catch (Exception e){
e.printStackTrace();
System.exit(0);
}
}
public void run(){
while(running){
tick();
render();
}
}
public void tick(){}
public void render(){
BufferStrategy bs = 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, WIDTH, HEIGHT, null);
g.dispose();
bs.show();
}
public static void main(String []args){
Display game = new Display();
JFrame frame = new JFrame();
frame.add(game);
frame.pack();
frame.setTitle(TITLE);
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
game.start();
}
}
屏幕:
package com.mads.minecraft.graphics;
import java.util.Random;
public class Screen extends Render{
private Render test;
public Screen(int width, int height) {
super(width, height);
Random random = new Random();
test = new Render(256, 256);
for(int i = 0; i < 256 * 256; i++) {
test.pixels[i] = random.nextInt();
}
}
public void render(){
draw(test, 0, 0);
}
}
答案 0 :(得分:2)
以下是您需要替换的无效声明:
int pixels = new int(width * height);
(int是原始数据类型而非引用类型,因此您无法创建对象)
通过
pixels = new int[width * height]
; (你可以创建一个int数组。你已经在实例级别将变量pixels
声明为int [],所以不需要再次声明)
希望,这有帮助。
答案 1 :(得分:1)
显然你正试图在Render
中初始化一个数组。您所要做的就是用{:}替换int pixels = new int(width * height)
构造函数中的Render
:
pixels = new int[width * height];