基本上,我试图创建一个让Finch跟随对象的程序。我创建了两个类:NewOption52和FollowClass。 NewOption52类包含一个确定GUI属性的方法。类FollowClass包含一个main方法,它从类NewOption52调用GUI方法,它还包含几个方法,用于指示Finch的行为以及向JTextArea feed附加文本。
当我连接Finch并运行程序时,应该出现一个GUI,JTextArea里面应该有文字说""请将一个物体放在Finch前面然后点击Finch激活!\ n& #34 ;.当我运行该程序时,它没有发生。
Class NewOption52:
import edu.cmu.ri.createlab.terk.robot.finch.Finch;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.*;
public class NewOption52 {
public static NewOption52 Op = new NewOption52();
public static JTextArea feed;
public static JButton halt;
public static JButton exit;
public static JFrame x ;
static protected Finch Grubert = new Finch();
//Method for determining the GUI
static void GUI()
{
final JFrame x = new JFrame("Finch Mission : Follow an Object!") ;
// How the window should be closed
x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Adding a layout manager
x.setLayout(new FlowLayout());
x.setResizable(false);
x.setLocationRelativeTo(null);
// Adding components ( significantly a JTextArea being a feedback box ) and adding ActionListeners to each button
halt = new JButton("Halt!");
x.add(halt);
halt.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
feed.append("You have halted the program\n");
Grubert.stopWheels();
Grubert.setLED(0, 0, 0);
JOptionPane.showMessageDialog(x,"You have halted the program , click OK to go back and please wait for 3 seconds for Finch to start again");
Grubert.sleep(3000);
x.repaint();
}
});
exit = new JButton("Exit");
x.add(exit);
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Grubert.quit();
System.exit(0);
}
});
feed = new JTextArea(30,60);
JScrollPane feedscroll = new JScrollPane(feed);
x.add(feedscroll);
// Arrange components neatly inside the window
x.pack();
// Making the window visible once Window is called
x.setVisible(true);
}
}
Class FollowClass:
import edu.cmu.ri.createlab.terk.robot.finch.Finch;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class FollowClass {
private static Finch Grubert = null;
private static int TapCount = 0;
public static JTextArea feed;
public static JButton halt;
public static JButton exit;
public static JFrame x ;
//public Finch Grubert = new Finch();
public static void main(String[] args)
{
//refers to first method to start program
NewOption52.GUI();
}
public static void Finch()
{
Grubert = new Finch();
ProgramFollow();
}
private static void ProgramFollow() {
// Loop to wait for Finch to be tapped and an obstacle detected in front of the Finch
boolean StartProgram = true;
NewOption52.feed.append("Please Place An Object in front of Finch And Then Tap Finch to Activate!\n");
while (StartProgram)
{
// Conditional statement for same thing as mentioned above
if (Grubert.isTapped()==true && Grubert.isObstacleLeftSide()==true && Grubert.isObstacleRightSide()==true)
{
feed.append("Finch is activated! Object is detected!\n");
Grubert.setLED(red,0,0);
Grubert.setWheelVelocities(leftVelocity, rightVelocity);
// Triggers RunAgain to true so the program doesnt stop in one run in order for Finch to move continuosuly
boolean RunAgain=true;
while(RunAgain)
{
// Calling Movement method for Finch movements
Movement();
// Inside while RunAgain loop , there is the incremental function called TapCount to determine how many times the Finch has been tapped
if (Grubert.isTapped()==true)
{
TapCount++;
feed.append("You have tapped the Finch. If TapCount = 3 , the program will terminate only if the object is not moving\n");
feed.append("Number of Taps:" + TapCount + "\n");
if (TapCount==3 && Grubert.isObstacleLeftSide()==false && Grubert.isObstacleRightSide()==false)
{
Grubert.stopWheels();
Grubert.setLED(0, 0, 0);
TapCount = 1;
JOptionPane.showMessageDialog(x,"You have tapped the Finch twice when the object is moving. Please click OK and wait for 3 seconds. Make sure the object is not moving in order for program to terminate when you have tapped it twice");
Grubert.sleep(3000);
}
if (TapCount==3 && Grubert.isObstacleLeftSide()==true && Grubert.isObstacleRightSide()==true)
{
Grubert.quit();
System.exit(0);
}
}
}
}
}
}
// Method for Finch movements
private static void Movement()
{
if (Grubert.isObstacleLeftSide()==false && Grubert.isObstacleRightSide()==false)
{
// send message to the feedback box ("Object is Detected! Following it now!");
feed.append("Following the Object now!\n");
StraightMovement();
}
else if (Grubert.isObstacleLeftSide()==true && Grubert.isObstacleRightSide()==false)
{
feed.append("Object detected on the left side\n");
LeftMovement();
}
else if (Grubert.isObstacleLeftSide()==false && Grubert.isObstacleRightSide()==true)
{
feed.append("Object detected on the right side\n");
RightMovement();
}
else if (Grubert.isObstacleLeftSide()==true && Grubert.isObstacleRightSide()==true)
{
StopMovement();
}
}
// Area of variables declaration for easy value modifications
static int Buzz = 500;
static int BuzzDuration = 10;
static int red = 255;
static int green = 255;
static int leftVelocity = 100;
static int rightVelocity = 100;
static int leftTurnV = -50;
static int rightTurnV = -50;;
// Area of variables declaration for easy value modifications
private static void StraightMovement()
{
Grubert.setLED(0, green, 0);
Grubert.setWheelVelocities(leftVelocity, rightVelocity);
Grubert.buzz(Buzz, BuzzDuration);
}
private static void LeftMovement()
{
Grubert.setLED(0, green, 0);
Grubert.setWheelVelocities(leftTurnV, rightVelocity);
Grubert.buzz(Buzz, BuzzDuration);
}
private static void RightMovement()
{
Grubert.setLED(0, green, 0);
Grubert.setWheelVelocities(leftVelocity, rightTurnV);
Grubert.buzz(Buzz, BuzzDuration);
}
private static void StopMovement()
{
Grubert.setLED(red, 0 , 0);
Grubert.stopWheels();
}
}