Jump to content

Do... While Loops


Drewyboyo

Recommended Posts

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 by Drewyboyo
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 

Link to comment
Share on other sites

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:

 bcf5978d9ae34c74ae1627062d317a20.png

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by Vilius
Link to comment
Share on other sites

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 by Explv
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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:boge:

Edited by Drewyboyo
Link to comment
Share on other sites

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:boge:

If you dont mind pming me your skype to try and see whats wrong, that would be great.

Link to comment
Share on other sites

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:boge:

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

Link to comment
Share on other sites

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 (-:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...