因此,首先,我是一名非常业余的Java程序员,它只是一个模块,然后就结束了。
标准是在TurtleGraphics程序中覆盖预定义的“ about()”命令。
如果将替代内容与其他所有内容一起放在GUI类中,则可以使其正常工作,但是当我尝试创建“ ExtendedTurtleGraphics”类并尝试对其进行调用时,它什么也不做,不会引发错误,什么也没做。
这是扩展类和内容
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类,关于部分将继续
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类。
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 calss中定义
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
}
请注意,您现在有两个乌龟对象和两个TurtleGraphicsObjects(或者实际上是三个,因为一旦扩展TurtleGraphics,ETG也是一个TurtleGraphics)
好吧,然后在ExtendedTurtleGraphics的“关于”功能中,在“ turtle”上运行命令,但是请注意,这些命令是在以下turtle上执行的:
TurtleGraphics TG
theTurtle turtle
ExtendedTurtleGraphics ETG {
TurtleGraphics TG
theTurle turtle // <- this one
}
因此,基本上,当您在ETG上运行“ about”方法时,在“ myGUI”类中定义的“ 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公开。
之后,您的结构现在看起来像这样:
MyGui:
ExtendedTurtleGraphic ETG {// ExtendedTurtleGraphics对象是TurtleGraphic
龟龟
}
现在,在myGui类中,将所有出现的“ 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));