Java graphics2D drawImage不使用timer.schedule

Java graphics2D drawImage不使用timer.schedule,java,swing,intellij-idea,drawimage,Java,Swing,Intellij Idea,Drawimage,我正在尝试用Java模拟Gameboy游戏。我试图每隔半秒左右将字母打印到文本框中,但是当我运行代码时,文本框绘制正确,但其中没有文本 文本框: package components; import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; import java.util.Timer; import java.util.TimerTask; public class Textbox extends E

我正在尝试用Java模拟Gameboy游戏。我试图每隔半秒左右将字母打印到文本框中,但是当我运行代码时,文本框绘制正确,但其中没有文本

文本框:

package components;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Timer;
import java.util.TimerTask;

public class Textbox extends Entity { //Only create one instance
private static String[] lines;
private static final int x = 1;
private static final int y = 97;
private Graphics2D graphics2D;
int lineNo = 0; //Used to count the line number when printing
int charNo = 0; //Used to count the character number when printing
int cx; //Used for the coordinates of each character printed
int cy; //Used for the coordinates of each character printed
boolean timerInitialized = false; //Used to indicate end of line when printing
java.util.Timer timer = new Timer(); //Used to control text speed
//java.util.TimerTask task;

private static volatile Textbox textbox = new Textbox();

public Textbox() {
    super(x, y);
    addLines();
}

public void draw(Graphics2D graphics2D) {
    this.graphics2D = graphics2D;
    this.graphics2D.drawImage(getImage(), x, y, null);
    if (!timerInitialized) {
        System.out.println("Scheduled..."); //For testing
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("Time's up!"); //For testing
                printChars(graphics2D, lines[lineNo]);
                timer.cancel();
            }
        }, 1000);
        timerInitialized = true;
    }
}

/*public void drawText(Image image, Point coordinates) {
    this.graphics2D.drawImage(image, coordinates.x, coordinates.y, null);
}*/

public Image getImage() {
    //graphics2D.drawString("Hello World", 100, 100); //Draws  a string
    ImageIcon imageIcon = new ImageIcon(
            "enterDirectoryHere"
                    + "\\src\\components\\Display\\Textbox.png");
    return imageIcon.getImage();
}

public void keyReleased(KeyEvent event) {
    int key = event.getKeyCode();

    if (key == KeyEvent.VK_ENTER) {
        if (GameFrame.screenPaused == true) {
            GameFrame.screenPaused = false;
        } else {
            GameFrame.screenPaused = true;
        }

    }
}

//FOR TESTING ONLY
public static void addLines() {
    lines = new String[] {"Hello", "World"};

}


/*
Make a boolean to indicate that line1 is full, and to continue on line 2 if
more text remains
Show cursor to indicate more text in lines
When pressing 'A' to continue text, translate text upward
If text exceeds boundaries, begin to cut off, starting from top, until gone

 */
/*public void printText(Graphics2D graphics2D) {
    if (lines != null) {
        //Assume that you already know the arrangement of words in the box
        //Starting point is 8, 112
        lineNo = 0;
        //endLine = false;
        while (lineNo < lines.length) {
            String line = lines[lineNo];
            if (line != null) {
                charNo = 0;
                for (int i = 0; i < line.length(); i++) {
                    printChars(graphics2D, line);
                }
            }
            lineNo++; //Move to the next line
            //line takes new line into account
            cx = 8;
        }
    }

}*/

