Advertisement
Guest User

Untitled

a guest
May 25th, 2010
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. import java.awt.*;
  2. import java.util.Random;
  3.  
  4.  
  5. @SuppressWarnings("serial")
  6. public class Practice extends Frame implements Runnable {
  7.     int frameNumber;
  8.     int delay;
  9.     Thread animatorThread;
  10.     static int x1=128, y1=128, x2=384, y2=128, x3=256, y3=256;
  11.  
  12.     Practice(int fps, String windowTitle) {
  13.     super(windowTitle);
  14.     delay = (fps > 0) ? (1000 / fps) : 100;
  15.     }
  16.  
  17.     public void startAnimation() {
  18.     if (animatorThread == null) {
  19.         animatorThread = new Thread(this);
  20.         animatorThread.start();
  21.     }
  22.     }
  23.  
  24.     public void stopAnimation() {
  25.     animatorThread = null;
  26.     }
  27.  
  28.     public boolean mouseDown(Event e, int x, int y) {
  29.     if (animatorThread == null) {
  30.         startAnimation();
  31.     }
  32.     else {
  33.         stopAnimation();
  34.     }
  35.     this.dispose();
  36.     this.setVisible(false);
  37.     return false;
  38.     }
  39.  
  40.     public void run() {
  41.    
  42.     long startTime = System.currentTimeMillis();
  43.    
  44.     Random moves = new Random();
  45.     int r1=0, r2=0, r3=0, r4=0, r5=0, r6=0;
  46.     while (Thread.currentThread() == animatorThread) {
  47.        
  48.         repaint();
  49.        
  50.         try {
  51.         startTime += delay;
  52.         Thread.sleep(Math.max(0,
  53.                       startTime-System.currentTimeMillis()));
  54.         } catch (InterruptedException e) {
  55.         break;
  56.         }
  57.  
  58.        
  59.         frameNumber++;
  60.        
  61.         // Start of application logic
  62.        
  63.         //Code for x1
  64.         if(r1 == 0) {
  65.             r1 = moves.nextInt(100);
  66.             if (r1>49) {
  67.                 r1 = -r1 + 50;
  68.             }
  69.            
  70.         }
  71.         if(r1 != 0) {
  72.             if(r1>0) {
  73.                 x1++;
  74.                 r1--;
  75.             } else if(r1<0) {
  76.                 x1--;
  77.                 r1++;
  78.             }
  79.         }
  80.        
  81.         if(x1==0) {
  82.             r1 = 50;
  83.         } else if(x1==512) {
  84.             r1 = -50;
  85.         }
  86.        
  87.         //Code for x2
  88.         if(r2 == 0) {
  89.             r2 = moves.nextInt(100);
  90.             if (r2>49) {
  91.                 r2 = -r2 + 50;
  92.             }
  93.            
  94.         }
  95.         if(r2 != 0) {
  96.             if(r2>0) {
  97.                 x2++;
  98.                 r2--;
  99.             } else if(r2<0) {
  100.                 x2--;
  101.                 r2++;
  102.             }
  103.         }
  104.        
  105.         if(x2==0) {
  106.             r2 = 50;
  107.         } else if(x2==512) {
  108.             r2 = -50;
  109.         }
  110.        
  111.         //Code for x3
  112.         if(r3 == 0) {
  113.             r3 = moves.nextInt(100);
  114.             if (r3>49) {
  115.                 r3 = -r3 + 50;
  116.             }
  117.            
  118.         }
  119.         if(r3 != 0) {
  120.             if(r3>0) {
  121.                 x3++;
  122.                 r3--;
  123.             } else if(r3<0) {
  124.                 x3--;
  125.                 r3++;
  126.             }
  127.         }
  128.        
  129.         if(x3==0) {
  130.             r3 = 50;
  131.         } else if(x3==512) {
  132.             r3 = -50;
  133.         }
  134.        
  135.         //Code for y1
  136.         if(r4 == 0) {
  137.             r4 = moves.nextInt(100);
  138.             if (r4>49) {
  139.                 r4 = -r4 + 50;
  140.             }
  141.            
  142.         }
  143.         if(r4 != 0) {
  144.             if(r4>0) {
  145.                 y1++;
  146.                 r4--;
  147.             } else if(r4<0) {
  148.                 y1--;
  149.                 r4++;
  150.             }
  151.         }
  152.        
  153.         if(y1==0) {
  154.             r4 = 50;
  155.         } else if(y1==384) {
  156.             r4 = -50;
  157.         }
  158.        
  159.         //Code for y2
  160.         if(r5 == 0) {
  161.             r5 = moves.nextInt(100);
  162.             if (r5>49) {
  163.                 r5 = -r5 + 50;
  164.             }
  165.            
  166.         }
  167.         if(r5 != 0) {
  168.             if(r5>0) {
  169.                 y2++;
  170.                 r5--;
  171.             } else if(r5<0) {
  172.                 y2--;
  173.                 r5++;
  174.             }
  175.         }
  176.        
  177.         if(y2==0) {
  178.             r5 = 50;
  179.         } else if(y2==384) {
  180.             r5 = -50;
  181.         }
  182.        
  183.         //Code for y3
  184.         if(r6 == 0) {
  185.             r6 = moves.nextInt(100);
  186.             if (r6>49) {
  187.                 r6 = -r6 + 50;
  188.             }
  189.            
  190.         }
  191.         if(r6 != 0) {
  192.             if(r6>0) {
  193.                 y3++;
  194.                 r6--;
  195.             } else if(r6<0) {
  196.                 y3--;
  197.                 r6++;
  198.             }
  199.         }
  200.        
  201.         if(y3==0) {
  202.             r6 = 50;
  203.         } else if(y3==384) {
  204.             r6 = -50;
  205.         }
  206.     }
  207.     }
  208.  
  209.     public void paint(Graphics g) {
  210.        
  211.         Graphics2D g2d = (Graphics2D) g;
  212.         g2d.drawLine(512, 0, 512, 768);
  213.         g2d.drawLine(0, 384, 1024, 384);
  214.        
  215.         //Original, Quadrant 2
  216.         g2d.drawLine(x1, y1, x2, y2);
  217.         g2d.drawLine(x2, y2, x3, y3);
  218.         g2d.drawLine(x3, y3, x1, y1);
  219.        
  220.         //Quadrant 1
  221.         g2d.drawLine(x1+(1024-x1*2), y1, x2+(1024-x2*2), y2);
  222.         g2d.drawLine(x2+(1024-x2*2), y2, x3+(1024-x3*2), y3);
  223.         g2d.drawLine(x3+(1024-x3*2), y3, x1+(1024-x1*2), y1);
  224.        
  225.         //Quadrant 3
  226.         g2d.drawLine(x1, y1+(768-y1*2), x2, y2+(768-y2*2));
  227.         g2d.drawLine(x2, y2+(768-y2*2), x3, y3+(768-y3*2));
  228.         g2d.drawLine(x3, y3+(768-y3*2), x1, y1+(768-y1*2));
  229.        
  230.         //Quadrant 4
  231.         g2d.drawLine(x1+(1024-x1*2), y1+(768-y1*2), x2+(1024-x2*2), y2+(768-y2*2));
  232.         g2d.drawLine(x2+(1024-x2*2), y2+(768-y2*2), x3+(1024-x3*2), y3+(768-y3*2));
  233.         g2d.drawLine(x3+(1024-x3*2), y3+(768-y3*2), x1+(1024-x1*2), y1+(768-y1*2));
  234.        
  235.        
  236.     }
  237.  
  238.     @SuppressWarnings("deprecation")
  239.     public static void main(String args[]) {
  240.     Practice animator = null;
  241.     int fps = 20;
  242.  
  243.     // Get frames per second from the command line argument
  244.     if (args.length > 0) {
  245.         fps = Integer.parseInt(args[0]);
  246.     }
  247.    
  248.     animator = new Practice(fps, "Animator");
  249.     animator.startAnimation();
  250.     animator.resize(200, 60);
  251.     animator.show();
  252.     animator.setSize(1024, 768);
  253.     animator.setTitle("Triangles 1.1 by Franz Sarmiento");
  254.     animator.setResizable(false);
  255.     animator.setLocationRelativeTo(null);
  256.    
  257.    
  258.     }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement