我正在尝试扩展 Uni 提供给我们的 Java 程序,但我无法让该程序使用新 class 中的方法
I'm trying to extend a Java program given to us at Uni, and I can't get the program to use the methods in the new class
所以,首先我是一个非常业余的 Java 程序员,它只是一个模块,然后就结束了。
条件是覆盖我们的 TurtleGraphics 程序中预定义的 'about()' 命令。
如果我将覆盖放在 GUI class 和其他所有内容中,我可以让它工作,但是当我尝试创建一个 'ExtendedTurtleGraphics' class 并尝试调用它时,它什么都不做,不抛出错误,什么也不做。
这是扩展的Class和内容
public class ExtendedTurtleGraphics extends TurtleGraphics {
TurtleGraphics TG = new TurtleGraphics();
theTurtle turtle = new theTurtle();
String Command;
@Override
public void about() {
super.about();
System.out.print("Testing..."); //Working!
Command = "backward 100";
turtle.getCommand(Command, this);
Command = "penDown";
turtle.getCommand(Command, this);
Command = "forward 200";
turtle.getCommand(Command, this);
Command = "turnright 120";
turtle.getCommand(Command, this);
Command = "forward 50";
turtle.getCommand(Command, this);
}
/*@Override
public void circle(int radius) {
super.circle(radius);
}*/
}
这是整个 GUI class,关于部分在后面
public class myGUI extends JPanel{
static JTextField commandbox;
boolean isSaved = false;
TurtleGraphics TG = new TurtleGraphics();
theTurtle turtle = new theTurtle();
TurtleGraphics ETG = new ExtendedTurtleGraphics();
public myGUI()
{
JFrame MFrame = new JFrame();
setLayout(new BorderLayout());
setBackground(Color.white);
commandbox = new JTextField(5);
MenuBr mb = new MenuBr();
MFrame.setJMenuBar(mb);
MFrame.add(commandbox, BorderLayout.SOUTH);
commandbox.addActionListener(new MyActionListener());
MFrame.add(TG);
MFrame.setSize(640, 480);
MFrame.setVisible(true);
MFrame.validate();
MFrame.repaint();
ExtendedTurtleGraphics ETG;
}
public static void infoBox(String infoMessage, String titleBar)
{
JOptionPane.showMessageDialog(null, infoMessage, titleBar, JOptionPane.INFORMATION_MESSAGE);
}
class MenuBr extends JMenuBar implements ActionListener {
JMenu fileMnu = new JMenu("File");
JMenuItem newMnuI = new JMenuItem("New");
JMenuItem openMnuI = new JMenuItem("Load");
JMenuItem saveMnuI = new JMenuItem("Save");
JMenuItem closeMnuI = new JMenuItem("Exit");
JMenu helpMnu = new JMenu("Help");
JMenuItem aboutMnuI = new JMenuItem("About");
MenuBr() {
add(fileMnu);
fileMnu.add(newMnuI);
fileMnu.add(openMnuI);
fileMnu.add(saveMnuI);
fileMnu.add(closeMnuI);
newMnuI.addActionListener(this);
openMnuI.addActionListener(this);
saveMnuI.addActionListener(this);
closeMnuI.addActionListener(this);
add(helpMnu);
helpMnu.add(aboutMnuI);
aboutMnuI.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == newMnuI) {
TG.reset();
TG.turnLeft(90);
TG.clear();
TG.penDown();
}
if (e.getSource() == openMnuI) {
if (isSaved == true) {
String loadName = JOptionPane.showInputDialog("Enter Load File Path (Excluding .png) :");
loadName = loadName + ".png";
try {
BufferedImage loadedImage = ImageIO.read(new File(loadName));
TG.setBufferedImage(loadedImage);
} catch (IOException exception) {
System.out.println(exception.getMessage());
}
} else if (isSaved == false) {
int n = JOptionPane.showConfirmDialog(null, "Save before loading?", "Are you sure?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
System.out.println(n);
if (n == 0) {
Graphics g = TG.getGraphicsConext();
BufferedImage i = TG.getBufferedImage();
String fileName = JOptionPane.showInputDialog("Enter Save File Path (Excluding .png): ");
fileName = fileName + ".png";
File file = new File(fileName);
try {
ImageIO.write(i, "png", file);
} catch (IOException el) {
el.printStackTrace();
}
String loadName = JOptionPane.showInputDialog("Enter Load File Path (Excluding .png) :");
loadName = loadName + ".png";
try {
BufferedImage loadedImage = ImageIO.read(new File(loadName));
TG.setBufferedImage(loadedImage);
} catch (IOException exception) {
System.out.println(exception.getMessage());
}
} else if (n == 1) {
String loadName = JOptionPane.showInputDialog("Enter Load File Path (Excluding .png) :");
loadName = loadName + ".png";
try {
BufferedImage loadedImage = ImageIO.read(new File(loadName));
TG.setBufferedImage(loadedImage);
} catch (IOException exception) {
System.out.println(exception.getMessage());
}
}
}
}
if (e.getSource() == saveMnuI) {
isSaved = true;
Graphics g = TG.getGraphicsConext();
BufferedImage i = TG.getBufferedImage();
String fileName = JOptionPane.showInputDialog("Enter File Path (Excluding .png): ");
fileName = fileName + ".png";
File file = new File (fileName);
try{
ImageIO.write(i, "png", file);
} catch (IOException el) {
el.printStackTrace();
}
}
if (e.getSource() == closeMnuI) {
if (isSaved == true)
{
System.exit(0);
}
else
{
int n = JOptionPane.showConfirmDialog(null, "Save before quitting?", "Are you sure?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
System.out.println(n);
if (n == 0)
{
Graphics g = TG.getGraphicsConext();
BufferedImage i = TG.getBufferedImage();
String fileName = JOptionPane.showInputDialog("Enter File Path (Excluding .png): ");
fileName = fileName + ".png";
File file = new File (fileName);
try{
ImageIO.write(i, "png", file);
} catch (IOException el) {
el.printStackTrace();
}
System.exit(0);
}
else if (n == 1)
{
System.exit(0);
}
}
}
这是关于部分
if (e.getSource() == aboutMnuI) {
ETG.clear();
ETG.about();
ImageIcon icon = new ImageIcon("FilePath");
JOptionPane.showMessageDialog(null, "Turtle Graphics\nJoe Bloggs\n4FS1\nOOP Assessment 2020", "About", JOptionPane.INFORMATION_MESSAGE, icon);
}
}
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == commandbox) {
String command = commandbox.getText();
isSaved = false;
turtle.getCommand(command, TG);
commandbox.setText("");
}
}
}
}
theTurtle Java Class.
public class theTurtle {
//ArrayList<String> oldCommands = new ArrayList<>();
public void getCommand(String command, TurtleGraphics TG)
{
String[]entries = command.split(" ");
int var = -1;
String norm = entries[0];
if (entries.length == 2)
{
var = Integer.parseInt(entries[1]);
}
getMethod(norm, var, TG);
}
private void getMethod(String command, int var, TurtleGraphics TG)
{
if (command.toLowerCase().equals("penup"))
{
TG.penUp();
}
else if (command.toLowerCase().equals("pendown"))
{
TG.penDown();
}
else if (command.toLowerCase().equals("turnleft"))
{
if (var==-1)
{
TG.turnLeft();
}
else{
TG.turnLeft(var);
}
}
else if (command.toLowerCase().equals("turnright"))
{
if (var==-1)
{
TG.turnRight();
}
else{
TG.turnRight(var);
}
}
else if (command.toLowerCase().equals("forward"))
{
if (var == -1 || var <= 0)
{
errorEcho(1);
}
else{
TG.forward(var);
}
}
else if (command.toLowerCase().equals("backward"))
{
if (var == -1 || var <= 0)
{
errorEcho(1);
}
else{
TG.turnRight(180);
TG.forward(var);
TG.turnRight(180);
}
}
else if (command.toLowerCase().equals("black"))
{
TG.setPenColour(Color.black);
}
else if (command.toLowerCase().equals("red"))
{
TG.setPenColour(Color.red);
}
else if (command.toLowerCase().equals("green"))
{
TG.setPenColour(Color.green);
}
else if (command.toLowerCase().equals("reset"))
{
TG.reset();
TG.turnLeft(90);
TG.clear();
TG.penDown();
}
else if (command.toLowerCase().equals("circle"))
{
if (var == -1 || var <= 0)
{
errorEcho(1);
}
else{
TG.circle(var);
}
}
else{
myGUI.infoBox("Invalid Command", "ERROR!");
}
}
private void errorEcho(int errorCode)
{
myGUI.infoBox("Missing Parameters", "ERROR!");
}
}
The variables that are show stepping through about
- 删除
TurtleGraphics TG = new TurtleGraphics();
- 将
TG
替换为this
这应该会让您朝着正确的方向前进。
@编辑
好吧,我们没有 TurtleGraphics 的代码,所以我们不能 100% 判断但是:
首先:在你的 gui 类中定义
TurtleGraphics TG = new TurtleGraphics();
theTurtle turtle = new theTurtle();
TurtleGraphics ETG = new ExtendedTurtleGraphics();
所以你在这里创建的结构是这样的
TurtleGraphics TG
theTurtle turtle
ExtendedTurtleGraphics ETG {
TurtleGraphics TG // You define a new instanct of TurtleGraphics called TG in the ETG object
theTurle turtle // You defined a new instance of theTurtle called turtle in the ETG object
}
请注意,您现在有两个 turtle 对象和两个 TurtleGraphicsObject(或者实际上是三个,因为 ETG 在扩展 TurtleGraphics 后也是一个 TurtleGraphics)
好吧,那么在 ExtendedTurtleGraphics 的 "about" 函数中,您 运行 对 "turtle" 命令,但请注意,这些命令是在以下海龟上执行的:
TurtleGraphics TG
theTurtle turtle
ExtendedTurtleGraphics ETG {
TurtleGraphics TG
theTurle turtle // <- this one
}
所以基本上,当您在 ETG 上 运行 "about" 方法时,您在 "myGUI" class 中定义的 "turtle" 不会改变。
这解释了为什么控制台输出在 about 方法中工作,但您看不到任何事情发生。
解决方法是删除 MyGui 初始化时的两行:
TurtleGraphics TG = new TurtleGraphics(); // remove this one
theTurtle turtle = new theTurtle(); // remove this one
TurtleGraphics ETG = new ExtendedTurtleGraphics();
然后从 ExtendedTurtleGraphics 中删除这一行:
TurtleGraphics TG = new TurtleGraphics(); // Remove this one
在 ExtendedTurtleGraphics 中创建 Turtle public。
在这之后你的结构现在看起来像这样:
我的图形用户界面:
ExtendedTurtleGraphic ETG { // ExtendedTurtleGraphics 对象是一个 TurtleGraphic
乌龟
}
现在,在 myGui class 中,将所有出现的 "TG" 替换为 "ETG",将所有出现的 "turtle" 替换为 "ETG.turtle"
这意味着每次您想对海龟进行某些操作时,您都可以从 ETG 访问海龟实例,然后在调用 ExtendedTurtleGraphics 中的 "about" 方法时修改同一个对象。
@二次编辑:
使用参数 ETG 向 MyActionListener 添加构造函数,如下所示:
class MyActionListener implements ActionListener {
private ExtendedTurtleGraphics ETG;
public MyActionListener(ExtendedTurtleGraphics ETG){
this.ETG = ETG;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == commandbox) {
String command = commandbox.getText();
isSaved = false;
ETG.turtle.getCommand(command, ETG);
commandbox.setText("");
}
}
}
在 MyGui 中更改 Action 侦听器注册,如下所示:
commandbox.addActionListener(new MyActionListener(ETG));
所以,首先我是一个非常业余的 Java 程序员,它只是一个模块,然后就结束了。
条件是覆盖我们的 TurtleGraphics 程序中预定义的 'about()' 命令。
如果我将覆盖放在 GUI class 和其他所有内容中,我可以让它工作,但是当我尝试创建一个 'ExtendedTurtleGraphics' class 并尝试调用它时,它什么都不做,不抛出错误,什么也不做。
这是扩展的Class和内容
public class ExtendedTurtleGraphics extends TurtleGraphics {
TurtleGraphics TG = new TurtleGraphics();
theTurtle turtle = new theTurtle();
String Command;
@Override
public void about() {
super.about();
System.out.print("Testing..."); //Working!
Command = "backward 100";
turtle.getCommand(Command, this);
Command = "penDown";
turtle.getCommand(Command, this);
Command = "forward 200";
turtle.getCommand(Command, this);
Command = "turnright 120";
turtle.getCommand(Command, this);
Command = "forward 50";
turtle.getCommand(Command, this);
}
/*@Override
public void circle(int radius) {
super.circle(radius);
}*/
}
这是整个 GUI class,关于部分在后面
public class myGUI extends JPanel{
static JTextField commandbox;
boolean isSaved = false;
TurtleGraphics TG = new TurtleGraphics();
theTurtle turtle = new theTurtle();
TurtleGraphics ETG = new ExtendedTurtleGraphics();
public myGUI()
{
JFrame MFrame = new JFrame();
setLayout(new BorderLayout());
setBackground(Color.white);
commandbox = new JTextField(5);
MenuBr mb = new MenuBr();
MFrame.setJMenuBar(mb);
MFrame.add(commandbox, BorderLayout.SOUTH);
commandbox.addActionListener(new MyActionListener());
MFrame.add(TG);
MFrame.setSize(640, 480);
MFrame.setVisible(true);
MFrame.validate();
MFrame.repaint();
ExtendedTurtleGraphics ETG;
}
public static void infoBox(String infoMessage, String titleBar)
{
JOptionPane.showMessageDialog(null, infoMessage, titleBar, JOptionPane.INFORMATION_MESSAGE);
}
class MenuBr extends JMenuBar implements ActionListener {
JMenu fileMnu = new JMenu("File");
JMenuItem newMnuI = new JMenuItem("New");
JMenuItem openMnuI = new JMenuItem("Load");
JMenuItem saveMnuI = new JMenuItem("Save");
JMenuItem closeMnuI = new JMenuItem("Exit");
JMenu helpMnu = new JMenu("Help");
JMenuItem aboutMnuI = new JMenuItem("About");
MenuBr() {
add(fileMnu);
fileMnu.add(newMnuI);
fileMnu.add(openMnuI);
fileMnu.add(saveMnuI);
fileMnu.add(closeMnuI);
newMnuI.addActionListener(this);
openMnuI.addActionListener(this);
saveMnuI.addActionListener(this);
closeMnuI.addActionListener(this);
add(helpMnu);
helpMnu.add(aboutMnuI);
aboutMnuI.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == newMnuI) {
TG.reset();
TG.turnLeft(90);
TG.clear();
TG.penDown();
}
if (e.getSource() == openMnuI) {
if (isSaved == true) {
String loadName = JOptionPane.showInputDialog("Enter Load File Path (Excluding .png) :");
loadName = loadName + ".png";
try {
BufferedImage loadedImage = ImageIO.read(new File(loadName));
TG.setBufferedImage(loadedImage);
} catch (IOException exception) {
System.out.println(exception.getMessage());
}
} else if (isSaved == false) {
int n = JOptionPane.showConfirmDialog(null, "Save before loading?", "Are you sure?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
System.out.println(n);
if (n == 0) {
Graphics g = TG.getGraphicsConext();
BufferedImage i = TG.getBufferedImage();
String fileName = JOptionPane.showInputDialog("Enter Save File Path (Excluding .png): ");
fileName = fileName + ".png";
File file = new File(fileName);
try {
ImageIO.write(i, "png", file);
} catch (IOException el) {
el.printStackTrace();
}
String loadName = JOptionPane.showInputDialog("Enter Load File Path (Excluding .png) :");
loadName = loadName + ".png";
try {
BufferedImage loadedImage = ImageIO.read(new File(loadName));
TG.setBufferedImage(loadedImage);
} catch (IOException exception) {
System.out.println(exception.getMessage());
}
} else if (n == 1) {
String loadName = JOptionPane.showInputDialog("Enter Load File Path (Excluding .png) :");
loadName = loadName + ".png";
try {
BufferedImage loadedImage = ImageIO.read(new File(loadName));
TG.setBufferedImage(loadedImage);
} catch (IOException exception) {
System.out.println(exception.getMessage());
}
}
}
}
if (e.getSource() == saveMnuI) {
isSaved = true;
Graphics g = TG.getGraphicsConext();
BufferedImage i = TG.getBufferedImage();
String fileName = JOptionPane.showInputDialog("Enter File Path (Excluding .png): ");
fileName = fileName + ".png";
File file = new File (fileName);
try{
ImageIO.write(i, "png", file);
} catch (IOException el) {
el.printStackTrace();
}
}
if (e.getSource() == closeMnuI) {
if (isSaved == true)
{
System.exit(0);
}
else
{
int n = JOptionPane.showConfirmDialog(null, "Save before quitting?", "Are you sure?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
System.out.println(n);
if (n == 0)
{
Graphics g = TG.getGraphicsConext();
BufferedImage i = TG.getBufferedImage();
String fileName = JOptionPane.showInputDialog("Enter File Path (Excluding .png): ");
fileName = fileName + ".png";
File file = new File (fileName);
try{
ImageIO.write(i, "png", file);
} catch (IOException el) {
el.printStackTrace();
}
System.exit(0);
}
else if (n == 1)
{
System.exit(0);
}
}
}
这是关于部分
if (e.getSource() == aboutMnuI) {
ETG.clear();
ETG.about();
ImageIcon icon = new ImageIcon("FilePath");
JOptionPane.showMessageDialog(null, "Turtle Graphics\nJoe Bloggs\n4FS1\nOOP Assessment 2020", "About", JOptionPane.INFORMATION_MESSAGE, icon);
}
}
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == commandbox) {
String command = commandbox.getText();
isSaved = false;
turtle.getCommand(command, TG);
commandbox.setText("");
}
}
}
}
theTurtle Java Class.
public class theTurtle {
//ArrayList<String> oldCommands = new ArrayList<>();
public void getCommand(String command, TurtleGraphics TG)
{
String[]entries = command.split(" ");
int var = -1;
String norm = entries[0];
if (entries.length == 2)
{
var = Integer.parseInt(entries[1]);
}
getMethod(norm, var, TG);
}
private void getMethod(String command, int var, TurtleGraphics TG)
{
if (command.toLowerCase().equals("penup"))
{
TG.penUp();
}
else if (command.toLowerCase().equals("pendown"))
{
TG.penDown();
}
else if (command.toLowerCase().equals("turnleft"))
{
if (var==-1)
{
TG.turnLeft();
}
else{
TG.turnLeft(var);
}
}
else if (command.toLowerCase().equals("turnright"))
{
if (var==-1)
{
TG.turnRight();
}
else{
TG.turnRight(var);
}
}
else if (command.toLowerCase().equals("forward"))
{
if (var == -1 || var <= 0)
{
errorEcho(1);
}
else{
TG.forward(var);
}
}
else if (command.toLowerCase().equals("backward"))
{
if (var == -1 || var <= 0)
{
errorEcho(1);
}
else{
TG.turnRight(180);
TG.forward(var);
TG.turnRight(180);
}
}
else if (command.toLowerCase().equals("black"))
{
TG.setPenColour(Color.black);
}
else if (command.toLowerCase().equals("red"))
{
TG.setPenColour(Color.red);
}
else if (command.toLowerCase().equals("green"))
{
TG.setPenColour(Color.green);
}
else if (command.toLowerCase().equals("reset"))
{
TG.reset();
TG.turnLeft(90);
TG.clear();
TG.penDown();
}
else if (command.toLowerCase().equals("circle"))
{
if (var == -1 || var <= 0)
{
errorEcho(1);
}
else{
TG.circle(var);
}
}
else{
myGUI.infoBox("Invalid Command", "ERROR!");
}
}
private void errorEcho(int errorCode)
{
myGUI.infoBox("Missing Parameters", "ERROR!");
}
}
The variables that are show stepping through about
- 删除
TurtleGraphics TG = new TurtleGraphics();
- 将
TG
替换为this
这应该会让您朝着正确的方向前进。
@编辑
好吧,我们没有 TurtleGraphics 的代码,所以我们不能 100% 判断但是:
首先:在你的 gui 类中定义
TurtleGraphics TG = new TurtleGraphics();
theTurtle turtle = new theTurtle();
TurtleGraphics ETG = new ExtendedTurtleGraphics();
所以你在这里创建的结构是这样的
TurtleGraphics TG
theTurtle turtle
ExtendedTurtleGraphics ETG {
TurtleGraphics TG // You define a new instanct of TurtleGraphics called TG in the ETG object
theTurle turtle // You defined a new instance of theTurtle called turtle in the ETG object
}
请注意,您现在有两个 turtle 对象和两个 TurtleGraphicsObject(或者实际上是三个,因为 ETG 在扩展 TurtleGraphics 后也是一个 TurtleGraphics)
好吧,那么在 ExtendedTurtleGraphics 的 "about" 函数中,您 运行 对 "turtle" 命令,但请注意,这些命令是在以下海龟上执行的:
TurtleGraphics TG
theTurtle turtle
ExtendedTurtleGraphics ETG {
TurtleGraphics TG
theTurle turtle // <- this one
}
所以基本上,当您在 ETG 上 运行 "about" 方法时,您在 "myGUI" class 中定义的 "turtle" 不会改变。 这解释了为什么控制台输出在 about 方法中工作,但您看不到任何事情发生。
解决方法是删除 MyGui 初始化时的两行:
TurtleGraphics TG = new TurtleGraphics(); // remove this one
theTurtle turtle = new theTurtle(); // remove this one
TurtleGraphics ETG = new ExtendedTurtleGraphics();
然后从 ExtendedTurtleGraphics 中删除这一行:
TurtleGraphics TG = new TurtleGraphics(); // Remove this one
在 ExtendedTurtleGraphics 中创建 Turtle public。 在这之后你的结构现在看起来像这样:
我的图形用户界面:
ExtendedTurtleGraphic ETG { // ExtendedTurtleGraphics 对象是一个 TurtleGraphic
乌龟
}
现在,在 myGui class 中,将所有出现的 "TG" 替换为 "ETG",将所有出现的 "turtle" 替换为 "ETG.turtle"
这意味着每次您想对海龟进行某些操作时,您都可以从 ETG 访问海龟实例,然后在调用 ExtendedTurtleGraphics 中的 "about" 方法时修改同一个对象。
@二次编辑:
使用参数 ETG 向 MyActionListener 添加构造函数,如下所示:
class MyActionListener implements ActionListener {
private ExtendedTurtleGraphics ETG;
public MyActionListener(ExtendedTurtleGraphics ETG){
this.ETG = ETG;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == commandbox) {
String command = commandbox.getText();
isSaved = false;
ETG.turtle.getCommand(command, ETG);
commandbox.setText("");
}
}
}
在 MyGui 中更改 Action 侦听器注册,如下所示:
commandbox.addActionListener(new MyActionListener(ETG));