我试图制作游戏并让文字像大多数RPG游戏一样出现在一个角色中。我在for循环的底部附近调用repaint(),但它一直运行到最后一次遍历for循环。这是整个班级。我正在使用displayMessage方法。请帮忙。 谢谢。
private class BattleTextArea extends JPanel{
private String atk1;
private String atk2;
private String atk3;
private String atk4;
private String messageWords;
private boolean messageDisplayed;
private boolean finishedMessage;
private int xSpot;
private String charAt;
public BattleTextArea(){
setAtk1("1");
setAtk2("2");
setAtk3("3");
setAtk4("4");
}
public void updateText(Pokemon poke){
setAtk1(poke.attacks.get(0).getName());
setAtk2(poke.attacks.get(1).getName());
setAtk3(poke.attacks.get(2).getName());
setAtk4(poke.attacks.get(3).getName());
repaint();
}
public void paintComponent(Graphics g){
System.out.println("begining of paint");
g.setColor(Color.WHITE);
g.fillRect(0, 0, 500, 225);
g.setColor(Color.BLACK);
g.drawRoundRect(0, 0, 489, 94, 10, 10);
if(!isMessageDisplayed()){
if(!isAttacksSelected()){
g.drawString("Fight", 325, 30);
g.drawString("Bag", 425, 30);
g.drawString("Party", 325, 75);
g.drawString("Run", 425, 75);
}else{
g.drawString(getAtk1() , 50, 30);
g.drawString(getAtk2(), 150, 30);
g.drawString(getAtk3(), 50, 75);
g.drawString(getAtk4(), 150, 75);
}
if(getSelectedButton() == 1){
g.drawRect(320, 15, 42, 22);
}else if(getSelectedButton() == 2){
g.drawRect(418, 15, 42, 22);
}else if(getSelectedButton() == 3){
g.drawRect(320, 60, 42, 22);
}else if(getSelectedButton() == 4){
g.drawRect(418, 60, 42, 22);
}
if(getSelectedButton() == 11){
g.drawRect(45, 15, 50, 22);
}else if(getSelectedButton() == 12){
g.drawRect(145, 15, 50, 22);
}else if(getSelectedButton() == 13){
g.drawRect(45, 60, 50, 22);
}else if(getSelectedButton() == 14){
g.drawRect(145, 60, 50, 22);
}
}else{
//Display message
/*int x = 20;
g.drawString(getMessageWords().substring(0, 1), 20,20);
for (int i = 1; i <= getMessageWords().length(); i++) {
try {
Thread.sleep(1000);//1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println("in for");
JComponent c = bta;
FontMetrics fm = c.getFontMetrics(c.getFont()); // or another font\
if(i != getMessageWords().length())
x += fm.stringWidth(getMessageWords().substring(i, i + 1));
else
x += fm.stringWidth(getMessageWords().substring(i));
if(i != getMessageWords().length())
g.drawString(getMessageWords().substring(i, i + 1), x,20);
else
g.drawString(getMessageWords().substring(i), x, 20);
}//for*/
//setFinishedMessage(true);
System.out.println("in paint");
System.out.println(getCharAt());
//System.out.println(getMessageWords().length());
g.drawString(getCharAt(), getXSpot(), 20);
System.out.println("in if");
}
}//paint comp
public void displayMessage(String message){
//setMessageWords(message);
setMessageDisplayed(true);
JComponent c = bta;
FontMetrics fm = c.getFontMetrics(c.getFont());
for(int i = 0; i < message.length(); i++){
setCharAt(message.substring(i, i + 1));
System.out.println("before if");
System.out.println(fm.stringWidth(getCharAt()));
System.out.println(getXSpot());
setXSpot(getXSpot() + fm.stringWidth(getCharAt()));
System.out.println(getXSpot());
System.out.println("right before paint");
repaint();
System.out.println("right after paint");
try {
Thread.sleep(1000);//1000 milliseconds is one second.
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
}//for
setFinishedMessage(true);
}
public String getAtk1() {
return atk1;
}
public void setAtk1(String atk1) {
this.atk1 = atk1;
}
public String getAtk2() {
return atk2;
}
public void setAtk2(String atk2) {
this.atk2 = atk2;
}
public String getAtk3() {
return atk3;
}
public void setAtk3(String atk3) {
this.atk3 = atk3;
}
public String getAtk4() {
return atk4;
}
public void setAtk4(String atk4) {
this.atk4 = atk4;
}
public boolean isMessageDisplayed() {
return messageDisplayed;
}
public void setMessageDisplayed(boolean messageDisplayed) {
this.messageDisplayed = messageDisplayed;
}
public String getMessageWords() {
return messageWords;
}
public void setMessageWords(String messageWords) {
this.messageWords = messageWords;
}
public boolean isFinishedMessage() {
return finishedMessage;
}
public void setFinishedMessage(boolean finishedMessage) {
this.finishedMessage = finishedMessage;
}
public int getXSpot() {
return xSpot;
}
public void setXSpot(int x) {
this.xSpot = x;
}
public String getCharAt() {
return charAt;
}
public void setCharAt(String charAt) {
this.charAt = charAt;
}
}//bta class