Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package spiro;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import javax.swing.*;
- import java.util.ArrayList;
- public static class TheDragon extends JInternalFrame
- {
- private static int paintIterator = 100, frame_Width, frame_Height; //Keep track of the number of pixels you have drawn
- private static float last_Time = 0f, delta_Time = 0f, pixels_Per_Sec = 500;
- private static SpiroObject current_Spiro; //Array to contain all spiros in case we draw multiple objects
- private static Graphics buffer;
- private static BufferedImage offScreen;
- public TheDragon(int width, int height, BufferedImage buffered_Img, Graphics b) //First method called
- {
- frame_Width = width;
- frame_Height = height;
- offScreen = buffered_Img; //Some arbitrary screen size TODO: change this to screen dimensions
- buffer = b; //Assign the buffer to get the graphics from off screen
- }
- private Color getColour(float iterator)
- {
- float current_Pos = (1f / (float)current_Spiro.x_Vals.size()) * iterator;
- Color a = null, b = null;
- for(int i = 0; i < current_Spiro.spiro_Colours.size(); ++i)
- {
- if(current_Pos * (float)current_Spiro.spiro_Colours.size() <= i)
- {
- a = current_Spiro.spiro_Colours.get(i - 1);
- b = current_Spiro.spiro_Colours.get(i);
- float percentage_Colour_A = (1f / ((float)current_Spiro.x_Vals.size() / current_Spiro.spiro_Colours.size()) * i - 1) * iterator;
- float percentage_Colour_B = (1f / ((float)current_Spiro.x_Vals.size() / current_Spiro.spiro_Colours.size()) * i) * iterator;
- current_Pos = (1f / (current_Pos - (float)current_Spiro.x_Vals.size() / current_Spiro.spiro_Colours.size()) * i) * iterator;
- break;
- }
- }
- Color current_Hue = null;
- if(a != null && b != null)
- {
- float temp_R = ((((1f / 255) * a.getRed()) - ((1f / 255) * b.getRed())) * current_Pos) + ((1f / 255f) * b.getRed());
- float temp_G = ((((1f / 255) * a.getGreen()) - ((1f / 255) * b.getGreen())) * current_Pos) + ((1f / 255f) * b.getGreen());
- float temp_B = ((((1f / 255) * a.getBlue()) - ((1f / 255) * b.getBlue())) * current_Pos) + ((1f / 255f) * b.getBlue());
- current_Hue = new Color(temp_R, temp_G, temp_B, 1f);
- }
- if(current_Hue == null)
- {
- return Color.white;
- }
- else
- {
- return current_Hue;
- }
- }
- public BufferedImage getImage()
- {
- buffer.clearRect(0, 0, frame_Width, frame_Height);
- if(last_Time != 0f)
- {
- delta_Time = System.nanoTime() - last_Time;
- }
- last_Time = System.nanoTime();
- int pixels_To_Draw = (int)(pixels_Per_Sec * delta_Time);
- paintIterator += pixels_To_Draw;
- if (current_Spiro.x_Vals.size() <= paintIterator) //If the paint iterator hasn't exceeded the number of pixels the shape actually contains
- {
- for (int j = 0; j < paintIterator; ++j) //For all positions up to the paint iterator
- {
- int x_Pos = current_Spiro.x_Vals.get(j); //Get the x and y values of the current pixel coordinate
- int y_Pos = current_Spiro.y_Vals.get(j);
- buffer.setColor(getColour((float)x_Pos));
- buffer.drawLine(x_Pos, y_Pos, x_Pos+1, y_Pos+1); //Draw the pixel
- }
- }
- return offScreen; //Draw the image
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement