Drewyboyo Posted February 6, 2017 Share Posted February 6, 2017 (edited) We're using "Finch Robots" in computer programming, and I'm trying to move it by key inputs. How do I let it take another key input after I input one? Using our lil' online book it looks something like a Do... While loop but I'm not sure, heres what I have right now. http://www.finchrobot.com/javadoc/edu/cmu/ri/createlab/terk/robot/finch/Finch.html - API of the robot package simpleOutput; import edu.cmu.ri.createlab.terk.robot.finch.Finch; import javax.swing.*; import java.awt.*; import java.util.*; import java.applet.Applet; /** * Created by: Drew * Date: 2/2 * A starter file to use the Finch */ public class Indiana { public static void main(final String[] args) { Finch myFinch = new Finch(); System.out.println("Left=A"); System.out.println("Forward=W"); System.out.println("Right=D"); System.out.println("Backwards=S"); System.out.println("Stop=E"); char Continue char Direction = Expo.enterChar(); switch (Direction) { case 'W': case 'w': myFinch.setWheelVelocities(255,255); break; case 'S': case 's': myFinch.setWheelVelocities(-255,-255); break; case 'D': case 'd': myFinch.setWheelVelocities(255,0); break; case 'A': case 'a': myFinch.setWheelVelocities(0,255); break; case 'E': case 'e': myFinch.setWheelVelocities(0,0); default : System.out.println("Please enter a valid key."); } myFinch.quit(); System.exit(0); } } } Edited February 6, 2017 by Drewyboyo Quote Link to comment Share on other sites More sharing options...
The Hero of Time Posted February 6, 2017 Share Posted February 6, 2017 (edited) little hint: Instead of having multiple cases for one letter, because it can be lower and upper, do this switch(Character.toLowerCase(Direction)) { case 'w': //stuff break; } Edited February 6, 2017 by The Hero of Time 1 Quote Link to comment Share on other sites More sharing options...
Vilius Posted February 6, 2017 Share Posted February 6, 2017 Ohhh this is an interesting thing you have here. What I have to ask is can you use swing for your project? If yes then great, I can throw something up real quick which will be far better than while loop. Quote Link to comment Share on other sites More sharing options...
Drewyboyo Posted February 6, 2017 Author Share Posted February 6, 2017 3 minutes ago, Vilius said: Ohhh this is an interesting thing you have here. What I have to ask is can you use swing for your project? If yes then great, I can throw something up real quick which will be far better than while loop. anything works as long as i can make my lil robot go through its maze, any learning or guidance is always helpful. Quote Link to comment Share on other sites More sharing options...
Explv Posted February 6, 2017 Share Posted February 6, 2017 44 minutes ago, Drewyboyo said: We're using "Finch Robots" in computer programming, and I'm trying to move it by key inputs. How do I let it take another key input after I input one? Using our lil' online book it looks something like a Do... While loop but I'm not sure, heres what I have right now. http://www.finchrobot.com/javadoc/edu/cmu/ri/createlab/terk/robot/finch/Finch.html - API of the robot Hide contents package simpleOutput; import edu.cmu.ri.createlab.terk.robot.finch.Finch; import javax.swing.*; import java.awt.*; import java.util.*; import java.applet.Applet; /** * Created by: Drew * Date: 2/2 * A starter file to use the Finch */ public class Indiana { public static void main(final String[] args) { Finch myFinch = new Finch(); System.out.println("Left=A"); System.out.println("Forward=W"); System.out.println("Right=D"); System.out.println("Backwards=S"); System.out.println("Stop=E"); char Continue char Direction = Expo.enterChar(); switch (Direction) { case 'W': case 'w': myFinch.setWheelVelocities(255,255); break; case 'S': case 's': myFinch.setWheelVelocities(-255,-255); break; case 'D': case 'd': myFinch.setWheelVelocities(255,0); break; case 'A': case 'a': myFinch.setWheelVelocities(0,255); break; case 'E': case 'e': myFinch.setWheelVelocities(0,0); default : System.out.println("Please enter a valid key."); } myFinch.quit(); System.exit(0); } } } You need a do while loop along the lines of "do, get the user's input and change the direction of the robot if the input is valid, while the user's input is not the exit program character" The exit character might be something like ESC Quote Link to comment Share on other sites More sharing options...
Drewyboyo Posted February 6, 2017 Author Share Posted February 6, 2017 5 minutes ago, aloevera said: Be prepared for people who are going to buy from you just to say that you recovered the account back so that they can claim the insurance won my case today: Screenshot as much as possible and save all his or her details it would be really helpful Sorry posted on your wrong thread :P yes waste lots of money on insurance for an 8$ account, as far as I remember their insurance is quite a chunk for a year or 2 year insurance. also have fun with their negative feedback and no one selling to them. Quote Link to comment Share on other sites More sharing options...
aloevera Posted February 6, 2017 Share Posted February 6, 2017 1 minute ago, Drewyboyo said: yes waste lots of money on insurance for an 8$ account, as far as I remember their insurance is quite a chunk for a year or 2 year insurance. also have fun with their negative feedback and no one selling to them. Sorry for your loss bro, if you are looking to buy in bulk for reselling than shoot me a message on discord Quote Link to comment Share on other sites More sharing options...
Vilius Posted February 6, 2017 Share Posted February 6, 2017 (edited) This will be a couple of classes, because me and my friend got carriad away and used the MVC pattern. But should be straight forward if you wanted to add anything else. Also this is easy to expand if you ever want to do so. Also if you want to expand this or just talk about this, pm me your skype and I will contact you. Main class: import java.awt.EventQueue; import javax.swing.JFrame; public class Main { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new JFrame(); frame.add(new AppPane(new AppModel())); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationByPlatform(true); frame.setVisible(true); }); } } AppModel import edu.cmu.ri.createlab.terk.robot.finch.Finch; public class AppModel { private Finch finch = new Finch(); public void buzz(int frequency, int duration) { finch.buzz(frequency, duration); } public void setWheelVelocities(int leftVelocity, int rightVelocity) { finch.setWheelVelocities(leftVelocity, rightVelocity); } } AppPane import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class AppPane extends JPanel implements KeyListener { private AppModel model; public AppPane(AppModel model) { this.model = model; JTextField frequency = new JTextField(3); JTextField duration = new JTextField(3); JButton buzz = new JButton("Buzz"); buzz.addActionListener( event -> model.buzz(frequency.getText().isEmpty() ? 1 : Integer.parseInt(frequency.getText()), duration.getText().isEmpty() ? 1 : Integer.parseInt(duration.getText()))); add(frequency); add(duration); add(buzz); JLabel click = new JLabel("Click me to drive flinch"); click.addKeyListener(this); add(click); } @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_W: model.setWheelVelocities(255, 255); break; case KeyEvent.VK_S: model.setWheelVelocities(-255, -255); break; // TODO: add other keys. } } @Override public void keyReleased(KeyEvent e) { } } Edited February 6, 2017 by Vilius Quote Link to comment Share on other sites More sharing options...
Explv Posted February 6, 2017 Share Posted February 6, 2017 (edited) 24 minutes ago, Vilius said: This will be a couple of classes, because me and my friend got carriad away and used the MVC pattern. But should be straight forward if you wanted to add anything else. Also this is easy to expand if you ever want to do so. Also if you want to expand this or just talk about this, pm me your skype and I will contact you. Hide contents Main class: Hide contents import java.awt.EventQueue; import javax.swing.JFrame; public class Main { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new JFrame(); frame.add(new AppPane(new AppModel())); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationByPlatform(true); frame.setVisible(true); }); } } AppModel Hide contents import edu.cmu.ri.createlab.terk.robot.finch.Finch; public class AppModel { private Finch finch = new Finch(); public void buzz(int frequency, int duration) { finch.buzz(frequency, duration); } public void setWheelVelocities(int leftVelocity, int rightVelocity) { finch.setWheelVelocities(leftVelocity, rightVelocity); } } AppPane Hide contents import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class AppPane extends JPanel implements KeyListener { private AppModel model; public AppPane(AppModel model) { this.model = model; JTextField frequency = new JTextField(3); JTextField duration = new JTextField(3); JButton buzz = new JButton("Buzz"); buzz.addActionListener( event -> model.buzz(frequency.getText().isEmpty() ? 1 : Integer.parseInt(frequency.getText()), duration.getText().isEmpty() ? 1 : Integer.parseInt(duration.getText()))); add(frequency); add(duration); add(buzz); JLabel click = new JLabel("Click me to drive flinch"); click.addKeyListener(this); add(click); } @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_W: model.setWheelVelocities(255, 255); break; case KeyEvent.VK_S: model.setWheelVelocities(-255, -255); break; // TODO: add other keys. } } @Override public void keyReleased(KeyEvent e) { } } OP is asking how to use a do while loop to keep reading input from the user, not how to use the MVC pattern with Swing? Seems silly to overcomplicate it with a GUI when OP doesn't even understand loops yet Edited February 6, 2017 by Explv Quote Link to comment Share on other sites More sharing options...
Vilius Posted February 6, 2017 Share Posted February 6, 2017 3 minutes ago, Explv said: OP is asking how to use a do while loop to keep reading input from the user, not how to use the MVC pattern with Swing? O well, I misread that, also for further development I doubt that a do while loop is good at all hence the use of the MVC pattern, thus making the application do all sorts of stuff without creating new projects for one thing. Quote Link to comment Share on other sites More sharing options...
Drewyboyo Posted February 8, 2017 Author Share Posted February 8, 2017 (edited) On 2/6/2017 at 1:07 PM, Vilius said: This will be a couple of classes, because me and my friend got carriad away and used the MVC pattern. But should be straight forward if you wanted to add anything else. Also this is easy to expand if you ever want to do so. Also if you want to expand this or just talk about this, pm me your skype and I will contact you. Hide contents Main class: Hide contents import java.awt.EventQueue; import javax.swing.JFrame; public class Main { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new JFrame(); frame.add(new AppPane(new AppModel())); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationByPlatform(true); frame.setVisible(true); }); } } AppModel Hide contents import edu.cmu.ri.createlab.terk.robot.finch.Finch; public class AppModel { private Finch finch = new Finch(); public void buzz(int frequency, int duration) { finch.buzz(frequency, duration); } public void setWheelVelocities(int leftVelocity, int rightVelocity) { finch.setWheelVelocities(leftVelocity, rightVelocity); } } AppPane Hide contents import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class AppPane extends JPanel implements KeyListener { private AppModel model; public AppPane(AppModel model) { this.model = model; JTextField frequency = new JTextField(3); JTextField duration = new JTextField(3); JButton buzz = new JButton("Buzz"); buzz.addActionListener( event -> model.buzz(frequency.getText().isEmpty() ? 1 : Integer.parseInt(frequency.getText()), duration.getText().isEmpty() ? 1 : Integer.parseInt(duration.getText()))); add(frequency); add(duration); add(buzz); JLabel click = new JLabel("Click me to drive flinch"); click.addKeyListener(this); add(click); } @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_W: model.setWheelVelocities(255, 255); break; case KeyEvent.VK_S: model.setWheelVelocities(-255, -255); break; // TODO: add other keys. } } @Override public void keyReleased(KeyEvent e) { } } Well I copy pasta'd your code because im a script kiddy and I just sit on "Connecting to Finch..." for awhile, you probably don't have a robot yourself to troubleshoot this obviously but i have no clue what im doing ¯\_(ツ)_/¯ edit: Nvm it opened the AppPane but the buttons and inputs aren't functional Edited February 8, 2017 by Drewyboyo Quote Link to comment Share on other sites More sharing options...
Vilius Posted February 8, 2017 Share Posted February 8, 2017 6 minutes ago, Drewyboyo said: Well I copy pasta'd your code because im a script kiddy and I just sit on "Connecting to Finch..." for awhile, you probably don't have a robot yourself to troubleshoot this obviously but i have no clue what im doing ¯\_(ツ)_/¯ edit: Nvm it opened the AppPane but the buttons and inputs aren't functional If you dont mind pming me your skype to try and see whats wrong, that would be great. Quote Link to comment Share on other sites More sharing options...
Explv Posted February 8, 2017 Share Posted February 8, 2017 1 hour ago, Drewyboyo said: Well I copy pasta'd your code because im a script kiddy and I just sit on "Connecting to Finch..." for awhile, you probably don't have a robot yourself to troubleshoot this obviously but i have no clue what im doing ¯\_(ツ)_/¯ edit: Nvm it opened the AppPane but the buttons and inputs aren't functional If you don't understand the code, you should either learn how it works or not use it. I would recommend you start off learning the basics of Java and building up from there. I would recommend you continue with your original code, and add a while loop to get your desired functionality rather than adding a GUI at this point Quote Link to comment Share on other sites More sharing options...
Drewyboyo Posted February 8, 2017 Author Share Posted February 8, 2017 39 minutes ago, Explv said: If you don't understand the code, you should either learn how it works or not use it. I would recommend you start off learning the basics of Java and building up from there. I would recommend you continue with your original code, and add a while loop to get your desired functionality rather than adding a GUI at this point i skyped him and commented out things i didnt understand (which was just the buzz function) and looked up and asked him the rest to get it to work but i understand your worry (-: Quote Link to comment Share on other sites More sharing options...