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

    Default Need a help For making a letter bounce

    im trying to make a letter bounce off of the edges of the frame , but im stuck . can anyone help me to fix it ?



    Java Code:
    import javax.swing.*;
    
    import java.awt.*;
    public class MainProgram extends JFrame{
    	
    		
    	
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		MainProgram frm = new MainProgram();
    		frm.setTitle("Moving a letter");
    		frm.setSize(600, 600);
    		frm.setLocationRelativeTo(null);
    		frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frm.add(new MovingObject());
    		frm.setVisible(true);
    	}
    
    }
    class MovingObject extends JComponent {
    	private int X = 300;
        private int Y = 300;
        private char letter = 'O';
        public MovingObject() {
            Thread animationThread = new Thread(new Runnable() {
                public void run() {
                    while (true) {
       
                        repaint();
                        try {Thread.sleep(10);} catch (Exception ex) {}
                    }
                }
            });
    
            animationThread.start();
        }
        public void paintComponent(Graphics g) {
           
            int w = getWidth();
            int h = getHeight();
    
            
             int objectSpeed = 2;
            
            int x = X + objectSpeed  ;
            
            int y = Y;
            
          
         
            super.paintComponent(g);
    		Font baru = new Font ("ARIAL",Font.PLAIN,30);
    		g.setFont(baru);
    		g.drawString(String.valueOf(letter), x, y);
           
         if ( x > h)
            {
            	  x = X - objectSpeed;
            	
            }
         Y = y;
            X = x ;
            
        }
    }
    Any help would be appreciated.thanks.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Need a help For making a letter bounce

    but im stuck . can anyone help me to fix it
    Can you explain what the code does now and what you want to change?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    andystwn is offline Member
    Join Date
    Jan 2015
    Posts
    2
    Rep Power
    0

    Default Re: Need a help For making a letter bounce

    I wanna make the alphabets bounce

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Need a help For making a letter bounce

    Ok, to do that requires that the x and y values of the shapes be changed every unit of time.
    When the shape's location touches a side of the view area, the values of x and y need to change to go in a new direction.

    In the code what values are in the variables: x and X? And y and Y?
    The names of variables should tell you what they contain.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. bounce ball
    By Pramod kulat in forum Java Applets
    Replies: 1
    Last Post: 10-14-2013, 11:16 AM
  2. Ball Bounce project
    By jwl in forum New To Java
    Replies: 4
    Last Post: 09-06-2012, 04:55 PM
  3. Not able to stored the bounce email
    By kumar11 in forum Apache Commons
    Replies: 0
    Last Post: 06-27-2012, 11:14 AM
  4. Help me making letter
    By xp_vista007 in forum Java 2D
    Replies: 3
    Last Post: 07-04-2011, 01:56 PM
  5. Replies: 2
    Last Post: 12-01-2007, 07:48 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
  •