Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Java Roomba Program Help

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Roomba Program Help

    Hey guys so im designing a game that simulates a roomba robot vacuum and have most of the framework done. So I have random floating circles and the Roomba bot in the center. The boot is suppose to move with the arrow keys. However when i run the program and select the keys nothing happens. Any suggestions here's my code.

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.Ellipse2D;
    import java.awt.Graphics;
     
    import javax.swing.*;
     
    public class Roomba extends JFrame implements ActionListener, KeyListener{
     
     
    	private Graphics brush;
    	  public Graphics display;
    	  private Image canvas;    // off-screen graphics buffer
    	  Timer t = new Timer(5, this);
     
     
    	  public Roomba(){
    			t.start();
    			addKeyListener(this);
    			setFocusable(true);
    			setFocusTraversalKeysEnabled(false);
    		}
     
     
     
     
    	  public static void main(String[] args) // needed for application
    	  {
    	    Roomba session  = new Roomba();
    	    session.addWindowListener(new WindowAdapter() {
    	              public void windowClosing(WindowEvent e) {System.exit(0);} });
    	    session.init();
    	  }
     
     
    	  public void init()
    	    {
    	       setBounds(0,0,800,600);
    	       show();
    	       RoombaBot rb = new RoombaBot();
    	       canvas = createImage(800,600);
    	       brush = canvas.getGraphics();
    	       display = this.getGraphics();
    	       brush.setColor(Color.black);   // clear 
    	       brush.fillRect(0,0,800,600);   // with black background
    	       brush.setColor(Color.gray);   // 
    	       animate();
    	    } // init
     
     
    	   private void clearbuffer()
    	    { Color old;
    	      old = brush.getColor();
    	      brush.setColor(Color.blue);   // clear 
    	      brush.fillRect(0,0,800,600);   // with black background
    	      brush.setColor(old);   //  restores color
    	    } // clearbuffer
     
    	   public void nextframe(int delay) // delay in ms
    	    {
    	       try { Thread.sleep(delay); } catch (InterruptedException IE) {}
    	       display.drawImage(canvas,0,0,null);  // draws to screen
    	       clearbuffer();
    	    } // nextframe with ms delay
     
     
    	    /* stuff pertaining to bouncing circles: */
     
    	   private int randint(int min, int max) 
    	    { return (int) (Math.random() * (max-min+1) + min); }
     
     
    	   public void animate()
    	    {  int x1; int y1;   // hold random numbers
    	       int x2; int y2;
    	       circle c1, c2, c3,c4, c5, c6, c7, c8, c9,c10, c11;
    	       c1 = new circle(50,100,30,Color.gray,brush);
    	       c2 = new circle(300,110,20,Color.gray,brush);
    	       c3 = new circle(51,101,30,Color.gray,brush);
    	       c4 = new circle(300,110,20,Color.gray,brush);
    	       c5 = new circle(50,100,30,Color.gray,brush);
    	       c6 = new circle(300,110,20,Color.gray,brush);
    	       c7 = new circle(50,100,30,Color.gray,brush);
    	       c8 = new circle(300,110,20,Color.gray,brush);
    	       c9 = new circle(50,100,30,Color.gray,brush);
    	       c10 = new circle(300,110,20,Color.gray,brush);
    	       c11 = new circle(300,300, 60,Color.black,brush);
    	       c1.setmovement(randint(-9,9),randint(-9,9));
    	       c2.setmovement(randint(-9,9),randint(-9,9));
    	       c3.setmovement(randint(-9,9),randint(-9,9));
    	       c4.setmovement(randint(-9,9),randint(-9,9));
    	       c5.setmovement(randint(-9,9),randint(-9,9));
    	       c6.setmovement(randint(-9,9),randint(-9,9));
    	       c7.setmovement(randint(-9,9),randint(-9,9));
    	       c8.setmovement(randint(-9,9),randint(-9,9));
    	       c9.setmovement(randint(-9,9),randint(-9,9));
    	       c10.setmovement(randint(-9,9),randint(-9,9));
     
    	       // c1.setmovement(4,0);
    	       // c2.setmovement(-2,0);
     
    	       while (1<2)
    		   {  
    	               if (c1.overlap(c2))  // switch movement vectors
    			   {   int olddx, olddy;
    			       olddx = c1.getdx();
    			       olddy = c1.getdy();
    			       c1.setmovement(c2.getdx(),c2.getdy());
    			       c2.setmovement(olddx,olddy);
    			   }
    		       c1.draw();
    		       c2.draw();
    		       c3.draw();
    		       c4.draw();
    		       c5.draw();
    		       c6.draw();
    		       c7.draw();
    		       c8.draw();
    		       c9.draw();
    		       c10.draw();
    		       c11.draw();
    		       c1.move();
    		       c2.move();
    		       c3.move();
    		       c4.move();
    		       c5.move();
    		       c6.move();
    		       c7.move();
    		       c8.move();
    		       c9.move();
    		       c10.move();
    		       nextframe(40);
    	           }
    	    } // animate
     
    	} // class circles
     
     
    	class circle implements ActionListener
    	{
    	  private int x;   // coordinate of center of circle
    	  private int y; 
    	  private int radius; // radius of circle
    	  private int dx;  // movement vector:
    	  private int dy;
    	  double velx, vely;
    	  Color thecolor;     
    	  private Graphics brush;
    	  private final int XBOUND = 800;
    	  private final int YBOUND = 600;
     
     
    	  public circle(int x0, int y0, int r, Color c, Graphics b) // constructor
    	    { x = x0;  y = y0;
    	      radius = r;  thecolor = c;
    	      dx = 0;   dy = 0;
    	      brush = b;
     
    	    }
     
    	  public int getx() { return x; }
    	  public int gety() { return y; }
    	  public int getr() { return radius; }
    	  public int getdx() { return dx; }
    	  public int getdy() { return dy; }
     
    	   // returns distance between two points
    	   private double distance(int x0, int y0, int x1, int y1)  // note private 
    	    { int x = (x0-x1)*(x0-x1);
    	      int y = (y0-y1)*(y0-y1);
    	      return Math.sqrt(x+y);
    	    }
     
    	   public void actionPerformed(ActionEvent e){
    		   //repaint();
    			x += velx;
    			y += vely;
    		}
    	   public void up(){
    			vely = -1.5;
    			velx = 0;
    		}
     
    		public void down(){
    			vely = 1.5;
    			velx = 0;
    		}
    		public void left(){
    			vely = 0;
    			velx = -1.5;
    		}
    		public void right(){
    			vely = 0;
    			velx = 1.5;
    		}
     
    		public void keyPressed(KeyEvent e){
    			int code = e.getKeyCode();
    			if(code == KeyEvent.VK_UP){
    				up();
    			}
    			if(code == KeyEvent.VK_DOWN){
    				down();
    			}
    			if(code == KeyEvent.VK_LEFT){
    				left();
    			}
    			if(code == KeyEvent.VK_RIGHT){
    				right();
    			}
    		}
     
    		public void keyTyped(KeyEvent e){}
    		public void keyReleased(KeyEvent e){}
     
    	  public void setmovement(int newdx, int newdy)
    	    {
    	      dx = newdx;  dy = newdy;
    	    }
     
    	  public void move(Dimension bounds) // move, bouncing off edges
    	  { if ( (x >= (XBOUND-radius)) || (x < radius) )
    	       dx = -1 * dx;  // reverse x direction
    	    if ( (y >= YBOUND-radius) || (y < radius+30) )
    	       dy = -1 * dy;  // reverse y direction
    	    x = x+dx;  y = y+dy;
    	  }
     
    	  public boolean overlap(circle other)
    	    {
    		double dist;
    		dist = distance(x,y,other.getx(),other.gety());
    		return dist <= radius + other.getr();
    	    }
     
    	  public void draw()
    	    {
    		brush.setColor(thecolor);
    		brush.fillOval(x-radius,y-radius,2*radius,2*radius);
     
    	    }
     
    	}  // class circle


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Roomba Program Help

    The posted code does not compile without errors.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Roomba Program Help

    Aren't you getting compiler errors? Or is there code you're not showing us?

  4. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Roomba Program Help

    Im not getting any errors but the goal was to make the black circle "c11" move around with the arrow keys.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Roomba Program Help

    Copy the code you posted to a empty folder and try compiling it.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Sep 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Roomba Program Help

    heres the code again
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Graphics;
     
    import javax.swing.*;
     
    public class Roomba extends JFrame implements ActionListener, KeyListener{
     
     
    	private Graphics brush;
    	  public Graphics display;
    	  private Image canvas;    // off-screen graphics buffer
    	  Timer t = new Timer(5, this);
     
     
    	  public Roomba(){
    			t.start();
    			addKeyListener(this);
    			setFocusable(true);
    			setFocusTraversalKeysEnabled(false);
    		}
     
     
     
     
    	  public static void main(String[] args) // needed for application
    	  {
    	    Roomba session  = new Roomba();
    	    session.addWindowListener(new WindowAdapter() {
    	              public void windowClosing(WindowEvent e) {System.exit(0);} });
    	    session.init();
    	  }
     
     
    	  public void init()
    	    {
    	       setBounds(0,0,800,600);
    	       show();
    	       //RoombaBot rb = new RoombaBot();
    	       canvas = createImage(800,600);
    	       brush = canvas.getGraphics();
    	       display = this.getGraphics();
    	       brush.setColor(Color.black);   // clear 
    	       brush.fillRect(0,0,800,600);   // with black background
    	       brush.setColor(Color.gray);   // 
    	       animate();
     
     
    	    } // init
     
     
    	   private void clearbuffer()
    	    { Color old;
    	      old = brush.getColor();
    	      brush.setColor(Color.blue);   // clear 
    	      brush.fillRect(0,0,800,600);   // with black background
    	      brush.setColor(old);   //  restores color
    	    } // clearbuffer
     
    	   public void nextframe(int delay) // delay in ms
    	    {
    	       try { Thread.sleep(delay); } catch (InterruptedException IE) {}
    	       display.drawImage(canvas,0,0,null);  // draws to screen
    	       clearbuffer();
    	    } // nextframe with ms delay
     
     
    	    /* stuff pertaining to bouncing circles: */
     
    	   private int randint(int min, int max) 
    	    { return (int) (Math.random() * (max-min+1) + min); }
     
     
    	   public void animate()
    	    {  int x1; int y1;   // hold random numbers
    	       int x2; int y2;
    	       circle c1, c2, c3,c4, c5, c6, c7, c8, c9,c10, c11;
    	       c1 = new circle(50,100,30,Color.gray,brush);
    	       c2 = new circle(300,110,20,Color.gray,brush);
    	       c3 = new circle(51,101,30,Color.gray,brush);
    	       c4 = new circle(300,110,20,Color.gray,brush);
    	       c5 = new circle(50,100,30,Color.gray,brush);
    	       c6 = new circle(300,110,20,Color.gray,brush);
    	       c7 = new circle(50,100,30,Color.gray,brush);
    	       c8 = new circle(300,110,20,Color.gray,brush);
    	       c9 = new circle(50,100,30,Color.gray,brush);
    	       c10 = new circle(300,110,20,Color.gray,brush);
    	       c11 = new circle(300,300, 60,Color.black,brush);//roomba
    	       c1.setmovement(randint(-9,9),randint(-9,9));
    	       c2.setmovement(randint(-9,9),randint(-9,9));
    	       c3.setmovement(randint(-9,9),randint(-9,9));
    	       c4.setmovement(randint(-9,9),randint(-9,9));
    	       c5.setmovement(randint(-9,9),randint(-9,9));
    	       c6.setmovement(randint(-9,9),randint(-9,9));
    	       c7.setmovement(randint(-9,9),randint(-9,9));
    	       c8.setmovement(randint(-9,9),randint(-9,9));
    	       c9.setmovement(randint(-9,9),randint(-9,9));
    	       c10.setmovement(randint(-9,9),randint(-9,9));
     
    	       // c1.setmovement(4,0);
    	       // c2.setmovement(-2,0);
     
    	       while (1<2)
    		   {  
    	               if (c1.overlap(c2))  // switch movement vectors
    			   {   int olddx, olddy;
    			       olddx = c1.getdx();
    			       olddy = c1.getdy();
    			       c1.setmovement(c2.getdx(),c2.getdy());
    			       c2.setmovement(olddx,olddy);
    			   }
    		       c1.draw();
    		       c2.draw();
    		       c3.draw();
    		       c4.draw();
    		       c5.draw();
    		       c6.draw();
    		       c7.draw();
    		       c8.draw();
    		       c9.draw();
    		       c10.draw();
    		       c11.draw();
    		       c1.move();
    		       c2.move();
    		       c3.move();
    		       c4.move();
    		       c5.move();
    		       c6.move();
    		       c7.move();
    		       c8.move();
    		       c9.move();
    		       c10.move();
    		       nextframe(40);
    	           }
    	    } // animate
     
     
     
     
    	@Override
    	public void keyPressed(KeyEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
     
     
     
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
     
     
     
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
     
     
     
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
     
     
     
     
     
    	} // class circles
     
     
    	class circle implements ActionListener, KeyListener
    	{
    	  private int x;   // coordinate of center of circle
    	  private int y; 
    	  private int radius; // radius of circle
    	  private int dx;  // movement vector:
    	  private int dy;
    	  double velx, vely;
    	  Color thecolor;     
    	  private Graphics brush;
    	  private final int XBOUND = 800;
    	  private final int YBOUND = 600;
     
     
     
     
    	  public circle(int x0, int y0, int r, Color c, Graphics b) // constructor
    	    { x = x0;  y = y0;
    	      radius = r;  thecolor = c;
    	      dx = 0;   dy = 0;
    	      brush = b;
     
     
    	    }
     
    	  public int getx() { return x; }
    	  public int gety() { return y; }
    	  public int getr() { return radius; }
    	  public int getdx() { return dx; }
    	  public int getdy() { return dy; }
     
    	   // returns distance between two points
    	   private double distance(int x0, int y0, int x1, int y1)  // note private 
    	    { int x = (x0-x1)*(x0-x1);
    	      int y = (y0-y1)*(y0-y1);
    	      return Math.sqrt(x+y);
    	    }
     
    	   public void actionPerformed(ActionEvent e){
    		   //repaint();
    			x += velx;
    			y += vely;
    		}
    	   public void up(){
    			vely = -1.5;
    			velx = 0;
    		}
     
    		public void down(){
    			vely = 1.5;
    			velx = 0;
    		}
    		public void left(){
    			vely = 0;
    			velx = -1.5;
    		}
    		public void right(){
    			vely = 0;
    			velx = 1.5;
    		}
     
    		public void keyPressed(KeyEvent e){
    			int code = e.getKeyCode();
    			if(code == KeyEvent.VK_UP){
    				up();
    			}
    			if(code == KeyEvent.VK_DOWN){
    				down();
    			}
    			if(code == KeyEvent.VK_LEFT){
    				left();
    			}
    			if(code == KeyEvent.VK_RIGHT){
    				right();
    			}
    		}
     
    		public void keyTyped(KeyEvent e){}
    		public void keyReleased(KeyEvent e){}
     
    	  public void setmovement(int newdx, int newdy)
    	    {
    	      dx = newdx;  dy = newdy;
    	    }
     
    	  public void move() // move, bouncing off edges
    	  { if ( (x >= (XBOUND-radius)) || (x < radius) )
    	       dx = -1 * dx;  // reverse x direction
    	    if ( (y >= YBOUND-radius) || (y < radius+30) )
    	       dy = -1 * dy;  // reverse y direction
    	    x = x+dx;  y = y+dy;
    	  }
     
    	  public boolean overlap(circle other)
    	    {
    		double dist;
    		dist = distance(x,y,other.getx(),other.gety());
    		return dist <= radius + other.getr();
    	    }
     
    	  public void draw()
    	    {
    		brush.setColor(thecolor);
    		brush.fillOval(x-radius,y-radius,2*radius,2*radius);
     
    	    }
     
    	}  // class circle

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Roomba Program Help

    That's a new version with several changes. It now compiles and executes.

    Are the key listener methods being called? Add some println() statements to the listener methods to see if they are called.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Roomba Program Help

    The key listener that does something should be on the JFrame, not on the Circle. (In Java, class names begin with capital letters.) As Norm suggested, verify the key listener on the JFrame is working by adding prints to your existing empty code, and then add code to those methods that moves your circle object.

    This approach is not up to date with how drawing and animation is currently done in Java. Are you following an old tutorial? Also, the init() method makes it look like it was adapted from an Applet or JApplet. Did you do the adapting or did someone else?

    Edit: Oh, and using variable names like c1 - c10 is beginner nonsense. Give your variables better names, and when you have a number of them, put them into a collection.

Similar Threads

  1. Invoke a Java program with windows program
    By jackhard in forum Object Oriented Programming
    Replies: 1
    Last Post: February 21st, 2013, 07:16 AM
  2. convert java program to java ME program
    By abhishek1212 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 4th, 2013, 03:22 PM
  3. [SOLVED] Write a java program to display even numbers between 2 and 200 using netbeans java
    By Shalom in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 11th, 2012, 05:16 AM
  4. Replies: 1
    Last Post: July 8th, 2012, 10:23 AM
  5. how to run a java program..when the program using jar
    By miriyalasrihari in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2012, 10:04 AM