Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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_Canvas_Graphics - Fatal编程技术网

Java 图形不会覆盖整个画布

Java 图形不会覆盖整个画布,java,canvas,graphics,Java,Canvas,Graphics,据我所知:画布显示正确,因为它是黄色的。然而,图形不会完全覆盖画布,我的法律课也不会在黑色区域结束后认出它 那么是什么导致了这种情况?我该如何在画布上当前无法绘制的部分(黄色部分)上绘制,还是应该以另一种方式实现图形 编辑:UI类创建一个画布和一个缓冲区,然后graphics类接管并开始在其上绘制,但是由于某些原因,它不能在黄色部分中进行绘制,处理与应用程序的红色立方体和墙的碰撞的Law类也不会将黄色区域作为一个有效的地方重新进行oggize。即使通过相同的维度变量,也可以在任何地方使用 主类

据我所知:画布显示正确,因为它是黄色的。然而,图形不会完全覆盖画布,我的法律课也不会在黑色区域结束后认出它

那么是什么导致了这种情况?我该如何在画布上当前无法绘制的部分(黄色部分)上绘制,还是应该以另一种方式实现图形

编辑:UI类创建一个画布和一个缓冲区,然后graphics类接管并开始在其上绘制,但是由于某些原因,它不能在黄色部分中进行绘制,处理与应用程序的红色立方体和墙的碰撞的Law类也不会将黄色区域作为一个有效的地方重新进行oggize。即使通过相同的维度变量,也可以在任何地方使用

主类

package app;

public class Main {

    static final int X = 1024;
    static final int Y = 680;
    static final int sanic = 10;
    int fps = 0;
    int frames = 0;
    long totalTime = 0;
    long curTime = System.currentTimeMillis();
    long lastTime = curTime;
    static int[] pos;
    Graphics graphics;
    Law physics;
    static int status;
    boolean holdState;

    public Main() {
        pos = new int[5];
        pos[1] = X;
        pos[2] = Y;
    }

    public void launch() {
        // Audio sound = new Audio();
        graphics = new Graphics();
        physics = new Law();
        graphics.draw();
        // sound.play();
        handle();
    }

    public void update() {
        graphics.cache();
        physics.check();
        graphics.render();
        try {
            Thread.sleep(10);
        } catch (Exception e) {
        } finally {
            graphics.dump();
        }
    }

    public void handle() {
        while (!isOver()) {
            if (!isPaused()) {
                update();
            }
        }
    }

    boolean isOver() {
        if (status == 1) {
            status = 0;
            return true;
        }
        return false;
    }

    boolean isPaused() {
        if (status == 2) {
            status = 0;
            if (!holdState) {
                holdState = true;
                pos[3] = pos[1];
                pos[4] = pos[2];
            } else if (holdState) {
                holdState = false;
                pos[1] = pos[3];
                pos[2] = pos[4];
            }
        }
        return holdState;
    }

    public static void main(String[] args) {
        Main game = new Main();
        game.launch();

    }
}
用户界面类

package app;

import java.awt.*;
import java.awt.image.*;
import java.net.URL;
import javax.swing.*;

public class UI extends Main {

    JFrame mainWin;
    Canvas wall;
    URL pic;
    Toolkit kit;
    Image img;
    BufferStrategy Buffer;
    Graphics2D shell;
    Graphics2D ball;

    public UI() {
        mainWin = new JFrame("Game");
        wall = new Canvas();
        wall.addKeyListener(new Input());
    }

    void draw() {
        frame();
        icon();
        canvas();
        show();
        prep();
    }

    void frame() {
        mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainWin.setBackground(Color.BLUE);
        mainWin.setIgnoreRepaint(true);
    }

    void icon() {
        pic = ClassLoader.getSystemResource("res/app.png");
        kit = Toolkit.getDefaultToolkit();
        img = kit.createImage(pic);
        mainWin.setIconImage(img);

    }

    void canvas() {
        wall.setBackground(Color.YELLOW);
        wall.setIgnoreRepaint(true);
        wall.setBounds(0, 0, X, Y);
    }

