Not getting the right movement of the circle

Thread Solved

Join Date: Apr 2008
Posts: 7
Reputation: painless is an unknown quantity at this point 
Solved Threads: 0
painless painless is offline Offline
Newbie Poster

Not getting the right movement of the circle

 
0
  #1
Jul 3rd, 2009
i m not getting the desired out put .....according to the program ,it will have to give the movement of the circle in the direction of the diagonal.


  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class DrawCircle{
  4. int x=70;
  5. int y=70;
  6.  
  7.  
  8. public static void main(String[] args){
  9. DrawCircle dc = new DrawCircle();
  10. dc.go();
  11. }
  12. public void go(){
  13. JFrame frame = new JFrame("fun");
  14. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. MyDrawPanel dp = new MyDrawPanel();
  16. frame.getContentPane().add(dp);
  17. frame.setSize(300,300);
  18. frame.setVisible(true);
  19. for (int i=0 ; i<130 ; i++ ){
  20. x++;
  21. y++;
  22. dp.repaint();
  23.  
  24. try{
  25. Thread.sleep(50);
  26. }catch(Exception ex){
  27. }
  28. }
  29. }
  30. class MyDrawPanel extends JPanel{
  31. public void painComponent(Graphics g){
  32. g.setColor(Color.white);
  33. g.fillRect(0,0,this.getWidth(), this.getHeight());
  34. g.setColor(Color.red);
  35. g.fillOval(x,y,40,40);
  36.  
  37. }
  38. }
  39. }
Last edited by painless; Jul 3rd, 2009 at 5:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,603
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 460
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Not getting the right movement of the circle

 
0
  #2
Jul 4th, 2009
Overrider paint() method with MyDrawPanel.
  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class DrawCircle{
  4. int x=70;
  5. int y=70;
  6.  
  7.  
  8. public static void main(String[] args){
  9. DrawCircle dc = new DrawCircle();
  10. dc.go();
  11. }
  12. public void go(){
  13. JFrame frame = new JFrame("fun");
  14. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. MyDrawPanel dp = new MyDrawPanel();
  16.  
  17. frame.getContentPane().add(dp);
  18. frame.setSize(300,300);
  19. frame.setVisible(true);
  20. int xdir=0,ydir=0;
  21. while(true){
  22. if(x<=10) xdir=0;
  23. if(x>=200) xdir=1;
  24. if(y<=10) ydir=0;
  25. if(y>=200) ydir=1;
  26.  
  27. if(xdir==0)
  28. x+=5;
  29. else
  30. x-=5;
  31. if(ydir==0)
  32. y+=10;
  33. else
  34. y-=10;
  35.  
  36. dp.repaint();
  37.  
  38. try{
  39. Thread.sleep(50);
  40. }catch(Exception ex){
  41. }
  42. }
  43. }
  44. class MyDrawPanel extends JPanel{
  45. public void paint(Graphics g){
  46. g.setColor(Color.white);
  47. g.fillRect(0,0,this.getWidth(), this.getHeight());
  48. g.setColor(Color.red);
  49. g.fillOval(x,y,40,40);
  50.  
  51. }
  52. }
  53. }
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC