Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.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 将画布拉伸到JFrame大小_Java_Swing_Canvas_Jframe - Fatal编程技术网

Java 将画布拉伸到JFrame大小

Java 将画布拉伸到JFrame大小,java,swing,canvas,jframe,Java,Swing,Canvas,Jframe,我的任务是编写一个128*128像素的游戏。所以我开始准备游戏Java框架,一切都很好:我有一个JFrame,里面是一块画布,我可以在上面画画。但是我不能正确设置尺寸。我必须使用128*128像素,但如果帧的大小相同,它就太小了,所以我需要放大它 但是如果我放大框架的尺寸,画布太小,无法再填充。如果我只是给它与帧相同的大小,那么我就不会再使用128*128像素了。那么,我怎样才能在我的小画布上画出一切正常的东西,然后再放大呢 package de.gaminggears.communitygam

我的任务是编写一个128*128像素的游戏。所以我开始准备游戏Java框架,一切都很好:我有一个JFrame,里面是一块画布,我可以在上面画画。但是我不能正确设置尺寸。我必须使用128*128像素,但如果帧的大小相同,它就太小了,所以我需要放大它

但是如果我放大框架的尺寸,画布太小,无法再填充。如果我只是给它与帧相同的大小,那么我就不会再使用128*128像素了。那么,我怎样才能在我的小画布上画出一切正常的东西,然后再放大呢

package de.gaminggears.communitygame;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;

import de.gaminggears.communitygame.display.Display;

public class Game implements Runnable{

private Display display;
public int width, height;
public String title;

private boolean running = false;
private Thread thread;

private BufferStrategy bs;
private Graphics g;


public Game(String title,int width, int height){
    this.height = height;
    this.width = width;
    this.title = title;

}

private void init(){
    display = new Display(title, width, height);
}

private void tick(){

}

private void render(){
    bs = display.getCanvas().getBufferStrategy();
    if(bs == null){
        display.getCanvas().createBufferStrategy(3);
        return;
    }
    g = bs.getDrawGraphics();
    Graphics2D g2d = (Graphics2D) g;
    //Clear Screen
    g2d.clearRect(0, 0, width, height);
    //Draw Here!

    g2d.setColor(Color.RED);
    g2d.fillRect(10, 50, 50, 70);
    g2d.fillRect(0, 0, 10, 10);

    //End Drawing

    g2d.scale(5, 5);
    bs.show();
    g.dispose();
}

public void run(){

    init();

    while(running){
        tick();
        render();
    }
    stop();
}


public synchronized void start(){
    if(running)
        return;
    running = true;
    thread = new Thread(this);
    thread.start();
}

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

}

使用Graphics2D对象很容易,只需使用..但我不想按对象缩放所有对象。我想准备画布,就像我想要它一样,然后简单地同时放大所有东西,但我不想一个对象一个对象地放大所有东西,这就是我所说的想法。当需要绘制时,在单个图形上绘制2D实例,并将所有内容绘制到同一实例。它将按要求生产。这是我用来画徽标的相同技术。打印时需要2000 x 2000像素,但这很难处理。所以我把它们设计成600 x 600,然后当我对结果满意时,我把整个东西放大到所需的尺寸。容易的好的,谢谢,我会这样尝试,并告诉你它是否有效:如果你看我的线程,你会看到我添加了一个新的代码,我已经注册。它仍然绘制矩形,但大小相同