Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.geom.Ellipse2D;
- import java.awt.geom.Rectangle2D;
- import javax.swing.JPanel;
- class LilacChaserPanel extends JPanel implements Runnable{
- // CS324e students. Add class constants here
- public static final int SIZE = 500;
- private final int NUM_PATCHES=12;
- private int NUM_CIRCLES=10;
- // instance variables for animation
- private Thread animator;
- private boolean running;
- private int counter = 0;
- // could not figure out how to get the panel bg to change, could only
- // change the frame bg... so using this to represent the bg that the
- // code below uses in drawing the concentric circles
- private int bg_red = 170;
- private int bg_green = 170;
- private int bg_blue = 170;
- private int omit;
- // why are we using this instead of just a regular Graphics2D object?
- private Image doubleBufferImage;
- private int delay; // how long to sleep
- // CS324e students. Add you instance variables here
- private int currX=0;
- private int currY=0;
- public LilacChaserPanel(int initialDelay){
- setBackground(Color.WHITE);
- setPreferredSize(new Dimension(SIZE, SIZE));
- // this seems to have no effect... can only change the background of panel itself...
- delay = initialDelay;
- }
- /*
- * "Notifies this component that it now has a parent component.
- * When this method is invoked, the chain of parent components
- * is set up with KeyboardAction event listeners.
- */
- public void addNotify() {
- super.addNotify();
- startAnimation();
- }
- private void startAnimation() {
- if(animator == null || !running) {
- animator = new Thread(this);
- animator.start();
- }
- }
- public void stopAnimation() {
- running = false;
- }
- // set the sleep time for the animation to a new value
- public void setDelay(int newDelay) {
- if(newDelay < 5)
- newDelay = 5;
- delay = newDelay;
- }
- public void run() {
- // required by Runnable interface
- running = true;
- // perform animation loop
- while(running) {
- update();
- render();
- repaint();
- try {
- // System.out.println("Trying to sleep!!");
- Thread.sleep(delay);
- }
- catch(InterruptedException e){/*nothing to do*/}
- }
- System.exit(0); // when thread stops, stop program
- }
- private void update() {
- // change as necessary
- // this is where you can have a counter that changes a variable that will tell
- // render which ellipse to omit
- if(counter == 12) {
- counter = 0;
- }
- if(counter == 0) {
- omit =0;
- System.out.println("changed omit to 0");
- }
- if(counter != 12) {
- counter++;
- }
- }
- private void render() {
- Graphics2D dbg;
- if(doubleBufferImage == null) {
- doubleBufferImage = createImage(SIZE, SIZE);
- }
- dbg = (Graphics2D) doubleBufferImage.getGraphics();
- // Add code to draw next part of animation
- // doubleBufferGraphics.translate(int, int) and
- // doubleBufferGraphics.rotate(int, int) are very useful methods!
- //dbg.setColor(Color.WHITE);
- //dbg.fill(background);
- // for < num patches
- // how much if any does passing a reference to the graphics object slow down the rendering?
- //draw the target
- Rectangle2D.Double rect1 = new Rectangle2D.Double(226, 239.5, 25, 3);
- dbg.setColor(Color.BLACK);
- dbg.fill(rect1);
- dbg.draw(rect1);
- Rectangle2D.Double rect2 = new Rectangle2D.Double(237, 228, 3, 25);
- dbg.setColor(Color.BLACK);
- dbg.fill(rect2);
- dbg.draw(rect2);
- // detect a variable which tells which one to omit.. it will be counting
- // from 0 to 11... the first time 0 will be omitted, then 1, and so forth
- //
- currX = 270;
- currY = 40;
- if(omit !=0) {
- drawCircles(dbg);
- }
- dbg.rotate(Math.toRadians(30.0),295, 65 );
- dbg.translate(100.0, 0.0);
- drawCircles(dbg);
- dbg.rotate(Math.toRadians(30.0),295, 65 );
- dbg.translate(100.0, 0.0);
- if(omit !=2) {
- drawCircles(dbg);
- }
- dbg.rotate(Math.toRadians(30.0),295, 65 );
- dbg.translate(100.0, 0.0);
- if(omit !=3) {
- drawCircles(dbg);
- }
- dbg.rotate(Math.toRadians(30.0),295, 65 );
- dbg.translate(100.0, 0.0);
- if(omit !=4) {
- drawCircles(dbg);
- }
- dbg.rotate(Math.toRadians(30.0),295, 65 );
- dbg.translate(100.0, 0.0);
- if(omit !=5) {
- drawCircles(dbg);
- }
- dbg.rotate(Math.toRadians(30.0),295, 65 );
- dbg.translate(100.0, 0.0);
- if(omit !=6) {
- drawCircles(dbg);
- }
- dbg.rotate(Math.toRadians(30.0),295, 65 );
- dbg.translate(100.0, 0.0);
- if(omit !=7) {
- drawCircles(dbg);
- }
- dbg.rotate(Math.toRadians(30.0),295, 65 );
- dbg.translate(100.0, 0.0);
- if(omit !=8) {
- drawCircles(dbg);
- }
- dbg.rotate(Math.toRadians(30.0),295, 65 );
- dbg.translate(100.0, 0.0);
- drawCircles(dbg);
- dbg.rotate(Math.toRadians(30.0),295, 65 );
- dbg.translate(100.0, 0.0);
- drawCircles(dbg);
- dbg.rotate(Math.toRadians(30.0),295, 65 );
- dbg.translate(100.0, 0.0);
- drawCircles(dbg);
- }
- public void drawCircles(Graphics2D dbg) {
- int red=0;
- int green=0;
- int blue=0;
- int currWidth=50;
- double circleX= (double) currX;
- double circleY= (double) currY;
- for (int x=0; x < NUM_CIRCLES; x++) {
- // take background color and if x==0
- // -add 8 to red and blue
- // -subtract 17 from the green
- // else add 10 to red and blue, subtract 20 from green
- if(x == 0){
- red = bg_red + 8;
- green = bg_green - 17;
- blue = bg_blue + 8;
- }
- else {
- red = red + 8;
- green = green - 17;
- blue = blue + 8;
- }
- Ellipse2D.Double ellipse = new Ellipse2D.Double(circleX, circleY, currWidth, currWidth);
- dbg.setColor(new Color(red, green, blue));
- dbg.fill(ellipse);
- dbg.draw(ellipse);
- // couldn't the circles to end be centered properly without using decimals when currWidth-5,
- // but 6 seems to work fine without them
- currWidth = currWidth-6;
- circleX = circleX + 3;
- circleY = circleY + 3;
- }
- }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.drawImage(doubleBufferImage, 0, 0, null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement