Results 1 to 4 of 4
  1. #1
    iamangel2010 is offline Member
    Join Date
    Feb 2015
    Posts
    2
    Rep Power
    0

    Default How to make rectangle move in a random direction

    Hi I am learning java and I wanted to make a rectangle to move in a random direction and when it collides with a wall to change its direction to another random direction. So I have a program
    Java Code:
    package game5;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    
    import javax.swing.*;
    
    public class game5 extends JPanel implements ActionListener{
    	Timer tm=new Timer(2, this);
    	Random ra=new Random();
    	int angle=0;
    	Rectangle rec;
    	boolean dr=false;
    	int frX=0;
    	int frY=0;
    	int frEndX=0;
    	int frEndY=0;
    	int dirX=0;
    	int dirY=0;
    	int val=1;
    	public game5(){
    		rec=new Rectangle();
    		rec.x=50;
    		rec.y=50;
    		rec.height=30;
    		rec.width=30;
    	}
    	public static void main(String[] args){
    		game5 ga=new game5();
    		ga.drawing();
    	}
    	public void drawing(){
    		JFrame frame=new JFrame();
    		frame.setSize(300, 300);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setResizable(false);
    		frame.add(this);
    		frame.setVisible(true);
    		frX=this.getX();
    		frY=this.getY();
    		frEndX=this.getX()+this.getWidth();
    		frEndY=this.getY()+this.getHeight();
    	}
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		Graphics2D gd=(Graphics2D) g;
    		gd.setColor(Color.orange);
    		gd.fill(rec);
    		tm.setRepeats(true);
    		tm.start();
    		
    		
    	}
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		// TODO Auto-generated method stub
    	//	while(true){
    		angle=ra.nextInt(160)+10;
    		if(angle>=45 && angle<=135){
    			if(angle==90)
    				angle=87;
    			dirY=0;
    			dirX=(int) ((frEndY-(rec.y+rec.height))/Math.abs(Math.tan(angle)));
    		}else if(angle<45 && angle>135){
    			dirX=frEndX-(rec.x+rec.width);
    			dirY=(int) (dirX*Math.abs(Math.tan(angle)));		
    	}
    		while(rec.x!=dirX && rec.y!=dirY){
    			System.out.println("Hello I am here");
    			if(rec.x>dirX)
    				rec.x=rec.x-val;
    			else if(rec.x<dirX)
    				rec.x=rec.x+val;
    			if(rec.y>dirY)
    				rec.y=rec.y-val;
    			else if(rec.y<dirY)
    				rec.y=rec.y+val;
    			repaint();
    		}
    //		}
    	}
    }
    But it moves the ball only once and it does it so quickly that i cannot see the movement at all. I have another code which works perfectly fine but the task was different easier so I don't understand why is that. Can someone please help me. Thanks in advance.
    Java Code:
    package game5;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class test extends JPanel implements ActionListener{
    	Timer tm=new Timer(3, this);
    	int x=40;
    	int y=40;
    	int val=1;
    	public static void main(String[] args){
    		test t=new test();
    		t.drawing();
    	}
    	public void drawing(){
    		JFrame frame=new JFrame();
    		frame.setSize(300, 300);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setResizable(false);
    		frame.add(this);
    		frame.setVisible(true);
    	}
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		Graphics2D gd=(Graphics2D) g;
    		gd.setColor(Color.BLUE);
    		gd.fillRect(x, y, 50, 50);
    		tm.start();
    		
    	}
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		// TODO Auto-generated method stub
    		//System.out.println("Hello");
    		repaint();
    		if(x==this.getX() ||x==(this.getX()+this.getWidth()))
    			val=-val;
    	
    		x=x+val;
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: How to make rectangle move in a random direction

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    iamangel2010 is offline Member
    Join Date
    Feb 2015
    Posts
    2
    Rep Power
    0

    Default Re: How to make rectangle move in a random direction

    yeah but no-one answered me so I am asking here

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: How to make rectangle move in a random direction

    I have no idea why you think it is necessary to start the timer each and every time you paint something. I'd just follow the official articles and start it once, since it already repeats at an interval on its own:

    How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

    Timer (Java Platform SE 7 )
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Can't get the Rectangle to move.
    By Wirezaid in forum New To Java
    Replies: 13
    Last Post: 03-24-2012, 03:29 PM
  2. Replies: 15
    Last Post: 02-24-2012, 10:12 AM
  3. how to move a rectangle with arrow keys,
    By jmu2101 in forum AWT / Swing
    Replies: 3
    Last Post: 09-27-2011, 01:13 AM
  4. To make slider resize rectangle and to move pictures
    By mneskovic in forum New To Java
    Replies: 11
    Last Post: 05-21-2010, 11:07 AM
  5. Can't get graphics to move opposite direction
    By Nimblyjacks in forum Java Applets
    Replies: 8
    Last Post: 12-22-2009, 01:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •