Results 1 to 9 of 9
  1. #1
    BlitzA is offline Member
    Join Date
    Dec 2007
    Posts
    11
    Rep Power
    0

    Default Movement of balls

    Hey, I'm trying to get two balls to more around inside the larger ball to create a ying yang sign that spins around while moving right. A little like the Hyduken on street fighter.

    - My first problem is I can't stop the grey semi-circle flickering, I have stopped the blue semi-circle flickering with dWin.hold() and dWin.release() but this doesn't affect the second semi-circle for some reason.

    - The second problem is that I need some idea of how to get the little circles moving in the right way around inside the bigger circle (made out of the semi-circles.

    Cheers for any advise.

    Java Code:
    import java.awt.Color;
    import element.*;
    
    public class Hyduken
    {
    	
    	public static void pause(long waitTime)
    	{
            long startTime = System.currentTimeMillis();
            long stopTime = startTime + waitTime;
            while( System.currentTimeMillis() < stopTime)
    		{
    			//nothing goes here - meaning do nothing
    		}
    	}
    	
    	public static void main ( String[] Args )
    	{
    		int x = 650;
    		int y = 500;
    		int y3 = 500;
    		
    		int x2 = 650;
    		int y2 = 500;
    		
    		int degreestart = 100;
    		int degreeend   = 100;
    		
    		int degreestart2 = 100;
    		int degreeend2 = 100;
    		
    		DrawingWindow dWin = new DrawingWindow( 1000, 1000 );
    		
    		Rect Background = new Rect ( 0, 0, 1000, 1000 );
    		dWin.setForeground( Color.black );
    		dWin.draw( Background );
    		dWin.fill( Background );
    		
    		while( x > 300)
    		{
    			dWin.setForeground( Color.blue );
    			Arc Arc1 = new Arc( x2-50, y2-50, 80, 80, degreestart -10, degreeend +80 );
    			dWin.fill( Arc1);
    			dWin.setForeground( Color.blue );
    			Oval Oval1 = new Oval( x-30, y-50, 41, 41 );
    			dWin.fill( Oval1 );
    			dWin.setForeground( Color.gray );
    			Arc Arc2 = new Arc( x2-50, y2-50, 80, 80, degreestart2-190, degreeend2 +80 );
    			dWin.fill( Arc2 );
    			dWin.setForeground( Color.gray );
    			Oval Oval2 = new Oval( x-30, y3-11, 41, 41 );
    			dWin.fill( Oval2 );
    			
    			x-=12;
    			y+=1;
    			y3-=1;
    			x2-=10;
    			degreestart +=10;
    			degreestart2 +=10;
    			pause(100);
    			
    			dWin.hold();
    			dWin.setForeground( Color.black );
    			dWin.fill( Oval1 );
    			//dWin.fill( Oval2 );
    			dWin.fill( Arc1 );
    			dWin.fill( Arc2 );
    			dWin.release();
    			
    		}
    		
        }
    }

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Animation

    Hello BlitzA.

    If I understand you correctly, you want to modify your program so that:
    1. It does not flicker
    2. It is animated
    3. It involves rotating shapes

    To stop the flicker, you will need to create a buffer to draw on and then draw the buffer onto your screen component. To create animation, you will need to create a thread. Finally, to rotate shapes, you must us the Math.sin() and Math.cos() methods.

    Do you want me to create an example for you and attach it later? ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    BlitzA is offline Member
    Join Date
    Dec 2007
    Posts
    11
    Rep Power
    0

    Default

    ok sure, that would be good, at least so that I can see how it should be done.

    What is wrong with the way I am trying to do it, it is moving and almost works, have you tried my code out? :D

    I'm new to java and haven't done threads or created buffers, but it would be good to see this anyway.

    Cheers.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Hyduken on street fighter.
    I didn't find an example of this so used two balls (trying to get two balls to more around inside the larger ball) to illustrate one way you could approach this.
    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class SpinTest extends JPanel implements Runnable {
        double cx = 140, cy = 140;
        Ellipse2D.Double green = new Ellipse2D.Double(cx-50,cy-50,100,100);
        Ellipse2D.Double red   = new Ellipse2D.Double(cx-20,cy-50,40,40);
        Ellipse2D.Double blue  = new Ellipse2D.Double(cx-20,cy+10,40,40);
        Shape[] shapes = { green, red, blue };
        double xLoc = 0;
        int dx = 2;
        double theta = 0;
        double thetaInc = Math.toRadians(3);
        long delay = 100;
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setPaint(Color.red);
            g2.draw(shapes[1]);
            g2.setPaint(Color.blue);
            g2.draw(shapes[2]);
            g2.setPaint(Color.green.darker());
            g2.draw(shapes[0]);
            g2.setPaint(Color.magenta);
            Point2D.Double p = getCenter(shapes[0]);
            g2.fill(new Ellipse2D.Double(p.x-2, p.y-2, 4, 4));
       }
    
        public void run() {
            boolean move = true;
            while(move) {
                try {
                    Thread.sleep(delay);
                } catch(InterruptedException e) {
                    move = false;
                    System.out.println("interrupted");
                }
                advance();
            }
        }
    
        private void advance() {
            checkBoundries();
            xLoc += dx;
            AffineTransform at = AffineTransform.getTranslateInstance(xLoc,0);
            shapes[0] = at.createTransformedShape(green);
            theta += thetaInc;
            Point2D.Double mp = getCenter(shapes[0]);
            Point2D.Double p = getCenter(red);
            double x = mp.x - p.x;
            //double y = mp.y - p.y;
            at = AffineTransform.getTranslateInstance(x, 0);
            at.rotate(theta, cx, cy);
            shapes[1] = at.createTransformedShape(red);
            shapes[2] = at.createTransformedShape(blue);
            repaint();
        }
    
        private Point2D.Double getCenter(Shape s) {
            Point2D.Double p = new Point2D.Double();
            Rectangle r = s.getBounds();
            p.x = r.getCenterX();
            p.y = r.getCenterY();
            return p;
        }
    
        private void checkBoundries() {
            Rectangle r = shapes[0].getBounds();
            if(r.x + dx < 0 || r.x + r.width + dx > getWidth())
                dx *= -1;
        }
    
        private void start() {
            while(!isVisible()) {
                try {
                    Thread.sleep(25);
                } catch(InterruptedException e) {
                    break;
                }
            }
            Thread thread = new Thread(this);
            thread.setPriority(Thread.NORM_PRIORITY);
            thread.start();
        }
    
        public static void main(String[] args) {
            SpinTest test = new SpinTest();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(test);
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
            test.start();
        }
    }

  5. #5
    BlitzA is offline Member
    Join Date
    Dec 2007
    Posts
    11
    Rep Power
    0

    Default

    Ok that looks pretty good. I don't understand how that all works though.. like I said I'm new to java.

    I'll have to see if I can study it.

    this is what i've came up with lol.

    Java Code:
    import java.awt.Color;
    import element.*;
    
    public class Hyduken
    {
    	
    	public static void pause(long waitTime)
    	{
            long startTime = System.currentTimeMillis();
            long stopTime = startTime + waitTime;
            while( System.currentTimeMillis() < stopTime)
    		{
    			//nothing goes here - meaning do nothing
    		}
    	}
    	
    	public static void main ( String[] Args )
    	{
    		int x = 650;
    		int y = 500;
    		int x3 = 650;
    		int y3 = 500;
    		
    		int x2 = 650;
    		int y2 = 500;
    		
    		int degreestart = 100;
    		int degreeend   = 100;
    		
    		int degreestart2 = 100;
    		int degreeend2 = 100;
    		
    		DrawingWindow dWin = new DrawingWindow( 1000, 1000 );
    		
    		Rect Background = new Rect ( 0, 0, 1000, 1000 );
    		dWin.setForeground( Color.black );
    		dWin.draw( Background );
    		dWin.fill( Background );
    		
    		while( x > 580)
    		{
    			dWin.setForeground( Color.blue );
    			Arc Arc1 = new Arc( x2-50, y2-50, 80, 80, degreestart -10, degreeend +80 );
    			dWin.fill( Arc1 );
    			dWin.setForeground( Color.gray );
    			Arc Arc2 = new Arc( x2-50, y2-50, 80, 80, degreestart2-190, degreeend2 +80 );
    			dWin.fill( Arc2 );
    			dWin.setForeground( Color.blue );
    			Oval Oval1 = new Oval( x-30, y-50, 41, 41 );
    			dWin.fill( Oval1 );
    			dWin.setForeground( Color.gray );
    			Oval Oval2 = new Oval( x3-30, y3-11, 41, 41 );
    			dWin.fill( Oval2 );
    			
    			x-=14;
    			y+=1;
    			x3-=8;
    			y3-=1;
    			x2-=11;
    			degreestart +=10;
    			degreestart2 +=10;
    			pause(300);
    			
    			dWin.hold();
    			dWin.setForeground( Color.black );
    			dWin.fill( Oval1 );
    			dWin.fill( Oval2 );
    			dWin.fill( Arc1 );
    			dWin.fill( Arc2 );
    			dWin.release();
    		}
    		
    		while( x > 545 )
    		{
    			dWin.setForeground( Color.blue );
    			Arc Arc3 = new Arc( x2-50, y2-50, 80, 80, degreestart -10, degreeend +80 );
    			dWin.fill( Arc3 );
    			dWin.setForeground( Color.gray );
    			Arc Arc4 = new Arc( x2-50, y2-50, 80, 80, degreestart2-190, degreeend2 +80 );
    			dWin.fill( Arc4 );
    			dWin.setForeground( Color.blue );
    			Oval Oval3 = new Oval( x-30, y-50, 41, 41 );
    			dWin.fill( Oval3 );
    			dWin.setForeground( Color.gray );
    			Oval Oval4 = new Oval( x3-30, y3-11, 41, 41 );
    			dWin.fill( Oval4 );
    			
    			x-=14;
    			y+=5;
    			x3-=11;
    			y3-=3;
    			x2-=12;
    			degreestart +=10;
    			degreestart2 +=10;
    			pause(300);
    			
    			dWin.hold();
    			dWin.setForeground( Color.black );
    			dWin.fill( Oval3 );
    			dWin.fill( Oval4 );
    			dWin.fill( Arc3 );
    			dWin.fill( Arc4 );
    			dWin.release();
    		}		
    		while( x > 500 )
    		{
    			dWin.setForeground( Color.blue );
    			Arc Arc5 = new Arc( x2-50, y2-50, 80, 80, degreestart -10, degreeend +80 );
    			dWin.fill( Arc5 );
    			dWin.setForeground( Color.gray );
    			Arc Arc6 = new Arc( x2-50, y2-50, 80, 80, degreestart2-190, degreeend2 +80 );
    			dWin.fill( Arc6 );
    			dWin.setForeground( Color.blue );
    			Oval Oval5 = new Oval( x-30, y-50, 41, 41 );
    			dWin.fill( Oval5 );
    			dWin.setForeground( Color.gray );
    			Oval Oval6 = new Oval( x3-30, y3-11, 41, 41 );
    			dWin.fill( Oval6 );
    			
    			x-=10;
    			y+=3;
    			x3-=10;
    			y3-=2;
    			x2-=11;
    			degreestart +=10;
    			degreestart2 +=10;
    			pause(300);
    			
    			dWin.hold();
    			dWin.setForeground( Color.black );
    			dWin.fill( Oval5 );
    			dWin.fill( Oval6 );
    			dWin.fill( Arc5 );
    			dWin.fill( Arc6 );
    			dWin.release();
    		}
        }
    }

  6. #6
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Arrow

    Quote Originally Posted by BlitzA View Post
    ok sure, that would be good, at least so that I can see how it should be done.

    What is wrong with the way I am trying to do it, it is moving and almost works, have you tried my code out? :D

    I'm new to java and haven't done threads or created buffers, but it would be good to see this anyway.

    Cheers.
    Okay, I'll make an example for you. :D

    I like to use buffers and threads because they improve performance. I'll use them in my example, then you can learn from it. These tools also simplify programming. If you use threading, it is unnecessary to create a pause() method. Looks like hardwired also used threading. I'll document my code for you.

    I'll post soon.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  7. #7
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Example done

    Hello BlitzA.

    I have completed an example for you. It is documented and implements threading and buffers as you asked. The symbol should look like this:



    I have attached the code, Main.java, and the executable, YinYang.jar. I hope this helps. ;)
    Attached Files Attached Files
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  8. #8
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    
    public class SpinningTaiChi extends JPanel implements Runnable {
        BufferedImage image;
        Point loc = new Point();
        int dx = 2;
        double theta = 0;
        double thetaInc = Math.toRadians(3);
        AffineTransform at = new AffineTransform();
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            if(image == null)
                initImage();
            g2.drawRenderedImage(image, at);
        }
    
        public void run() {
            boolean more = true;
            while(more) {
                try {
                    Thread.sleep(50);
                } catch(InterruptedException e) {
                    System.out.println("run interrupted");
                    more = false;
                }
                step();
            }
        }
    
        private void step() {
            checkBoundries();
            setTransform();
            repaint();
        }
    
        private void checkBoundries() {
            if(loc.x + dx < 0 || loc.x + image.getWidth() + dx > getWidth())
                dx *= -1;
        }
    
        private void setTransform() {
            loc.x += dx;
            theta += thetaInc;
            at.setToTranslation(loc.x, loc.y);
            at.rotate(theta, image.getWidth()/2, image.getHeight()/2);
        }
    
        private void start() {
            while(image == null) {
                try {
                    Thread.sleep(25);
                } catch(InterruptedException e) {
                    System.out.println("waiting interrupted");
                    break;
                }
            }
            Thread thread = new Thread(this);
            thread.setPriority(Thread.NORM_PRIORITY);
            thread.start();
        }
    
        private void initImage() {
            int s = 160;
            int type = BufferedImage.TYPE_INT_ARGB_PRE;
            image = new BufferedImage(s, s, type);
            Graphics2D g2 = image.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            Ellipse2D.Double e = new Ellipse2D.Double(0,0,s,s);
            g2.setPaint(Color.white);
            g2.fill(e);
            Rectangle2D.Double r1 = new Rectangle2D.Double(0,s/4,s/2,s/2);
            Rectangle2D.Double r2 = new Rectangle2D.Double(s/2,s/4,s/2-1,s/2);
            Arc2D.Double a1 = new Arc2D.Double(r1,0,180,Arc2D.OPEN);
            Arc2D.Double a2 = new Arc2D.Double(r2,180,180,Arc2D.OPEN);
            Arc2D.Double top = new Arc2D.Double(0,0,s-1,s,0,180,Arc2D.OPEN);
            Path2D.Double path = new Path2D.Double(Path2D.WIND_EVEN_ODD);
            path.append(a1, false);
            path.append(a2, false);
            path.append(top, false);
            g2.setPaint(Color.black);
            g2.fill(path);
            g2.draw(path);
            g2.fill(new Ellipse2D.Double(s/4-15,s/2-15,30,30));
            g2.setPaint(Color.white);
            g2.fill(new Ellipse2D.Double(s*3/4-15,s/2-15,30,30));
            g2.dispose();
            double y = (getHeight() - s)/2;
            loc.setLocation(0,y);
            at.setToTranslation(0,y);
        }
    
        public static void main(String[] args) {
            SpinningTaiChi test = new SpinningTaiChi();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.setSize(600,400);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
            test.start();
        }
    }
    Edit: Moved last three lines of start method which were mistakenly placed inside while loop.
    Last edited by hardwired; 01-10-2008 at 03:03 AM.

  9. #9
    BlitzA is offline Member
    Join Date
    Dec 2007
    Posts
    11
    Rep Power
    0

    Default

    ok thanks for all the help, ill look into all that. Right now that's to hard to understand -_- but cheers.

Similar Threads

  1. Multiple bouncing balls
    By Algar in forum AWT / Swing
    Replies: 2
    Last Post: 04-24-2008, 09:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •