84766

Using threads on animations in Java

Question:

I have created a class whit two shapes namely two ovals. Here I draw them.

import ...; public class Drawings extends JPanel{ public double degrees = 0; public void paintComponent(Graphics g){ super.paintComponent(g); int xcen = getWidth() / 2; int ycen = getHeight()/ 2; int radius = 10; degrees++; double radians = Math.toRadians(degrees); int posx = (int)(100.getDistance() * Math.cos(radians)); int posy = (int)(100.getDistance() * Math.sin(radians)); g.setColor(Color.BLUE); g.FillOval(xcen + posx, ycen + posy, 20, 20); g.setColor(Color.GREEN); g.drawOval(xcen + posx, ycen + posy, 100,100) } }

Now I implement it in a main.

import ....; public class Animate extends JFrame{ public static void main(String [] args) { JFrame window = new JFrame(); window.add(new Drawings()); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setSize(500,500); window.setLocationRelativeTo(null); window.setVisible(true); //now I implement the thread to animate the two shapes Thread paintThread = new Thread(new Runnable(){ @Override public void run(){ while(true) { window.repaint(); try{ Thread.sleep(25);//determines how slow the ovals will move }catch(InterruptedException e){ e.printStackTrace(); } } } }); paintThread.start();//start the animation } }

When the program runs the two Ovals rotate on the screen. But the two ovals rotates at the same speed as I would expect but I would like the two ovals to move at diffident speeds.

I have tried using a method to move them at different speed but with no success. How would I get the two ovals moving at different speeds?

Answer1:

Make a class to represent an oval. Make two instances. Give the two instances different angular velocities. Currently because you increment degrees by 1.0 every 25 ms you have an angular velocity fixed at 40 degrees per second. If each oval has its own degrees field and you increment the two by different amounts, the ovals will rotate at different rates.

Answer2:

The easiest way is:

import ...; public class Drawings extends JPanel{ public double degrees = 0; private int firstOvalSpeed; private int secondOvalSpeed; public void paintComponent(Graphics g){ super.paintComponent(g); int xcen = getWidth() / 2; int ycen = getHeight()/ 2; int radius = 10; degrees++; double radians = Math.toRadians(degrees); int posx = (int)(100.getDistance() * Math.cos(radians)); int posy = (int)(100.getDistance() * Math.sin(radians)); g.setColor(Color.BLUE); g.FillOval(xcen + posx*firstOvalSpeed, ycen + posy*firstOvalSpeed, 20, 20); g.setColor(Color.GREEN); g.drawOval(xcen + posx*secondOvalSpeed, ycen + posy*secondOvalSpeed, 100,100) } public void setFirstOvalSpeed(int firstOvalSpeed) { this.firstOvalSpeed = firstOvalSpeed; } public void setSecondOvalSpeed(int secondOvalSpeed) { this.secondOvalSpeed = secondOvalSpeed; } } public class Animate extends JFrame { public static void main(String[] args) { final JFrame window = new JFrame(); Drawings drawings = new Drawings(); drawings.setFirstOvalSpeed(1); drawings.setSecondOvalSpeed(2); window.add(drawings); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setSize(500, 500); window.setLocationRelativeTo(null); window.setVisible(true); // now I implement the thread to animate the two shapes Thread paintThread = new Thread(new Runnable() { @Override public void run() { while (true) { window.repaint(); try { Thread.sleep(25);// determines how slow the ovals will // move } catch (InterruptedException e) { e.printStackTrace(); } } } }); paintThread.start();// start the animation }

}

Recommend

  • Why is the graphic not working when the button number 8 is pressed?
  • opening JFrame from applet
  • Is it a oop good design?
  • Fix position mouse cursor
  • Is it possible to create an algorithm which generates an autogram?
  • Why doesn't this Java paint program paint more than one oval?
  • How should a certain percentage of evenly-distributed elements of a Python list be extracted?
  • Circle shape for ViewPager
  • Copy Object Properties to a Map by Value not by Reference
  • VSS to SVN - Repositories
  • How many entries are in a node of B-Tree of a MySQL Index?
  • image quality issue in flash
  • Defining percentage for random number
  • In shoes, how do I dock a stack to the bottom of the window?
  • Better way to make a thread sleep
  • Fill a column of a numpy array with another array
  • data.table print error when nrow>100
  • What is the equivalent of Matlab's imadjust in python?
  • Python sin and cosine giving incorrect values
  • Get attributes of existing SVG elements and bind as data with d3.js
  • Setting color in a row of a Jtable
  • Stretch a span across a td
  • How can I use a custom function within an expression using the eval dataframe method?
  • How can a flex item keep the same dimensions when it is forced to a new row?
  • Differences between drawing an Ellipse in Android and Java
  • How to return a number as a binary string with a set number of bits in python
  • UIButton Borders Function Only Gives Back White Borders
  • Responsive left sidebar open close
  • Django simple Captcha “No module named fields” error
  • How to modify the colors in the legend of a plot using a fill gradient?
  • C# - Serializing and deserializing static member
  • Bug in WPF DataGrid
  • output of program is not same as passed argument
  • Incrementing object id automatically JS constructor (static method and variable)
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • jquery mobile loadPage not working
  • Akka Routing: Reply's send to router ends up as dead letters
  • Android Google Maps API OnLocationChanged only called once
  • unknown Exception android
  • Checking variable from a different class in C#