    void show() {
        mainWin.add(wall);
        mainWin.pack();
        mainWin.setResizable(false);
        mainWin.setLocationRelativeTo(null);
        mainWin.setVisible(true);
    }

    void prep() {
        wall.createBufferStrategy(2);
        Buffer = wall.getBufferStrategy();
    }
}
图形类

    package app;

    import java.awt.*;

    public class Graphics extends UI {
    public void render() {
        mask();
        player();
        fps();
        Buffer.show();
    }

    void cache() {
        shell = (Graphics2D) Buffer.getDrawGraphics();
        ball = (Graphics2D) Buffer.getDrawGraphics();
    }

    void dump() {
        shell.dispose();
        ball.dispose();
    }

    void mask() {
        shell.setColor(Color.black);
        shell.fillRect(0, 0, X, Y);
    }

    void fps() {
        lastTime = curTime;
        curTime = System.currentTimeMillis();
        totalTime += curTime - lastTime;
        if (totalTime > 1000) {
            totalTime -= 1000;
            fps = frames;
            frames = 0;
        }
        frames++;
        shell.setColor(Color.GREEN);
        shell.setFont(new Font("Courier New", Font.PLAIN, 12));
        shell.drawString(String.format("FPS: %s", fps), 20, 20);
    }

      void player() {
            ball.setColor(Color.RED);
            ball.fillRect(pos[1], pos[2], 32, 32);
        }
    }
法律课

package app;

public class Law extends Main {

    public void check() {
        out();
    }

    void out() {
        if (pos[1] < 0) {
            pos[1] = 0;
        }
        if (pos[2] < 0) {
            pos[2] = 0;
        }
        if (pos[1] > X - 32) {
            pos[1] = X - 32;
        }
        if (pos[2] > Y - 32) {
            pos[2] = Y - 32;
        }
    }
}
package应用程序;
公共阶级法扩展了主要的{
公共作废检查(){
out();
}
作废{
如果(位置[1]<0){
pos[1]=0;
}
如果(位置[2]<0){
pos[2]=0;
}
如果(位置[1]>X-32){
位置[1]=X-32;
}
如果(位置[2]>Y-32){
pos[2]=Y-32;
}
}
}

这是一个带有
Frame#setresizeable
的bug。不要问我为什么,但它想在帧的宽度和高度上增加大约10个像素

我所知道的最佳解决方案是在
pack

void show() {
    mainWin.add(wall);
    mainWin.setResizable(false); // Call me first
    mainWin.pack();
    mainWin.setLocationRelativeTo(null);
    mainWin.setVisible(true);
}

您没有正确地与Swing交互,并且您正在使用默认的
图形
对象执行不安全的操作,您不应该触摸该对象。以下是需要发生的事情的基本情况

  • JFrame mainFrame=new JFrame()
  • MyCanvas MyCanvas=newmycanvas()
  • mainFrame.getContentPane().add(myCanvas,BorderLayout.CENTER)
  • mainFrame.setVisible(true)
  • 新线程(myThread).start()
  • Main.Main()
    返回
MyCanvas需要如下所示:

public class MyCanvas extends Canvas {
    public void paint(Graphics g) {
        // drawing code goes here
    } // g is now no longer valid.  Don't hold onto it or dispose it or anything.
}
以下是物理更新线程:

public class MyThread implements Runnable {
    public void run() {
        while (keepRunning()) {
            physics.update();
            myCanvas.repaint(); // myCanvas.paint() will eventually be called
            sleep(10);
        }
        System.exit(0);
    }
}

您可能希望向这些对象添加构造函数,并将引用传递给它们需要引用的其他对象。

您能更详细地描述您的问题吗?它实际上应该是什么样子的?我不想搜遍整个源代码来了解发生了什么。K,添加了更好的描述。这是
setResizble
的一个问题,它想在宽度和高度上添加大约10个像素,就是这样。。。谢谢你,我猜这是在为虚构的滚动条窃取空间?