Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-07-2008, 03:16 AM
Member
 
Join Date: Dec 2007
Posts: 11
BlitzA is on a distinguished road
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.

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(); } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-07-2008, 10:38 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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?
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-07-2008, 05:32 PM
Member
 
Join Date: Dec 2007
Posts: 11
BlitzA is on a distinguished road
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?

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

Cheers.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-08-2008, 01:05 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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.
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(); } }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-08-2008, 05:17 AM
Member
 
Join Date: Dec 2007
Posts: 11
BlitzA is on a distinguished road
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.

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(); } } }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-08-2008, 01:45 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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?

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.

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.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-08-2008, 06:59 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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
File Type: zip Yin Yang.zip (5.8 KB, 7 views)
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-09-2008, 03:49 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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 04:03 AM.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 01-09-2008, 05:30 PM
Member
 
Join Date: Dec 2007
Posts: 11
BlitzA is on a distinguished road
ok thanks for all the help, ill look into all that. Right now that's to hard to understand -_- but cheers.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple bouncing balls Algar AWT / Swing 2 04-24-2008 10:35 PM


All times are GMT +3. The time now is 02:19 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org