home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch07 / MandelRect.java < prev   
Encoding:
Java Source  |  1998-12-14  |  2.4 KB  |  135 lines

  1. import java.awt.*;
  2.  
  3. /*
  4.  * a helper class to manage the zoom rectangle
  5.  */
  6. public class MandelRect {
  7.  
  8. /*
  9.  * the coordinates of the zoom rectangle
  10.  * in screen space
  11.  */
  12. int x;
  13. int y;
  14. int width;
  15. int height;
  16.  
  17. /*
  18.  * the final image width and height
  19.  */
  20. int imgWidth;
  21. int imgHeight;
  22.  
  23. /*
  24.  * the coordinates of the zoom rectangle
  25.  * in set space
  26.  */
  27. double mandelX;
  28. double mandelY;
  29. double mandelWidth;
  30. double mandelHeight;
  31.  
  32. /*
  33.  * set to true if the zoom rectangle should be painted
  34.  */
  35. boolean paintRect;
  36.  
  37. /**
  38.  * constructor initializes variables
  39.  * @param iW - final image width
  40.  * @param iH - final image height
  41.  */
  42. public MandelRect (int iW, int iH) {
  43.  
  44.     imgWidth = iW;
  45.     imgHeight = iH;
  46.     paintRect = false;
  47.  
  48.     mandelX = -1.75;
  49.     mandelY = -1.125;
  50.     mandelWidth = 2.25;
  51.     mandelHeight = 2.25; 
  52. }
  53.  
  54. /**
  55.  * set the top left of the zoom rectangle in screen space
  56.  * @param ix, iy - top-left corner of zoom rectangle
  57.  */
  58. void setXY (int ix, int iy) {
  59.  
  60.     x = ix;
  61.     y = iy;
  62. }
  63.  
  64. /**
  65.  * set the width, height of the zoom rectangle in screen space
  66.  * @param ix, iy - bottom-right corner of zoom rectangle
  67.  */
  68. void setWidthHeight (int ix, int iy) {
  69.  
  70.     width = ix - x;
  71.     height = iy - y; 
  72. }
  73.  
  74. /*
  75.  * translate screen coordinates to set coordinates
  76.  */
  77. void scaleSet () {
  78.  
  79.     int tx, ty, tw, th;
  80.  
  81.     tx = x;
  82.     ty = y;
  83.     tw = width;
  84.     th = height;
  85.     if (tw < 0) {
  86.         tw = -width;
  87.         tx = x-tw;
  88.     }
  89.     if (th < 0) {
  90.         th = -height;
  91.         ty = y-th;
  92.     }
  93.     mandelX = mandelX + (mandelWidth) * ((double) tx)/((double) imgWidth);
  94.     mandelY = mandelY + (mandelHeight) * ((double) ty)/((double) imgHeight);
  95.     mandelWidth = mandelWidth * ((double) tw)/((double)imgWidth);
  96.     mandelHeight = mandelHeight * ((double) th)/((double)imgHeight);
  97. }
  98.  
  99. /**
  100.  * set the paintRect flag
  101.  * @param p - true means zoom rectangle should be painted
  102.  */
  103. void setPaintRect (boolean p) {
  104.  
  105.     paintRect = p;
  106. }
  107.  
  108. /**
  109.  * paint the zoom rectangle if necessary
  110.  * @param g - destination graphics object
  111.  */
  112. void paint (Graphics g) {
  113.  
  114.     if (paintRect == false) return;
  115.  
  116.     int tx, ty, tw, th;
  117.  
  118.     tx = x;
  119.     ty = y;
  120.     tw = width;
  121.     th = height;
  122.     if (tw < 0) {
  123.         tw = -width;
  124.         tx = x-tw;
  125.     }
  126.     if (th < 0) {
  127.         th = -height;
  128.         ty = y-th;
  129.     }
  130.     g.setColor (Color.white);
  131.     g.drawRect (tx, ty, tw, th);
  132. }
  133. }
  134.  
  135.