home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / JAVA_UTL / HYPERPRO / SRC / PVS / HYPERBOL / GRAPHICS.JAV < prev    next >
Encoding:
Text File  |  1996-09-14  |  5.7 KB  |  196 lines

  1. package PVS.Hyperbolic;
  2. //
  3. // Copyright (C) 1996 by Vladimir Bulatov <V.Bulatov@ic.ac.uk>.  
  4. //        All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions
  8. // are met:
  9. // 1. Redistributions of source code must retain the above copyright
  10. //    notice, this list of conditions and the following disclaimer.
  11. // 2. Redistributions in binary form must reproduce the above copyright
  12. //    notice, this list of conditions and the following disclaimer in the
  13. //    documentation and/or other materials provided with the distribution.
  14. //
  15. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. // ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. // SUCH DAMAGE.
  26.  
  27. import java.awt.*;
  28. import java.awt.image.*;
  29.  
  30. public class Graphics2d extends Object {
  31.  
  32.   Graphics g;
  33.   double scalex=1,scaley=1,x0=0,y0=0;
  34.  
  35.   // Constructor.
  36.   public Graphics2d() {
  37.   }
  38.  
  39.   public Graphics2d(Graphics g) {
  40.     this.g = g;
  41.   }
  42.  
  43.   public void setGraphics(Graphics g){
  44.     this.g = g;
  45.   }
  46.  
  47.   public Graphics getGraphics(){
  48.     return g;
  49.   }
  50.  
  51.   public void setScale(double scale){
  52.     this.scalex = scale;
  53.     this.scaley = scale;
  54.   }
  55.  
  56.   public void setOrigin(double x0, double y0){
  57.     this.x0 = x0;
  58.     this.y0 = y0;
  59.   }
  60.  
  61.   public int x2screen(double x){
  62.     return (int)(scalex*(x+x0));
  63.   }
  64.  
  65.   public int y2screen(double x){
  66.     return (int)(scaley*(x+y0));
  67.   }
  68.  
  69.   static double rad2grad = 180./Math.PI;
  70.  
  71.   public void drawCyrcle(double x, double y, double radius){
  72.     g.drawOval(x2screen(x-radius),y2screen(y-radius),
  73.            x2screen(2*radius),y2screen(2*radius));
  74.   }
  75.  
  76.   public void fillCyrcle(double x, double y, double radius){
  77.     g.fillOval(x2screen(x-radius),y2screen(y-radius),
  78.            x2screen(2*radius),y2screen(2*radius));
  79.   }
  80.  
  81.   private int segmentsInArc(double radius, double angle){
  82.     // number of grad in segment
  83.     if(radius == 0.0)
  84.       return 1;
  85.     int n1 = (int)Math.ceil(Math.abs(angle*rad2grad/5));
  86.     //number of 5 pixel segments in cyrcle
  87.     int n2 = Math.abs(x2screen(radius))+1;
  88.     return  Math.min(n1,n2);
  89.   }
  90.  
  91.   /*
  92.   static double sine[] = new double[360];
  93.   static double cosine[] = new double[360];
  94.   static {
  95.     for(int i=0;i<360;i++){
  96.       sine[i] = Math.sin(i/rad2grad);
  97.       cosine[i] = Math.cos(i/rad2grad);
  98.     }
  99.   }
  100.   */
  101.   public void drawArc(double x, double y, double radius,
  102.               double start, double angle){
  103.     
  104.     int n = segmentsInArc(radius,angle);
  105.     int x0,y0,x1,y1;
  106.     x0 = x2screen(x+radius*Math.cos(start));
  107.     y0 = y2screen(y+radius*Math.sin(start)); 
  108.     for(int i=1;i <= n;i++){
  109.       double fi = start + i*angle/n;
  110.       x1 = x2screen(x+radius*Math.cos(fi));
  111.       y1 = y2screen(y+radius*Math.sin(fi));
  112.       g.drawLine(x0,y0, x1,y1);
  113.       x0 = x1; y0 = y1;
  114.     }
  115.   }
  116.  
  117.   public void drawArc(Arc arc){
  118.     if(arc.r == 0.0){
  119.       drawLine(arc.x,arc.y,arc.start,arc.angle);
  120.       return;
  121.     } else {
  122.       drawArc(arc.x, arc.y, arc.r, arc.start, arc.angle);
  123.     }
  124.   }
  125.  
  126.   public void fillArcs(Arc[] arc){
  127.     int nvert = 0; // number of verteces
  128.     for(int i=0; i < arc.length;i++){
  129.       Arc a = arc[i];
  130.       nvert += segmentsInArc(a.r, a.angle);
  131.     }      
  132.     int[] x = new int[nvert];
  133.     int[] y = new int[nvert];
  134.     int n = 0;
  135.     for(int i=0; i < arc.length;i++){
  136.       Arc a = arc[i];
  137.       int nv = segmentsInArc(a.r, a.angle);
  138.       if(a.r == 0.0){  // strait line
  139.     x[n] = x2screen(a.x);
  140.     y[n] = y2screen(a.y);    
  141.     n++;
  142.       } else {
  143.     for(int j = 0; j < nv; j++){
  144.       double fi = a.start + j*a.angle/nv;
  145.       x[n] = x2screen(a.x+a.r*Math.cos(fi));
  146.       y[n] = y2screen(a.y+a.r*Math.sin(fi));
  147.       n++;
  148.     }
  149.       }
  150.     } 
  151.     g.fillPolygon(x,y,nvert);
  152.     g.drawPolygon(x,y,nvert);
  153.   }
  154.  
  155.   public void drawArc1(double x, double y, double radius,
  156.               double start, double angle){
  157.  
  158.     int iangle, istart; 
  159.     if(angle > 0){
  160.       istart = (int)Math.ceil(rad2grad*start);
  161.       iangle = (int)(Math.floor(rad2grad*(angle+start)))-istart;
  162.     } else { // angle <= 0
  163.       istart = (int)Math.floor(rad2grad*start);
  164.       iangle = (int)(Math.ceil(rad2grad*(angle+start)))-istart;
  165.     }
  166.     //drawTinySegment(x,y,radius,start,start+angle);
  167.     g.drawArc(x2screen(x-radius),y2screen(y-radius),
  168.               x2screen(2*radius), y2screen(2*radius), istart,iangle);
  169.     drawTinySegment(x,y,radius,start,istart/rad2grad);
  170.     drawTinySegment(x,y,radius,(istart+iangle)/rad2grad,start+angle);
  171.     //System.out.println("start: "+(int)(rad2grad*minang)+
  172.     //               ", angle: "+(int)(rad2grad*angle));
  173.   }
  174.  
  175.   void drawTinySegment(double x,double y,double radius,
  176.                double start,double end){
  177.     g.drawLine(x2screen(x+radius*Math.cos(start)),
  178.            y2screen(y-radius*Math.sin(start)), 
  179.            x2screen(x+radius*Math.cos(end)),
  180.            y2screen(y-radius*Math.sin(end)));
  181.   }
  182.  
  183.   public void drawLine( double x1, double  y1, double x2, double y2 ) {
  184.     g.drawLine(x2screen(x1),y2screen(y1), x2screen(x2),y2screen(y2));
  185.   }   
  186.  
  187.   public void setColor(Color color){
  188.     g.setColor(color);
  189.   }
  190.   
  191.   public void fillRect(double x, double y, double width, double height){
  192.     g.fillRect(x2screen(x),y2screen(y),x2screen(width),y2screen(height));
  193.   }  
  194. }
  195.  
  196.