我一直在尝试编写一个程序或“游戏”代码,根据用户选择的方式转移到新的场景。用户可以选择“A”或“B”。为了做到这一点,我需要显示用户需要的信息,但我无法删除过去的对话,导致重叠文本并使其无法阅读。
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Window extends JFrame{
private static final long serialVersionUID = 1L;
public static String name, choice;
public static BufferedImage wizard;
public static int width = 640;
public static int height = 400;
static String write =
"You will be entering a new world now!\nOne where your decision may either save you or cost you your life.\nYou will be given two options to choose from.\nPlease enter the letters 'A' or 'B' and if you wish to leave, '!'.\npress 'ENTER' to begin!";
public Window() { //first method that gets called from other class
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Yasaman's game");
setSize(width, height);
setResizable(true);
setLocation(0,0);
setVisible(true);
setLayout(new FlowLayout());
setAlwaysOnTop(true);
readWizard();
name = JOptionPane.showInputDialog("What is your name");
repaint();
}
private void readWizard() { //read wizard from file
try {
wizard = ImageIO.read(new File("magic.png"));
} catch (IOException e) {
System.out.println("Somebody ate my Wizard!");
}
}
public void paint(Graphics g) //this is the god method, everything that you want to put onto the screen goes in here
{
int sBx = 100;
int sBy = 100;
int sBwid = 400;
int sBhei = 200;
int sBcurve = 50;
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(new Color(0, 255, 155));
if(wizard != null) {
g2.drawImage(wizard,0,0, this);
}else {
System.out.println("Wizard was eaten alive, have a box");
}
g2.fillRoundRect(sBx, sBy, sBwid, sBhei, sBcurve, sBcurve);
g2.fillRect(100, 100, 100, 100);
g2.setColor(new Color(0,0,0));
//THIS IS WHERE THE PROBLEM BEGINS
g2.setBackground(new Color(255, 255, 255));
int y = 170;
if (write!= null)
for(String line: write.split("\n"))
g2.drawString(line, 130, y+= g.getFontMetrics().getHeight());
//g2.setBackground(new Color(255, 255, 255));
senario1();
y = 170;
if (write!= null)
for(String line: write.split("\n"))
g2.drawString(line, 130, y+= g.getFontMetrics().getHeight());
choice = JOptionPane.showInputDialog("Would you A. Stay Home , B. Go to school ?");
if (choice.equalsIgnoreCase("B")){
//g2.setBackground(new Color(255, 255, 255));
senario2();
y = 170;
if (write!= null)
for(String line: write.split("\n"))
g2.drawString(line, 130, y+= g.getFontMetrics().getHeight());
}
}
public static void senario1(){
write = "Today is the day.\nThe day you can finally use your magic to hustle through life.\nBefore you use your powers, you must learn how to use them!\nLet's get started!\n";
}
public static void senario2(){
write = "You are so lazy! It's okay though if you had went to school\n, due to many unfortunate events your life would have ended... \n\nOr maybe not...";
}
}
我如何删除过去的文字以显示新文字。 fisr显示和第二个显示也因某种原因合并在一起。
谢谢你,请帮助我,我真的很喜欢它!
答案 0 :(得分:1)
首先,paint函数中的代码很乱。该函数用于绘制porpuses,而不是用于很多if ... else语句(因为每次调整框架时,都会调用paint方法,这会导致你的应用非常滞后)。无论如何,我创建了一个单独的课程,所以每当你点击"输入"写入值更改:
class adapter extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
Draw.write = nextScenario();
repaint();
}
}
}
然后我已经将一个FocusListener添加到Window构造函数中,这样您就可以在执行其他操作时按Enter键,并且该应用程序没有做某事&#34;奇怪&#34;:< / p>
public Draw() { //first method that gets called from other class
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Yasaman's game");
write =
"You will be entering a new world now!\nOne where your decision may either save you or cost you your life.\nYou will be given two options to choose from.\nPlease enter the letters 'A' or 'B' and if you wish to leave, '!'.\npress 'ENTER' to begin!";
setSize(width, height);
setResizable(true);
adapter adapter = new adapter();
addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
addKeyListener(adapter);
}
@Override
public void focusLost(FocusEvent e) {
removeKeyListener(adapter);
}
});
setLocation(0,0);
setVisible(true);
readWizard();
name = JOptionPane.showInputDialog("What is your name?");
repaint();
}
然后,我创建了一个&#34; nextscenario&#34;以简单的方式在场景之间切换的功能。 private static int scenario = 0;
以及从KeyListener类调用的方法:
private static String nextScenario(){
switch(scenario) {
case 0:
scenario++; return senario1();
case 1:
scenario++; return senario2();
default:
return null;
}
}
最后,油漆功能成为了这个&#34;简短&#34;为了更有效率:
public void paint(Graphics g) //this is the god method, everything that you want to put onto the screen goes in here
{
int sBx = 100;
int sBy = 100;
int sBwid = 400;
int sBhei = 200;
int sBcurve = 50;
int y = 170;
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(new Color(0, 255, 155));
if (wizard != null) {
g2.drawImage(wizard, 0, 0, this);
} else {
System.out.println("Wizard was eaten alive, have a box");
}
g2.fillRoundRect(sBx, sBy, sBwid, sBhei, sBcurve, sBcurve);
g2.fillRect(100, 100, 100, 100);
g2.setColor(new Color(0, 0, 0));
g2.setBackground(new Color(255, 255, 255));
for (String line : write.split("\n"))
g2.drawString(line, 130, y += g.getFontMetrics().getHeight());
}
如果有任何问题请告诉我,我会向您解决。如果它解决了你的问题,请标记为答案! :d