Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.99 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.Image;
  6. import java.awt.geom.Ellipse2D;
  7. import java.awt.geom.Rectangle2D;
  8.  
  9. import javax.swing.JPanel;
  10.  
  11. class LilacChaserPanel extends JPanel implements Runnable{
  12.  
  13.  
  14.     // CS324e students. Add class constants here
  15.     public static final int SIZE = 500;
  16.    
  17.     private final int NUM_PATCHES=12;
  18.    
  19.     private int NUM_CIRCLES=10;
  20.    
  21.     // instance variables for animation
  22.     private Thread animator;
  23.     private boolean running;
  24.    
  25.     private int counter = 0;
  26.    
  27.    
  28.     // could not figure out how to get the panel bg to change, could only
  29.     // change the frame bg... so using this to represent the bg that the
  30.     // code below uses in drawing the concentric circles
  31.     private int bg_red = 170;
  32.     private int bg_green = 170;
  33.     private int bg_blue = 170;
  34.     private int omit;
  35.    
  36.     // why are we using this instead of just a regular Graphics2D object?
  37.     private Image doubleBufferImage;
  38.     private int delay; // how long to sleep
  39.    
  40.     // CS324e students. Add you instance variables here
  41.     private int currX=0;
  42.     private int currY=0;
  43.    
  44.    
  45.    
  46.     public LilacChaserPanel(int initialDelay){
  47.         setBackground(Color.WHITE);
  48.         setPreferredSize(new Dimension(SIZE, SIZE));
  49.          
  50.          // this seems to have no effect... can only change the background of panel itself...
  51.          
  52.          delay = initialDelay;
  53.     }
  54.  
  55.  
  56.     /*
  57.      * "Notifies this component that it now has a parent component.
  58.      * When this method is invoked, the chain of parent components
  59.      * is set up with KeyboardAction event listeners.
  60.      */
  61.     public void addNotify() {
  62.         super.addNotify();
  63.         startAnimation();
  64.     }
  65.  
  66.     private void startAnimation() {
  67.      
  68.         if(animator == null || !running) {
  69.             animator = new Thread(this);
  70.             animator.start();
  71.         }
  72.     }
  73.  
  74.     public void stopAnimation() {
  75.         running = false;
  76.     }
  77.  
  78.     // set the sleep time for the animation to a new value
  79.     public void setDelay(int newDelay) {
  80.         if(newDelay < 5)
  81.             newDelay = 5;
  82.         delay = newDelay;
  83.     }
  84.  
  85.     public void run() {
  86.         // required by Runnable interface
  87.         running = true;
  88.         // perform animation loop
  89.         while(running) {
  90.             update();
  91.             render();
  92.             repaint();
  93.             try {
  94.                 // System.out.println("Trying to sleep!!");
  95.                 Thread.sleep(delay);
  96.             }
  97.             catch(InterruptedException e){/*nothing to do*/}
  98.         }
  99.         System.exit(0); // when thread stops, stop program
  100.     }
  101.  
  102.     private void update() {
  103.         // change as necessary
  104.        
  105.         // this is where you can have a counter that changes a variable that will tell
  106.         // render which ellipse to omit
  107.        
  108.         if(counter == 12) {
  109.            
  110.             counter = 0;
  111.         }
  112.        
  113.         if(counter == 0) {
  114.             omit =0;
  115.             System.out.println("changed omit to 0");
  116.         }
  117.        
  118.          
  119.        
  120.         if(counter != 12) {
  121.             counter++;
  122.         }
  123.        
  124.        
  125.        
  126.        
  127.        
  128.    
  129.      
  130.        
  131.        
  132.        
  133.     }
  134.  
  135.     private void render() {
  136.        
  137.    
  138.    
  139.         Graphics2D dbg;
  140.         if(doubleBufferImage == null) {
  141.             doubleBufferImage = createImage(SIZE, SIZE);
  142.         }
  143.        
  144.         dbg = (Graphics2D) doubleBufferImage.getGraphics();
  145.  
  146.         // Add code to draw next part of animation
  147.         // doubleBufferGraphics.translate(int, int) and
  148.         // doubleBufferGraphics.rotate(int, int) are very useful methods!
  149.        
  150.         //dbg.setColor(Color.WHITE);
  151.         //dbg.fill(background);
  152.      
  153.        
  154.       // for < num patches
  155.         // how much if any does passing a reference to the graphics object slow down the rendering?
  156.        
  157.          //draw the target
  158.         Rectangle2D.Double rect1 = new Rectangle2D.Double(226, 239.5, 25, 3);
  159.         dbg.setColor(Color.BLACK);
  160.         dbg.fill(rect1);
  161.         dbg.draw(rect1);
  162.        
  163.         Rectangle2D.Double rect2 = new Rectangle2D.Double(237, 228, 3, 25);
  164.         dbg.setColor(Color.BLACK);
  165.         dbg.fill(rect2);
  166.         dbg.draw(rect2);
  167.        
  168.        
  169.        
  170.        
  171.         // detect a variable which tells which one to omit.. it will be counting
  172.         // from 0 to 11...  the first time 0 will be omitted, then 1, and so forth
  173.         //
  174.          
  175.         currX = 270;
  176.         currY = 40;
  177.          
  178.         if(omit !=0) {  
  179.             drawCircles(dbg);
  180.            
  181.         }
  182.        
  183.          
  184.            
  185.          
  186.         dbg.rotate(Math.toRadians(30.0),295, 65 );
  187.         dbg.translate(100.0, 0.0);
  188.        
  189.        
  190.             drawCircles(dbg);
  191.          
  192.          
  193.        
  194.          
  195.         dbg.rotate(Math.toRadians(30.0),295, 65 );
  196.         dbg.translate(100.0, 0.0);
  197.         if(omit !=2) {
  198.             drawCircles(dbg);
  199.         }
  200.        
  201.          
  202.         dbg.rotate(Math.toRadians(30.0),295, 65 );
  203.         dbg.translate(100.0, 0.0);
  204.         if(omit !=3) {
  205.             drawCircles(dbg);
  206.         }
  207.        
  208.          
  209.         dbg.rotate(Math.toRadians(30.0),295, 65 );
  210.         dbg.translate(100.0, 0.0);
  211.         if(omit !=4) {
  212.             drawCircles(dbg);
  213.         }
  214.        
  215.         dbg.rotate(Math.toRadians(30.0),295, 65 );
  216.         dbg.translate(100.0, 0.0);
  217.         if(omit !=5) {
  218.             drawCircles(dbg);
  219.         }
  220.        
  221.         dbg.rotate(Math.toRadians(30.0),295, 65 );
  222.         dbg.translate(100.0, 0.0);
  223.         if(omit !=6) {
  224.             drawCircles(dbg);
  225.         }
  226.        
  227.         dbg.rotate(Math.toRadians(30.0),295, 65 );
  228.         dbg.translate(100.0, 0.0);
  229.         if(omit !=7) {
  230.             drawCircles(dbg);
  231.         }
  232.        
  233.         dbg.rotate(Math.toRadians(30.0),295, 65 );
  234.         dbg.translate(100.0, 0.0);
  235.    
  236.         if(omit !=8) {
  237.             drawCircles(dbg);
  238.         }
  239.        
  240.         dbg.rotate(Math.toRadians(30.0),295, 65 );
  241.         dbg.translate(100.0, 0.0);
  242.        
  243.             drawCircles(dbg);
  244.        
  245.        
  246.         dbg.rotate(Math.toRadians(30.0),295, 65 );
  247.         dbg.translate(100.0, 0.0);
  248.        
  249.             drawCircles(dbg);
  250.        
  251.        
  252.          
  253.        
  254.         dbg.rotate(Math.toRadians(30.0),295, 65 );
  255.         dbg.translate(100.0, 0.0);
  256.        
  257.        
  258.             drawCircles(dbg);
  259.        
  260.  
  261.        
  262.        
  263.        
  264.     }
  265.    
  266.     public void drawCircles(Graphics2D dbg) {
  267.         int red=0;
  268.         int green=0;
  269.         int blue=0;
  270.         int currWidth=50;
  271.         double circleX= (double) currX;
  272.         double circleY= (double) currY;
  273.          for (int x=0; x < NUM_CIRCLES; x++) {
  274.                 // take background color and if x==0
  275.             //     -add 8 to red and blue
  276.             //     -subtract 17 from the green
  277.             //  else add 10 to red and blue, subtract 20 from green
  278.            
  279.             if(x == 0){
  280.                 red = bg_red + 8;
  281.                 green = bg_green - 17;
  282.                 blue = bg_blue + 8;
  283.                
  284.             }
  285.             else {
  286.                 red = red + 8;
  287.                 green = green - 17;
  288.                 blue = blue + 8;
  289.                
  290.             }
  291.            
  292.            
  293.             Ellipse2D.Double ellipse = new Ellipse2D.Double(circleX, circleY, currWidth, currWidth);
  294.            
  295.             dbg.setColor(new Color(red, green, blue));
  296.             dbg.fill(ellipse);
  297.             dbg.draw(ellipse);
  298.            
  299.             // couldn't the circles to end be centered properly without using decimals when currWidth-5,
  300.             // but 6 seems to work fine without them
  301.             currWidth = currWidth-6;
  302.    
  303.            
  304.             circleX = circleX + 3;
  305.             circleY = circleY + 3;
  306.          }
  307.     }
  308.  
  309.  
  310.     public void paintComponent(Graphics g) {
  311.         super.paintComponent(g);
  312.         g.drawImage(doubleBufferImage, 0, 0, null);
  313.     }
  314.    
  315.    
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement