Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 像素应该渲染,但它';不是_Java - Fatal编程技术网

Java 像素应该渲染,但它';不是

Java 像素应该渲染,但它';不是,java,Java,希望你能帮助我 我参加了Ludum Dare 28,我遇到了一个问题,我在我的屏幕上渲染像素,他们应该出现,但他们没有 我是新来的,我不理解代码插入,所以我希望您介意我将您发送到gyazo linkg(屏幕快照上传器) 我有我的代码,它应该是渲染像素,如前所述。但事实并非如此 (希望我的代码工作正常*)我在eclipse中也没有出现错误 Screen.java package com.cmnatic.cmnatic.graphics; public class Scree

希望你能帮助我

我参加了Ludum Dare 28,我遇到了一个问题,我在我的屏幕上渲染像素,他们应该出现,但他们没有

我是新来的,我不理解代码插入,所以我希望您介意我将您发送到gyazo linkg(屏幕快照上传器)

我有我的代码,它应该是渲染像素,如前所述。但事实并非如此

(希望我的代码工作正常*)我在eclipse中也没有出现错误

Screen.java

       package com.cmnatic.cmnatic.graphics;

    public class Screen {

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

        int xtime = 0, ytime = 50;
        int counter = 0;

        public Screen(int width, int height) {
            this.width = width;
            this.height = height;
            pixels = new int[width * height]; // 0 - 50,399 = 50,400
        }

        public void clear() {
            for (int i = 0; i < pixels.length; i++) {
                pixels[i] = 0;
            }
        }

        public void render()  {
            counter++;
            if (counter % 100 == 0) {
                xtime++;
            if (counter % 100 == 0) {
                ytime++;

            for (int y = 0; y < height; y++)  {
                if (ytime >= height) break;
                 for (int x = 0; x < width; x++)  { 
                     if (xtime >= width) break;
                     pixels[xtime + ytime * width] = 0xff00ff;
                 }

            }
       }
    }
    }
    }

                          Game.java



   package com.cmnatic.cmnatic;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
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.cmnatic.cmnatic.graphics.Screen;

public class Game extends Canvas implements Runnable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private static final Screen Screen = null;
    public static int width = 300;
    public static int height = width / 16 * 9;  // 168
    public static int scale = 3;

    private Thread thread;
    private JFrame frame;
    private boolean running = false;

    private Screen screen;

    private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();

    public Game() {
        Dimension size = new Dimension(width * scale, height * scale);
        setPreferredSize(size);

        screen =  new Screen(width, height);

        frame = new JFrame();
   }

    public synchronized void start() {
        running = true;
        thread = new Thread(this, "Display");
        thread.start();
    }

    public synchronized void stop() {
        running = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        while (running == true) {
            update();
            render();
        }
    }

    public void update() {

    }

    public void render() {
        BufferStrategy bs = getBufferStrategy();
        if (bs == null) {
            createBufferStrategy(3);
            return;
        }

        screen.clear();

        screen.render();

        for (int i = 0; i < pixels.length; i++) {
            pixels[i] = screen.pixels[i];
        }

        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
        g.dispose();
        bs.show();
    }

    public static void main(String[] args) {
        Game game = new Game();
        game.frame.setResizable(false);
        game.frame.setTitle("The Last Hit");
        game.frame.add(game);
        game.frame.pack();
        game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        game.frame.setLocationRelativeTo(null);
        game.frame.setVisible(true);

        game.start();
    }

    public static Screen getScreen() {
        return Screen;
    }

}
package com.cmnatic.cmnatic.graphics;
公共类屏幕{
私人int宽度、高度;
公共整数[]像素;
int-xtime=0,ytime=50;
int计数器=0;
公共屏幕(整数宽度、整数高度){
这个。宽度=宽度;
高度=高度;
像素=新整数[宽度*高度];//0-50399=50400
}
公共空间清除(){
对于(int i=0;i=高度)中断;
对于(int x=0;x=宽度)中断;
像素[xtime+ytime*宽度]=0xff00ff;
}
}
}
}
}
}
Game.java
包com.cmnatic.cmnatic;
导入java.awt.Canvas;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.image.BufferStrategy;
导入java.awt.image.buffereImage;
导入java.awt.image.DataBufferInt;
导入javax.swing.JFrame;
导入com.cmnatic.cmnatic.graphics.Screen;
公共类游戏扩展画布实现可运行{
/**
* 
*/
私有静态最终长serialVersionUID=1L;
私有静态最终屏幕=空;
公共静态整数宽度=300;
公共静态整数高度=宽度/16*9;//168
公共静态int标度=3;
私有线程;
私有JFrame;
私有布尔运行=false;
私人屏幕;
私有BuffereImage=新的BuffereImage(宽度、高度、BuffereImage.TYPE_INT_RGB);
私有int[]像素=((DataBufferInt)image.getRaster().getDataBuffer()).getData();
公共游戏(){
尺寸尺寸=新尺寸(宽度*比例,高度*比例);
设置首选大小(大小);
屏幕=新屏幕(宽度、高度);
frame=新的JFrame();
}
公共同步的void start(){
运行=真;
线程=新线程(此“显示”);
thread.start();
}
公共同步无效停止(){
运行=错误;
试一试{
thread.join();
}捕捉(中断异常e){
e、 printStackTrace();
}
}
公开募捐{
while(running==true){
更新();
render();
}
}
公共无效更新(){
}
公共无效呈现(){
BufferStrategy bs=getBufferStrategy();
如果(bs==null){
创新战略(3);
返回;
}
screen.clear();
screen.render();
对于(int i=0;i
读取。具体来说,这句话是:为了在画布上执行自定义图形,必须重写paint方法

然后在游戏类(类型为
Canvas
)中重写(例如,实现)绘制。我认为它应该开始显示一些东西

// That is, something like this - 
public void paint(Graphics g) {
    super.paint(g);
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
}

你发布了两个屏幕类。你有多少?“我送你去gyazo linkg”我不知道gyazo linkg到底是什么,但听起来很痛苦。你能把它翻译成英语吗?我决定不加一个:P这是一个屏幕截图上传器,所以你在屏幕上选择一个区域,它就会上传,这样可以快速方便地分享。另外@ElliottFrisch我更新了代码,很抱歉。抱歉,如果不清楚的话。@ElliottFrisch似乎OP有一条新消息要告诉你。操作提示-添加
@PersonName
@
很重要)通知他们一条新评论。我不敢相信你编辑了这条评论,但却在其中留下了垃圾词(@ElliotFrisch我该怎么做?还是我现在太笨了?在Game
public void paint(Graphics g){super.paint(g);//DRAW HERE}
“构造函数调用必须是构造函数中的第一条语句”抱歉,没有像素在绘制:cAdd另一个问题,显示新代码…你在我留下的
draw Here
注释的地方真的画了什么吗?