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 9 of 9

Thread: Random Color Ball and Move Bottom Bar Vector Java Help

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Random Color Ball and Move Bottom Bar Vector Java Help

    Hi,
    I created this simple vector java program where ball falling and when collide with the bar below, the ball stops.
    I need to make the ball falling randomly from different direction and also need to make the bar move left right using keyboard arrow.
    Can anyone help me with this:
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
     
    //using timer to increase the counter
    public class Ball extends JFrame implements ActionListener{
    	int xSpeed=0;//moving speed
    	int x,y;//ball location
     
        Rectangle blockRect;
        Rectangle ballRect;
     
    	Timer time;
     
     
    	public Ball(String title){
    		super(title);
     
    		xSpeed=5;//moving 5 pixels for every second
    		x=400;
    		y=100;//center of the screen
     
    		//Instantiate the timer
    		time=new Timer(100,this);//o.5 second
    		time.start();//start the timer
     
    		blockRect = new Rectangle(350,400,150,70);
     
     
    		setSize(800,600);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
     
     
    	//timer method
    	public void actionPerformed(ActionEvent e){
    		y=y+xSpeed;//make the move horizontally
    		repaint();
     
    		if(y> this.getWidth()){
    			y=25;
    		}
    	}
     
    	public void paint(Graphics g){
    		Graphics2D g2d=(Graphics2D) g;
     
    		//erase the all record
    		Rectangle rect=new Rectangle(0,0,800,600);
    		g2d.setColor(Color.WHITE);
    		g2d.fill(rect);
     
    		g2d.setColor(Color.CYAN);
     
    		//draw the circle here
    		g2d.fillOval(x,y,25,25);
     
    		ballRect = new Rectangle(x,y,30,30);
     
    		if (blockRect.intersects(ballRect))
    			xSpeed = 0;//stop movement
     
     
    		g2d.setColor(Color.BLACK);
    		g2d.fillRect(350,400,150,70);
     
     
    	}
     
     
    	public static void main(String args[]){
    		new Ball("Animated Ball");
    	}
    }


  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: Random Color Ball and Move Bottom Bar Vector Java Help

    Where are you having problems? What specific questions do you have about the program?

    Pick one of the changes you want to make and work on that before moving on to the next one.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Random Color Ball and Move Bottom Bar Vector Java Help

    Thank you for reply. How can I edit this ball so that the ball falls in random color and from different direction. my current code is:
    public Ball(String title){
    super(title);

    xSpeed=5;//moving 5 pixels for every second
    x=400;
    y=100;//center of the screen

    //Instantiate the timer
    time=new Timer(100,this);//o.5 second
    time.start();//start the timer

  4. #4
    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: Random Color Ball and Move Bottom Bar Vector Java Help

    Let's work on one item at a time:
    in random color
    Use the Random class to generate the 3 int values for the Color class's constructor.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Random Color Ball and Move Bottom Bar Vector Java Help

    thank you Norm but I am new in Java. Anyway I will try to make changes and come back. It will take some days for sure.

  6. #6
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Falling Ball and Catching with Bar Game Problem Help

    Hi,
    I have created a small game in java.
    Screen Shot 2014-02-21 at 7.41.09 PM.jpg
    I have a ball falling but I want the ball to fall randomly with randomly color. In my case its falling only once.
    I want the ball to collide with the rectangle bar and stop. I will catch random balls falling using the rectangle bar. Problem is ball passed the rectangle and not colliding.
    Here is the code. Can anyone help me finish this project:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.util.Random.*;
     
    //using timer to increase the counter
    public class Ball extends JFrame implements ActionListener, KeyListener{
    	int xSpeed=0;//moving speed
    	int x,y;//ball location
    	int x1,y1; //rectangle (basket)
    	int score;
     
        Rectangle blockRect;
        Rectangle ballRect;
        //Random rand = new Random;
    	Timer time;
     
     
    	public Ball(String title){
    		super(title);
     
    		xSpeed=5;//moving 5 pixels for every second
    		x=400;
    		y=100;//center of the screen
     
    		x1= 325;
    		y1= 500;
    		//Instantiate the timer
    		time=new Timer(100,this);//o.5 second
    		time.start();//start the timer
     
    		blockRect = new Rectangle(350,400,150,70);
     
    		addKeyListener(this);
     
    		setSize(800,600);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
     
     
    	//timer method
    	public void actionPerformed(ActionEvent e){
    		y=y+xSpeed;//make the move horizontally
    		repaint();
     
    		if(y> this.getWidth()){
    			y=25;
    		//if (blockRect.intersects(ballRect))
    			//x =  ;//stop movement
    			score +=1;
     
    		}
    	}
     
    	public void paint(Graphics g){
    		Graphics2D g2d=(Graphics2D) g;
     
    		//erase the all record
    		Rectangle rect=new Rectangle(0,0,800,600);
    		g2d.setColor(Color.BLACK);
    		g2d.fill(rect);
     
    		g2d.setColor(Color.CYAN);
     
    		//draw the circle here
    		g2d.fillOval(x,y,25,25);
     
    		ballRect = new Rectangle(x1,y1,30,30);
     
     
     
    		g2d.setColor(Color.RED);
    		g2d.fillRect(x1,y1,150,40);
    		g2d.drawString("Score:" +score,500,50);
     
     
    	}
     
     
        public void left() {
     
            x1-=5;
        }
     
        public void right() {
     
            x1 +=5;;   
        }
     
     
     
        /** Handle the key-released event from the text field. */
        public void keyReleased(KeyEvent e) 
        	{}
        public void keyTyped(KeyEvent e) 
        	{}
     
     
        public void keyPressed(KeyEvent e) 
        	{
            if (e.getKeyCode()==KeyEvent.VK_LEFT){
            	left();
            	System.out.println("Left:"+x1);
     
            }
     
            if (e.getKeyCode()==KeyEvent.VK_RIGHT){
            	  right();
            	  System.out.println("Left:"+x1);
            }
     
        }
     
     
    	public static void main(String args[]){
    		new Ball("Animated Ball");
    	}
    }

  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: Random Color Ball and Move Bottom Bar Vector Java Help

    Duplicate Threads merged.
    You have listed several features you want to add. I suggested that you work on them one at a time.
    Pick one and ask some questions about the problems you are having doing it.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Random Color Ball and Move Bottom Bar Vector Java Help

    OK. How can I make the ball fall randomly with multiple color from different direction?

  9. #9
    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: Random Color Ball and Move Bottom Bar Vector Java Help

    fall randomly with multiple color from different direction
    That looks like three features.

    Before trying to change this code, you need to think about a better design for the program.
    There needs to be several different classes:
    One for each balls that will contain its colors and current location etc
    One to provide a window/panel where the balls will be drawn
    One for the logic to add new balls and control the game

    See the tutorial for how to write the display area for the balls: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Put a random number from one vector to another
    By JohnJohnson123 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: October 6th, 2013, 06:36 PM
  2. Java Program - Graphics - Ball Drop and Retrieve
    By kbrady481 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 1st, 2013, 04:47 PM
  3. To check presence of padlock icon in the location bar /address bar
    By Rexy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 25th, 2013, 06:44 AM
  4. Replies: 2
    Last Post: April 6th, 2012, 01:58 AM
  5. Bouncing Ball Program Random Color Change
    By coderEvolution in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 3rd, 2011, 04:01 PM

Tags for this Thread