Results 1 to 4 of 4
- 01-09-2015, 08:40 PM #1
Member
- Join Date
- Jan 2015
- Posts
- 2
- Rep Power
- 0
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 ; } }
- 01-09-2015, 09:26 PM #2
Re: Need a help For making a letter bounce
but im stuck . can anyone help me to fix itIf you don't understand my response, don't ignore it, ask a question.
- 01-10-2015, 09:06 AM #3
Member
- Join Date
- Jan 2015
- Posts
- 2
- Rep Power
- 0
Re: Need a help For making a letter bounce
I wanna make the alphabets bounce
- 01-10-2015, 02:44 PM #4
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
-
bounce ball
By Pramod kulat in forum Java AppletsReplies: 1Last Post: 10-14-2013, 12:16 PM -
Ball Bounce project
By jwl in forum New To JavaReplies: 4Last Post: 09-06-2012, 05:55 PM -
Not able to stored the bounce email
By kumar11 in forum Apache CommonsReplies: 0Last Post: 06-27-2012, 12:14 PM -
Help me making letter
By xp_vista007 in forum Java 2DReplies: 3Last Post: 07-04-2011, 02:56 PM -
Need help making ball move and bounce off of sides of JPanel
By adlb1300 in forum New To JavaReplies: 2Last Post: 12-01-2007, 08:48 AM
Bookmarks