图形不会覆盖整个画布 [英] Graphics will not cover entire canvas

查看:141
本文介绍了图形不会覆盖整个画布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我知道:Canvas正确显示,由于黄色。但是,图形不会完全覆盖画布,我的法律阶层也不会识别它过去黑色区域的结束...



那么是什么原因导致这种情况发生?如何绘制我目前不可绘制的画布(黄色部分),或者我应该以另一种方式实现我的图形?



编辑:UI类创建一个画布和一个缓冲区,然后图形类接管并开始绘制它们,然而由于某种原因,它不能在黄色部分,处理与应用程序的红色立方体和墙壁碰撞的法律类也调节黄色区域一个有效的地方去。



主类

  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;
图形图形;
法律物理;
static int status;
boolean holdState;

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

public void launch(){
//音频声音= 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();

}
}

UI类

 包应用程序; 

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

public class UI extends Main {

JFrame mainWin;
画布墙
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();
}
}

图形类

 包应用程序; 

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);
}
}

法律类别

 包应用程序; 

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;
}
}
}


解决方案>

这是 Frame#setResizable 的错误。不要问我为什么,但它想添加 10 像素到框架的宽度和高度。



我知道的最好的解决方案是调用 setResizable BEFORE pack

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


What I know: Canvas is displayed properly, due to the yellow. However, graphics will not cover the canvas completely, nor will my law class recognized it past the end of the black area...

So what is causing this to happen? And how could I draw on my currently undrawable section of canvas(yellow part), or should I just implement my graphics another way?

EDIT: The UI class creates a canvas and a buffer, then the graphics class takes over and starts drawing on them, however for some reason it cannot in the yellow section, nor will the Law Class which handles collision with the red cube and walls of the app, regogize the yellow area as a valid place to go. Even through the same variables for dimensions, were used everywhere.

Main Class

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();

    }
}

UI Class

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();
    }
}

Graphics Class

    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);
        }
    }

Law Class

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;
        }
    }
}

解决方案

This is a bug with Frame#setResizable. Don't ask me why, but it wants to add about 10 pixels to the width and height of the frame.

The best solution I know is to call setResizable BEFORE pack

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

这篇关于图形不会覆盖整个画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