Advertisement
Guest User

Untitled

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