Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Ron McLeod
Paul Clapham
Devaka Cooray
Liutauras Vilda
Sheriffs:
Jeanne Boyarsky
paul wheaton
Henry Wong
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Tim Moores
Carey Brown
Mikalai Zaikin
Bartenders:
Lou Hamers
Piet Souris
Frits Walraven
Forum:
Swing / AWT / SWT
animation
Anandh Ramesh
Ranch Hand
Posts: 61
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi,
i have a text which i have to move from a point(x1,y1) to a point(x2,y2). How do i animate this motion?
any help is greatly appreciated.
cheers,<br />Anandh
Craig Wood
Ranch Hand
Posts: 1535
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.awt.*; import java.awt.font.*; import java.awt.geom.Ellipse2D; import javax.swing.*; public class MovingText extends JPanel { Point p1, p2, loc; String text = "abc"; public MovingText() { p1 = new Point(25, 25); p2 = new Point(350, 350); loc = p1.getLocation(); } public void addNotify() { super.addNotify(); Thread thread = new Thread(new Runner()); thread.setPriority(Thread.NORM_PRIORITY); thread.start(); } 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.fill(new Ellipse2D.Double(p1.x-2, p1.y-2, 4, 4)); g2.setPaint(Color.blue); g2.fill(new Ellipse2D.Double(p2.x-2, p2.y-2, 4, 4)); g2.setPaint(Color.green.darker()); Font font = g2.getFont().deriveFont(18f); g2.setFont(font); FontRenderContext frc = g2.getFontRenderContext(); float width = (float)font.getStringBounds(text, frc).getWidth(); LineMetrics lm = font.getLineMetrics(text, frc); float height = lm.getAscent() + lm.getDescent(); float x = loc.x - width/2; float y = loc.y + height/2 - lm.getDescent(); g2.drawString(text, x, y); } private class Runner implements Runnable { final int steps = 100; int step = 0; public void run() { while(step <= steps) { try { Thread.sleep(100); } catch(InterruptedException e) { System.out.println("interrupt"); break; } double distance = p1.distance(p2); double progress = step*(distance/steps); double dy = p2.y - p1.y; double dx = p2.x - p1.x; double theta = Math.atan2(dy, dx); int x = p1.x + (int)(progress*Math.cos(theta)); int y = p1.y + (int)(progress*Math.sin(theta)); loc.setLocation(x, y); repaint(); step++; } } } public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new MovingText()); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
pdf & rtf export options are not coming on jsp using displaytag table.
Question on Garbage Collection
Servlet desktop application
Two instances of a servlet
Pagination -- Composite to List?
More...