Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. import java.awt.Dimension;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. import javax.swing.JButton;
  6. import javax.swing.JFrame;
  7. import javax.swing.JLabel;
  8. import javax.swing.JPanel;
  9. import javax.swing.JTextField;
  10. import javax.swing.Timer;
  11.  
  12.  
  13. public class main {
  14.  
  15. final static int circleCenterX = 350;
  16. final static int circleCenterY = 275;
  17. final static int radius = 275;
  18. static double initialTheta = Math.toRadians(90);
  19. static double theta = initialTheta;
  20. final static double thetaIncrement = -0.015;
  21. static JButton button = new JButton("Go");
  22. static JTextField textfield = new JTextField();
  23. static JLabel getname = new JLabel("Enter name:");
  24. static Timer timer;
  25. static boolean running = false;
  26.  
  27. public static void doit(JPanel panel){
  28. panel.removeAll();
  29. panel.add(getname);
  30. getname.setBounds(50,21,getname.getPreferredSize().width,getname.getPreferredSize().height);
  31. panel.add(textfield);
  32. textfield.setBounds(getname.getLocation().x + getname.getPreferredSize().width + 5, 20, 144,20);
  33. panel.add(button);
  34. button.setBounds(textfield.getLocation().x + 149,20,50,20);
  35. panel.revalidate();
  36. panel.repaint();
  37.  
  38. String textinfo = textfield.getText();
  39. if(textinfo.equals("")){
  40. System.out.println("enter a name man");
  41. }
  42. else{
  43. final String[] text = textinfo.split("");
  44. final int num = text.length;
  45. final JLabel[] letters = new JLabel[num];
  46. for(int i = 0; i < text.length; i ++){
  47. letters[i] = new JLabel();
  48. letters[i].setText(text[i]);
  49. letters[i].setBounds( //
  50. circleCenterX - (int) (Math.sin(theta) * radius), //
  51. circleCenterY - (int) (Math.cos(theta) * radius), //
  52. (int) letters[i].getPreferredSize().getWidth(), (int) letters[i].getPreferredSize().getHeight());
  53. theta -= thetaIncrement;
  54. }
  55.  
  56.  
  57. timer = new Timer(20001/601, new ActionListener(){
  58.  
  59. @Override
  60. public void actionPerformed(ActionEvent e) {
  61. // TODO Auto-generated method stub
  62. running = true;
  63. if(Math.round( Math.toDegrees(theta)) < 270){
  64. for(int i = 0; i < num; i ++){
  65. System.out.println(Math.toDegrees(theta));
  66. letters[i].setBounds( //
  67. circleCenterX - (int) (Math.sin(theta) * radius), //
  68. circleCenterY - (int) (Math.cos(theta) * radius), //
  69. (int) letters[i].getPreferredSize().getWidth(), (int) letters[i].getPreferredSize().getHeight());
  70. theta -= thetaIncrement;
  71. }
  72. }
  73. else if(Math.round(Math.toDegrees(theta)) >= 270 && Math.round(Math.toDegrees(theta)) <= 360){
  74. //((Timer)e.getSource()).setDelay(30001/601);
  75. for(int i = 0; i < num; i ++){
  76. letters[i].setLocation(letters[i].getLocation().x - 10, 275);
  77. if(letters[i].getLocation().x <= 35){
  78. ((Timer)e.getSource()).stop();
  79. theta = Math.toRadians(90);
  80. for(int x = 0; x < num; x++){
  81. if(x == 0){
  82. letters[x].setLocation(35 + (12 * (x)),275);
  83. }
  84. else{
  85.  
  86. letters[x].setLocation(45 + (15 * (x)),275);
  87. }
  88. }
  89. running = false;
  90. }
  91. }
  92. }
  93. }
  94.  
  95. });
  96.  
  97. for(int i = 0; i < text.length; i ++){
  98. panel.add(letters[i]);
  99. }
  100. }
  101. }
  102.  
  103. public static void main(String[] args) {
  104. final double initialTheta = Math.toRadians(90);
  105. // double initialTheta = Math.toRadians(0); // start north
  106. // double initialTheta = Math.toRadians(90); // start west
  107. // double initialTheta = Math.toRadians(180); // start south
  108. // double initialTheta = Math.toRadians(270); // start east
  109.  
  110. JFrame frame = new JFrame();
  111. frame.setSize(new Dimension(800, 700));
  112. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  113. frame.setLocationRelativeTo(null);
  114.  
  115. final JPanel panel = new JPanel();
  116. panel.setLayout(null);
  117. frame.add(panel);
  118.  
  119. panel.add(getname);
  120. getname.setBounds(50,21,getname.getPreferredSize().width,getname.getPreferredSize().height);
  121. panel.add(textfield);
  122. textfield.setBounds(getname.getLocation().x + getname.getPreferredSize().width + 5, 20, 144,20);
  123. panel.add(button);
  124. button.setBounds(textfield.getLocation().x + 149,20,50,20);
  125.  
  126. /*Timer timer = new Timer();
  127. timer.scheduleAtFixedRate(new TimerTask() {
  128. @Override
  129. public void run() {
  130. SwingUtilities.invokeLater(new Runnable() {
  131. @Override
  132. public void run() {
  133. label.rotate();
  134.  
  135. }
  136. });
  137. }
  138. }, 0, 1000l / 60l); // 60 frames per second */
  139.  
  140.  
  141.  
  142.  
  143.  
  144. button.addActionListener(new ActionListener(){
  145.  
  146. @Override
  147. public void actionPerformed(ActionEvent arg0) {
  148. // TODO Auto-generated method stub
  149. doit(panel);
  150. if(running){
  151. }
  152. else{
  153. if(textfield.getText().equals(""));
  154. else{
  155. timer.start();
  156. }
  157.  
  158. }
  159.  
  160. }
  161.  
  162. });
  163.  
  164. frame.setVisible(true);
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement