Advertisement
Guest User

Trishia's code

a guest
Apr 14th, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. public abstract class UniversalJPanel extends JPanel{
  2.  
  3. protected BufferedImage image;
  4. protected Graphics2D g2d;
  5.  
  6. protected int iterations = 100; // max number of iterations
  7. protected double realMin = -2.0; // default min real
  8. protected double realMax = 2.0;// default max real
  9. protected double imaginaryMin = -1.6; // default min imaginary
  10. protected double imaginaryMax = 1.6;// default max imaginary
  11. protected int panelHeight;
  12. protected int panelWidth;
  13. protected Point pressed, released; // points pressed and released - used to calculate drawn rectangle
  14. protected boolean dragged; // if is dragged - draw rectangle
  15. protected int recWidth, recHeight,xStart, yStart; // variables to calculate rectangle
  16. protected FractalWorker[] arrayOfWorkers; // array of Swing workers
  17.  
  18.  
  19.  
  20. public abstract int calculateIterations(Complex c);
  21. public abstract double getDistance(Complex a, Complex b);
  22.  
  23. public void paintComponent(Graphics g){
  24. super.paintComponent(g);
  25. panelHeight = getHeight();
  26. panelWidth = getWidth();
  27. image =new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); // creating new Bufered image
  28. g2d= (Graphics2D) image.getGraphics();
  29.  
  30.  
  31.  
  32. arrayOfWorkers= new FractalWorker[getHeight()]; // create new worker for each row and execute them
  33. for(int q = 0; q < getHeight(); q++ ){
  34. arrayOfWorkers[q] = new FractalWorker(q);
  35. arrayOfWorkers[q].execute();
  36. }
  37.  
  38. g.drawImage(image, 0, 0, image.getWidth(), image.getWidth(), null); // draw an image
  39.  
  40. }
  41.  
  42. // *** getters, setters, different code//
  43.  
  44. private class FractalWorker extends SwingWorker<Object, Object>{
  45. private int y; // row on which worker should work now
  46. private Color[] arrayOfColors; // array of colors produced by workers
  47. public FractalWorker( int z){
  48. y = z;
  49. }
  50. protected Object doInBackground() throws Exception {
  51.  
  52. arrayOfColors = new Color[getWidth()];
  53.  
  54. for(int q=0; q<getWidth(); q++){ // calculate and insert into array proper color for given pixel
  55. int iter = calculateIterations(setComplexNumber(new Point(q,y)));
  56. if(iter == iterations){
  57. arrayOfColors[q] = Color.black;
  58. }else{
  59. arrayOfColors[q] = Color.getHSBColor((float)((iter/ 20.0)), 1.0f, 1.0f );
  60. }
  61. }
  62. return null;
  63. }
  64. protected void done(){ // take color from the array and draw pixel
  65. for(int i = 0; i<arrayOfColors.length; i++){
  66. g2d.setColor(arrayOfColors[i]);
  67. g2d.drawLine(i, y, i, y);
  68. }
  69. }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement