Advertisement
Shemamforash

Spiro Update

Oct 21st, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. package spiro;
  2.  
  3. import java.awt.*;
  4. import java.awt.image.BufferedImage;
  5. import javax.swing.*;
  6. import java.util.ArrayList;
  7.  
  8. public static class TheDragon extends JInternalFrame
  9. {
  10.     private static int paintIterator = 100, frame_Width, frame_Height; //Keep track of the number of pixels you have drawn
  11.     private static float last_Time = 0f, delta_Time = 0f, pixels_Per_Sec = 500;
  12.     private static SpiroObject current_Spiro; //Array to contain all spiros in case we draw multiple objects
  13.     private static Graphics buffer;
  14.     private static BufferedImage offScreen;
  15.    
  16.     public TheDragon(int width, int height, BufferedImage buffered_Img, Graphics b) //First method called
  17.     {
  18.         frame_Width = width;
  19.         frame_Height = height;
  20.         offScreen = buffered_Img; //Some arbitrary screen size TODO: change this to screen dimensions
  21.         buffer = b; //Assign the buffer to get the graphics from off screen
  22.     }
  23.    
  24.     private Color getColour(float iterator)
  25.     {
  26.         float current_Pos = (1f / (float)current_Spiro.x_Vals.size()) * iterator;
  27.        
  28.         Color a = null, b = null;
  29.        
  30.         for(int i = 0; i < current_Spiro.spiro_Colours.size(); ++i)
  31.         {
  32.             if(current_Pos * (float)current_Spiro.spiro_Colours.size() <= i)
  33.             {
  34.                 a = current_Spiro.spiro_Colours.get(i - 1);
  35.                 b = current_Spiro.spiro_Colours.get(i);
  36.                 float percentage_Colour_A = (1f / ((float)current_Spiro.x_Vals.size() / current_Spiro.spiro_Colours.size()) * i - 1) * iterator;
  37.                 float percentage_Colour_B = (1f / ((float)current_Spiro.x_Vals.size() / current_Spiro.spiro_Colours.size()) * i) * iterator;
  38.                 current_Pos = (1f / (current_Pos - (float)current_Spiro.x_Vals.size() / current_Spiro.spiro_Colours.size()) * i) * iterator;
  39.                 break;
  40.             }
  41.         }
  42.        
  43.         Color current_Hue = null;
  44.        
  45.         if(a != null && b != null)
  46.         {
  47.             float temp_R = ((((1f / 255) * a.getRed()) - ((1f / 255) * b.getRed())) * current_Pos) + ((1f / 255f) * b.getRed());
  48.             float temp_G = ((((1f / 255) * a.getGreen()) - ((1f / 255) * b.getGreen())) * current_Pos) + ((1f / 255f) * b.getGreen());
  49.             float temp_B = ((((1f / 255) * a.getBlue()) - ((1f / 255) * b.getBlue())) * current_Pos) + ((1f / 255f) * b.getBlue());
  50.             current_Hue = new Color(temp_R, temp_G, temp_B, 1f);
  51.         }
  52.        
  53.         if(current_Hue == null)
  54.         {
  55.             return Color.white;
  56.         }
  57.         else
  58.         {
  59.             return current_Hue;
  60.         }
  61.     }
  62.    
  63.     public BufferedImage getImage()
  64.     {
  65.         buffer.clearRect(0, 0, frame_Width, frame_Height);
  66.        
  67.         if(last_Time != 0f)
  68.         {
  69.             delta_Time = System.nanoTime() - last_Time;
  70.         }
  71.        
  72.         last_Time = System.nanoTime();
  73.         int pixels_To_Draw = (int)(pixels_Per_Sec * delta_Time);
  74.         paintIterator += pixels_To_Draw;
  75.         if (current_Spiro.x_Vals.size() <= paintIterator) //If the paint iterator hasn't exceeded the number of pixels the shape actually contains
  76.         {
  77.             for (int j = 0; j < paintIterator; ++j) //For all positions up to the paint iterator
  78.             {
  79.                 int x_Pos = current_Spiro.x_Vals.get(j); //Get the x and y values of the current pixel coordinate
  80.                 int y_Pos = current_Spiro.y_Vals.get(j);
  81.                 buffer.setColor(getColour((float)x_Pos));
  82.                 buffer.drawLine(x_Pos, y_Pos, x_Pos+1, y_Pos+1); //Draw the pixel
  83.             }
  84.         }
  85.        
  86.         return offScreen; //Draw the image
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement