| | |
Not getting the right movement of the circle
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 7
Reputation:
Solved Threads: 0
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.
java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.*; public class DrawCircle{ int x=70; int y=70; public static void main(String[] args){ DrawCircle dc = new DrawCircle(); dc.go(); } public void go(){ JFrame frame = new JFrame("fun"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyDrawPanel dp = new MyDrawPanel(); frame.getContentPane().add(dp); frame.setSize(300,300); frame.setVisible(true); for (int i=0 ; i<130 ; i++ ){ x++; y++; dp.repaint(); try{ Thread.sleep(50); }catch(Exception ex){ } } } class MyDrawPanel extends JPanel{ public void painComponent(Graphics g){ g.setColor(Color.white); g.fillRect(0,0,this.getWidth(), this.getHeight()); g.setColor(Color.red); g.fillOval(x,y,40,40); } } }
Last edited by painless; Jul 3rd, 2009 at 5:13 pm.
Overrider paint() method with MyDrawPanel.
Java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.*; public class DrawCircle{ int x=70; int y=70; public static void main(String[] args){ DrawCircle dc = new DrawCircle(); dc.go(); } public void go(){ JFrame frame = new JFrame("fun"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyDrawPanel dp = new MyDrawPanel(); frame.getContentPane().add(dp); frame.setSize(300,300); frame.setVisible(true); int xdir=0,ydir=0; while(true){ if(x<=10) xdir=0; if(x>=200) xdir=1; if(y<=10) ydir=0; if(y>=200) ydir=1; if(xdir==0) x+=5; else x-=5; if(ydir==0) y+=10; else y-=10; dp.repaint(); try{ Thread.sleep(50); }catch(Exception ex){ } } } class MyDrawPanel extends JPanel{ public void paint(Graphics g){ g.setColor(Color.white); g.fillRect(0,0,this.getWidth(), this.getHeight()); g.setColor(Color.red); g.fillOval(x,y,40,40); } } }
Failure is not fatal, but failure to change might be. - John Wooden
![]() |
Similar Threads
- Mouse Movement is slow in VMware Guest OS (Windows NT / 2000 / XP)
- Drawing a circle in Borland C++ builder (was: Help Me!!!!!) (C++)
- Trying to find circumference of circle and area (C)
- Change the direction of a shape with a button (C)
- Trouble creating a boundary around a target or circle (Java)
- Circle class (C++)
- detecting movement with motion sensors (USB Devices and other Peripherals)
Other Threads in the Java Forum
- Previous Thread: Java loop error?
- Next Thread: Help needed to develop an application
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