public void printChars(Graphics2D graphics2D, String line) {
    if (charNo < line.length()) {
        if (lineNo == 0) {
            cy = 112;
        } else {
            cy = 128;
        }
        if (line.charAt(charNo) >= 'A' && line.charAt(charNo) <= 'Z') {
            Image image = getCharImage(line.charAt(charNo));
            graphics2D.drawImage(image, cx, cy, null);
            cx += image.getWidth(null);
        } else if (line.charAt(charNo) >= 'a' && line.charAt(charNo) <= 'z') {
            if (line.charAt(charNo) == 'b' || line.charAt(charNo) == 'd'
                    || line.charAt(charNo) == 'f' || line.charAt(charNo) == 'h'
                    || line.charAt(charNo) == 'k' || line.charAt(charNo) == 'l') {
                Image image = getCharImage(line.charAt(charNo));
                graphics2D.drawImage(image, cx, cy, null);
                cx += image.getWidth(null);
            } else if (line.charAt(charNo) == 'i' || line.charAt(charNo) == 'j'
                    || line.charAt(charNo) == 't') {
                cy += 1;
                //Add extra spaces for i, j, and l
                Image image = getCharImage(line.charAt(charNo));
                graphics2D.drawImage(image, cx, cy, null);
                cx += image.getWidth(null);
            } else {
                cy += 2;
                Image image = getCharImage(line.charAt(charNo));
                graphics2D.drawImage(image, cx, cy, null);
                cx += image.getWidth(null);
            }
        } else if (line.charAt(charNo) >= 32 && line.charAt(charNo) <= 95) {
            //Check for symbols and spaces
            if (line.charAt(charNo) == '_') {
                cx += 7;
            } else if (line.charAt(charNo) == '.') {
                cy += 5;
                Image image = getCharImage(line.charAt(charNo));
                graphics2D.drawImage(image, cx, cy, null);
                cx += image.getWidth(null);
            } else if (line.charAt(charNo) == ',') {

            } else if (line.charAt(charNo) == '/') {

            } else if (line.charAt(charNo) == '?') {

            } else if (line.charAt(charNo) == '!') {
                Image image = getCharImage(line.charAt(charNo));
                graphics2D.drawImage(image, cx, cy, null);
                cx += image.getWidth(null) + 8;
            } else if (line.charAt(charNo) == ']') {

            } else if (line.charAt(charNo) == '[') {

            } else if (line.charAt(charNo) == ':') {

            } else if (line.charAt(charNo) == ';') {

            } else if (line.charAt(charNo) == '-') {

            } else if (line.charAt(charNo) == '(') {

            } else if (line.charAt(charNo) == ')') {

            } else if (line.charAt(charNo) == '\'') {
                Image image = getCharImage(line.charAt(charNo));
                graphics2D.drawImage(image, cx, cy, null);
                cx += image.getWidth(null);
            }
        }
        cx += 2; //move two pixels to the right
        //Writes on the second half of the box if there is
        //more than 1 line
        if (lineNo == 0) {
            cy = 112;
        } else {
            cy = 128;
        }
        charNo++;
    } else {
        //charNo = 0;
        //lineNo++;
    }
    //Schedule the next letter here if there are more letters
    if (lineNo < lines.length || charNo < line.length()) {
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                printChars(graphics2D, lines[lineNo]);
                timer.cancel();
            }
        }, 200);
    }
}
import java.awt.event.KeyEvent;
import java.util.Timer;
import java.util.TimerTask;

public class Textbox extends Entity {

private static String[] lines;

private static final int x = 1;
private static final int y = 97;
private Graphics2D graphics2D;
int lineNo = 0; //Used to count the line number when printing
int charNo = 0; //Used to count the character number when printing
int cx; //Used for the coordinates of each character printed
int cy; //Used for the coordinates of each character printed
boolean timerInitialized = false; //Used to indicate end of line when printing
java.util.Timer timer = new Timer();

private static volatile Textbox textbox = new Textbox();

public Textbox() {
    super(x, y);
    addLines();
}

public void draw(Graphics2D graphics2D) {
    this.graphics2D = graphics2D;
    this.graphics2D.drawImage(getImage(), x, y, null);
    if (!timerInitialized) {
        /*System.out.println("Scheduled..."); //For testing
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("Time's up!"); //For testing
                printChars(graphics2D, lines[lineNo]);
                timer.cancel();
            }
        }, 1000);*/
        timerInitialized = true;
    }
}

/*public void drawText(Image image, Point coordinates) {
    this.graphics2D.drawImage(image, coordinates.x, coordinates.y, null);
}*/

public Image getImage() {
    //graphics2D.drawString("Hello World", 100, 100); //Draws  a string
    ImageIcon imageIcon = new ImageIcon(
            "C:\\EnterDirectoryHere"
                    + "\\src\\components\\Display\\Textbox.png");
    return imageIcon.getImage();

public void keyReleased(KeyEvent event) {

    int key = event.getKeyCode();

    if (key == KeyEvent.VK_ENTER) {
        if (GameFrame.screenPaused == true) {
            GameFrame.screenPaused = false;
        } else {
            GameFrame.screenPaused = true;
        }

    }
}

//FOR TESTING ONLY
public static void addLines() {
    lines = new String[] {"Hello", "World"};

}


/*
Make a boolean to indicate that line1 is full, and to continue on line 2 if
more text remains
Show cursor to indicate more text in lines
When pressing 'A' to continue text, translate text upward
If text exceeds boundaries, begin to cut off, starting from top, until gone

 */
public void printText(Graphics2D graphics2D) {
    if (lines != null) {
        //Assume that you already know the arrangement of words in the box
        //Starting point is 8, 112
        lineNo = 0;
        //endLine = false;
        while (lineNo < lines.length) {
            String line = lines[lineNo];
            if (line != null) {
                charNo = 0;
                for (int i = 0; i < line.length(); i++) {
                    printChars(graphics2D, line);
                }
            }
            lineNo++; //Move to the next line
            //line takes new line into account
            cx = 8;
        }
    }

}

public void printChars(Graphics2D graphics2D, String line) {
    if (charNo < line.length()) {
        if (lineNo == 0) {
            cy = 112;
        } else {
            cy = 128;
        }
        if (line.charAt(charNo) >= 'A' && line.charAt(charNo) <= 'Z') {
            Image image = getCharImage(line.charAt(charNo));
            graphics2D.drawImage(image, cx, cy, null);
            cx += image.getWidth(null);
        } else if (line.charAt(charNo) >= 'a' && line.charAt(charNo) <= 'z') {
            if (line.charAt(charNo) == 'b' || line.charAt(charNo) == 'd'
                    || line.charAt(charNo) == 'f' || line.charAt(charNo) == 'h'
                    || line.charAt(charNo) == 'k' || line.charAt(charNo) == 'l') {
                Image image = getCharImage(line.charAt(charNo));
                graphics2D.drawImage(image, cx, cy, null);
                cx += image.getWidth(null);
            } else if (line.charAt(charNo) == 'i' || line.charAt(charNo) == 'j'
                    || line.charAt(charNo) == 't') {
                cy += 1;
                //Add extra spaces for i, j, and l
                Image image = getCharImage(line.charAt(charNo));
                graphics2D.drawImage(image, cx, cy, null);
                cx += image.getWidth(null);
            } else {
                cy += 2;
                Image image = getCharImage(line.charAt(charNo));
                graphics2D.drawImage(image, cx, cy, null);
                cx += image.getWidth(null);
            }
        } else if (line.charAt(charNo) >= 32 && line.charAt(charNo) <= 95) {
            //Check for symbols and spaces
            if (line.charAt(charNo) == '_') {
                cx += 7;
            } else if (line.charAt(charNo) == '.') {
                cy += 5;
                Image image = getCharImage(line.charAt(charNo));
                graphics2D.drawImage(image, cx, cy, null);
                cx += image.getWidth(null);
            } else if (line.charAt(charNo) == ',') {

            } else if (line.charAt(charNo) == '/') {

            } else if (line.charAt(charNo) == '?') {

            } else if (line.charAt(charNo) == '!') {
                Image image = getCharImage(line.charAt(charNo));
                graphics2D.drawImage(image, cx, cy, null);
                cx += image.getWidth(null) + 8;
            } else if (line.charAt(charNo) == ']') {

            } else if (line.charAt(charNo) == '[') {

            } else if (line.charAt(charNo) == ':') {

            } else if (line.charAt(charNo) == ';') {

            } else if (line.charAt(charNo) == '-') {

            } else if (line.charAt(charNo) == '(') {

            } else if (line.charAt(charNo) == ')') {

            } else if (line.charAt(charNo) == '\'') {
                Image image = getCharImage(line.charAt(charNo));
                graphics2D.drawImage(image, cx, cy, null);
                cx += image.getWidth(null);
            }
        }
        cx += 2; //move two pixels to the right
        //Writes on the second half of the box if there is
        //more than 1 line
        if (lineNo == 0) {
            cy = 112;
        } else {
            cy = 128;
        }
        charNo++;
    } else {
        //charNo = 0;
        //lineNo++;
    }
    //Schedule the next letter here if there are more letters
    if (lineNo < lines.length || charNo < line.length()) {
        /*timer.schedule(new TimerTask() {
            @Override
            public void run() {
                printChars(graphics2D, lines[lineNo]);
                timer.cancel();
            }
        }, 200);*/
    }
}
封装组件;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.KeyEvent;
导入java.util.Timer;
导入java.util.TimerTask;
公共类文本框扩展实体{//仅创建一个实例
私有静态字符串[]行;
专用静态最终整数x=1;
专用静态最终整数y=97;
专用图形2d图形2d;
int lineNo=0;//用于打印时计算行号
int charNo=0;//用于打印时计算字符数
int cx;//用于打印的每个字符的坐标
int cy;//用于打印的每个字符的坐标
boolean timerInitialized=false;//用于在打印时指示行尾
java.util.Timer Timer=new Timer();//用于控制文本速度
//java.util.TimerTask任务;
私有静态易失性Textbox Textbox=new Textbox();
公共文本框(){
super(x,y);
添加行();
}
公共作废绘图(Graphics2D Graphics2D){
this.graphics2D=graphics2D;
this.graphics2D.drawImage(getImage(),x,y,null);
如果(!timerInitialized){
System.out.println(“预定…”);//用于测试
timer.schedule(新TimerTask(){
@凌驾
公开募捐{
System.out.println(“时间到了!”;//用于测试
打印字符(图形2d,行[lineNo]);
timer.cancel();
}
}, 1000);
timerInitialized=true;
}
}
/*公共空白绘图文本(图像、点坐标){
这个.graphics2D.drawImage(图像,坐标.x,坐标.y,空);
}*/
公共映像getImage(){
//graphics2D.drawString(“Hello World”,100100);//绘制字符串
ImageIcon ImageIcon=新的ImageIcon(
“enterDirectoryHere”
+“\\src\\components\\Display\\Textbox.png”);
返回imageIcon.getImage();
}
公共无效密钥已释放(KeyEvent事件){
int key=event.getKeyCode();
if(key==KeyEvent.VK_ENTER){
如果(GameFrame.screenPaused==true){
GameFrame.screenspaused=false;
}否则{
GameFrame.screenspaused=true;
}
}
}
//仅供测试
公共静态void addLines(){
lines=新字符串[]{“Hello”,“World”};
}
/*
生成一个布尔值以指示第1行已满,如果已满,则在第2行继续
还有更多的文本
显示光标以指示行中的更多文本
按“A”继续文字时,向上翻译文字
如果文本超出边界,则从顶部开始截断,直到消失
*/
/*公共无效打印文本(Graphics2D Graphics2D){
如果(行!=null){
//假设您已经知道框中单词的排列方式
//起点是8112
lineNo=0;
//endLine=false;
while(lineNo如果(line.charAt(charNo)>='A'&&line.charAt(charNo)='A'&&line.charAt(charNo)=32&&line.charAt(charNo)我意识到了问题所在。我没有在每次调用draw()时更新要显示的文本。通过这样做,我能够得到想要的结果:

public void draw(Graphics2D graphics2D) {
    graphics2D.drawImage(getImage(), x, y, null);
    //Every time draw is called, ensure that the coordinates are the same
    //each time text is redrawn
    if (!display.isEmpty()) { //This will only print when characters have
        //been added every second
        for (int i = 0; i < display.size(); i++) {
            drawText(graphics2D, display.get(i).charAt(0), coordinates.get(i));
        }
    }
    if (!timerInitialized && line1 != null) { //This only allows the timer to be set once
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                addTextToDisplay(lines[0]);
            }
        }, 100);
        timerInitialized = true;
    }
}
public void draw(Graphics2D Graphics2D){
graphics2D.drawImage(getImage(),x,y,null);
//每次调用draw时,确保坐标相同
//每次重新绘制文本时
如果(!display.isEmpty()){//,则仅当字符数为
//每秒钟添加一次
对于(int i=0;i

我使用了两个ArrayList,display和coordinates。display列出了要打印的字母,并在屏幕上标记了该字符的坐标。

我意识到了问题所在。我没有在每次调用draw()时更新要显示的文本。通过这样做,我能够得到想要的结果:

public void draw(Graphics2D graphics2D) {
    graphics2D.drawImage(getImage(), x, y, null);
    //Every time draw is called, ensure that the coordinates are the same
    //each time text is redrawn
    if (!display.isEmpty()) { //This will only print when characters have
        //been added every second
        for (int i = 0; i < display.size(); i++) {
            drawText(graphics2D, display.get(i).charAt(0), coordinates.get(i));
        }
    }
    if (!timerInitialized && line1 != null) { //This only allows the timer to be set once
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                addTextToDisplay(lines[0]);
            }
        }, 100);
        timerInitialized = true;
    }
}
public void draw(Graphics2D Graphics2D){
graphics2D.drawImage(getImage(),x,y,null);
//每次调用draw时,确保坐标相同
//每次重新绘制文本时
如果(!display.isEmpty()){//,则仅当字符数为
//每秒钟添加一次
对于(int i=0;i
我使用了两个数组列表,display和coordinates。display列出要打印的字母,coordinates在屏幕上标记该字符的坐标。

您有一个“while循环”。在循环完成之前,GUI无法重新绘制自身
public void draw(Graphics2D graphics2D) {
    graphics2D.drawImage(getImage(), x, y, null);
    //Every time draw is called, ensure that the coordinates are the same
    //each time text is redrawn
    if (!display.isEmpty()) { //This will only print when characters have
        //been added every second
        for (int i = 0; i < display.size(); i++) {
            drawText(graphics2D, display.get(i).charAt(0), coordinates.get(i));
        }
    }
    if (!timerInitialized && line1 != null) { //This only allows the timer to be set once
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                addTextToDisplay(lines[0]);
            }
        }, 100);
        timerInitialized = true;
    }
}