home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / awt / win32 / Win32Graphics.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  10.1 KB  |  342 lines

  1. /*-
  2.  * Copyright (c) 1994 by Sun Microsystems, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * @(#)Win32Graphics.java    1.20 96/03/31 11/14/94
  6.  *
  7.  *      Sami Shaio, 11/14/94
  8.  */
  9. package sun.awt.win32;
  10.  
  11. import java.awt.*;
  12. import java.io.*;
  13. import java.util.*;
  14. import java.awt.image.ImageObserver;
  15. import sun.awt.image.OffScreenImageSource;
  16. import sun.awt.image.ImageRepresentation;
  17. import com.ms.awt.GraphicsX;
  18.  
  19. /**
  20.  * Win32Graphics is an object that encapsulates a graphics context for a
  21.  * particular canvas. 
  22.  * 
  23.  * @version 1.20 03/31/96
  24.  * @author Sami Shaio
  25.  */
  26.  
  27. /*
  28.  * bugbug: DavidMS/IanEl need to clean up Win32Graphics vs GraphicsX as 
  29.  * part of the de-MFCizing of AWT.  (There is not much point is doing
  30.  * much until Ian gets back).
  31.  */
  32. public class Win32Graphics extends Graphics {
  33.     int        pData;
  34.     Color    foreground;
  35.     Font    font;
  36.     int        originX;    // REMIND: change to float
  37.     int        originY;    // REMIND: change to float
  38.  
  39.     private native int pGetDC();
  40.     private native void createFromComponent(MComponentPeer w);
  41.     private native void createFromGraphics(Win32Graphics g);
  42.     private native void createFromPrintJob(Win32PrintJob pj);
  43.     private native void imageCreate(ImageRepresentation ir);
  44.     private native void pSetFont(Font f);
  45.     private native void pSetForeground(Color c);
  46.  
  47.     private sun.awt.image.Image        image;
  48.  
  49.     // bugbug: davidms/ianel - made this public
  50.     public Win32Graphics(Win32PrintJob pj) {
  51.     createFromPrintJob(pj);
  52.     }
  53.     // bugbug: davidms/ianel - made this public
  54.     public Win32Graphics(Win32Graphics g) {
  55.     createFromGraphics(g);
  56.     }
  57.     // bugbug: davidms/ianel - made this public
  58.     public Win32Graphics(MComponentPeer comp) {
  59.     createFromComponent(comp);
  60.     }
  61.  
  62.     public Win32Graphics(Win32Image image) {
  63.     OffScreenImageSource osis = (OffScreenImageSource)image.getSource();
  64.     imageCreate(osis.getImageRep());
  65.     this.image = image;
  66.     }
  67.  
  68.     public int GetDC()
  69.     {
  70.         return pGetDC();
  71.     }
  72.     
  73.     /**
  74.      * Create a new Win32Graphics Object based on this one.
  75.      */
  76.     public Graphics create() 
  77.     {
  78.         Win32Graphics g ;
  79.     
  80.         if( this instanceof com.ms.awt.GraphicsX )
  81.             g = (Win32Graphics)new GraphicsX((GraphicsX)this);
  82.         else
  83.         {
  84. //            System.out.println("bugbug: Win32Graphics::create()-> creating a new Win32Graphics");
  85.             g = new Win32Graphics(this);
  86.         }    
  87.         g.foreground = foreground;
  88.         g.font = font;
  89.         g.originX = originX;
  90.         g.originY = originY;
  91.         g.image = image;
  92.         return g;
  93.     }
  94.  
  95.     /**
  96.      * Translate
  97.      */
  98.     public void translate(int x, int y) {
  99.     originX += x;
  100.     originY += y;
  101.     }
  102.  
  103.     /**
  104.      * Disposes of this Win32Graphics context. It cannot be used after being
  105.      * disposed.
  106.      */
  107.     public native void dispose();
  108.  
  109.     public void setFont(Font font) {
  110.     if ((font != null) && (this.font != font)) {
  111.         this.font = font;
  112.         pSetFont(font);
  113.     }
  114.     }
  115.     public Font getFont() {
  116.     return font;
  117.     }
  118.  
  119.     /**
  120.      * Gets font metrics for the given font.
  121.      */
  122.     public FontMetrics getFontMetrics(Font font) {
  123.     return Win32FontMetrics.getFontMetrics(font);
  124.     }
  125.  
  126.     /**
  127.      * Sets the foreground color.
  128.      */
  129.     public void setColor(Color c) {
  130.     if ((c != null) && (c != foreground)) {
  131.         foreground = c;
  132.         pSetForeground(c);
  133.     }
  134.     }
  135.     public Color getColor() {
  136.     return foreground;
  137.     }
  138.  
  139.     /**
  140.      * Sets the paint mode to overwrite the destination with the
  141.      * current color. This is the default paint mode.
  142.      */
  143.     public native void setPaintMode();
  144.  
  145.     /**
  146.      * Sets the paint mode to alternate between the current color
  147.      * and the given color.
  148.      */
  149.     public native void setXORMode(Color c1);
  150.  
  151.     /** 
  152.      * Gets the current clipping area 
  153.      */
  154.     public Rectangle getClipRect() {
  155.     Rectangle clip = new Rectangle();
  156.     getClipRect(clip);
  157.     return clip;
  158.     }
  159.     native void getClipRect(Rectangle clip);
  160.  
  161.     /** Sets the clipping rectangle for this Win32Graphics context. */
  162.     public native void clipRect(int X, int Y, int W, int H);
  163.     
  164.     /** Clears the rectangle indicated by x,y,w,h. */
  165.     public native void clearRect(int x, int y, int w, int h);
  166.  
  167.     /** Fills the given rectangle with the foreground color. */
  168.     public native void fillRect(int X, int Y, int W, int H);
  169.  
  170.     /** Draws the given rectangle. */
  171.     public native void drawRect(int X, int Y, int W, int H);
  172.  
  173.  
  174.     /** Draws the given string. */
  175.     public native void drawString(String str, int x, int y);
  176.  
  177.     /** Draws the given character array. */
  178.     public native void drawChars(char data[], int offset, int length, int x, int y);
  179.  
  180.     /** Draws the given byte array. */
  181.     public native void drawBytes(byte data[], int offset, int length, int x, int y);
  182.  
  183.     /** Draws the given string and returns the length of the drawn
  184.       string in pixels.  If font is not set then returns -1. */
  185.     public native int drawStringWidth(String str, int x, int y);
  186.  
  187.     /** Draws the given character array and return the width in
  188.       pixels. If font is not set then returns -1. */
  189.     public native int drawCharsWidth(char data[], int offset, int length, int x, int y);
  190.  
  191.     /** Draws the given character array and return the width in
  192.       pixels. If font is not set then returns -1. */
  193.     public native int drawBytesWidth(byte data[], int offset, int length, int x, int y);
  194.  
  195.     /** Draws the given line. */
  196.     public native void drawLine(int x1, int y1, int x2, int y2);
  197.  
  198.     /**
  199.      * Draws an image at x,y in nonblocking mode with a callback object.
  200.      */
  201.     public boolean drawImage(Image img, int x, int y, ImageObserver observer) {
  202.     Win32Image win32img = (Win32Image) img;
  203.     if (win32img.hasError()) {
  204.         if (observer != null) {
  205.         observer.imageUpdate(img,
  206.                      ImageObserver.ERROR|ImageObserver.ABORT,
  207.                      -1, -1, -1, -1);
  208.         }
  209.         return false;
  210.     }
  211.     ImageRepresentation ir = win32img.getImageRep(-1, -1);
  212.     return ir.drawImage(this, x, y, null, observer);
  213.     }
  214.  
  215.     /**
  216.      * Draws an image scaled to x,y,w,h in nonblocking mode with a
  217.      * callback object.
  218.      */
  219.     public boolean drawImage(Image img, int x, int y, int width, int height,
  220.                  ImageObserver observer) {
  221.     if (width == 0 || height == 0) {
  222.         return true;
  223.     }
  224.     Win32Image win32img = (Win32Image) img;
  225.     if (win32img.hasError()) {
  226.         if (observer != null) {
  227.         observer.imageUpdate(img,
  228.                      ImageObserver.ERROR|ImageObserver.ABORT,
  229.                      -1, -1, -1, -1);
  230.         }
  231.         return false;
  232.     }
  233.     if (width < 0) width = -1;
  234.     if (height < 0) height = -1;
  235.     ImageRepresentation ir = win32img.getImageRep(width, height);
  236.     return ir.drawImage(this, x, y, null, observer);
  237.     }
  238.  
  239.     /**
  240.      * Draws an image at x,y in nonblocking mode with a solid background
  241.      * color and a callback object.
  242.      */
  243.     public boolean drawImage(Image img, int x, int y, Color bg,
  244.                  ImageObserver observer) {
  245.     Win32Image win32img = (Win32Image) img;
  246.     if (win32img.hasError()) {
  247.         if (observer != null) {
  248.         observer.imageUpdate(img,
  249.                      ImageObserver.ERROR|ImageObserver.ABORT,
  250.                      -1, -1, -1, -1);
  251.         }
  252.         return false;
  253.     }
  254.     ImageRepresentation ir = win32img.getImageRep(-1, -1);
  255.     return ir.drawImage(this, x, y, bg, observer);
  256.     }
  257.  
  258.     /**
  259.      * Draws an image scaled to x,y,w,h in nonblocking mode with a
  260.      * solid background color and a callback object.
  261.      */
  262.     public boolean drawImage(Image img, int x, int y, int width, int height,
  263.                  Color bg, ImageObserver observer) {
  264.     if (width == 0 || height == 0) {
  265.         return true;
  266.     }
  267.     Win32Image win32img = (Win32Image) img;
  268.     if (win32img.hasError()) {
  269.         if (observer != null) {
  270.         observer.imageUpdate(img,
  271.                      ImageObserver.ERROR|ImageObserver.ABORT,
  272.                      -1, -1, -1, -1);
  273.         }
  274.         return false;
  275.     }
  276.     if (width < 0) width = -1;
  277.     if (height < 0) height = -1;
  278.     ImageRepresentation ir = win32img.getImageRep(width, height);
  279.     return ir.drawImage(this, x, y, bg, observer);
  280.     }
  281.  
  282.     /**
  283.      * Copies an area of the canvas that this graphics context paints to.
  284.      * @param X the x-coordinate of the source.
  285.      * @param Y the y-coordinate of the source.
  286.      * @param W the width.
  287.      * @param H the height.
  288.      * @param dx the x-coordinate of the destination.
  289.      * @param dy the y-coordinate of the destination.
  290.      */
  291.     public native void copyArea(int X, int Y, int W, int H, int dx, int dy);
  292.  
  293.  
  294.     /** Draws a rounded rectangle. */
  295.     public native void drawRoundRect(int x, int y, int w, int h,
  296.                      int arcWidth, int arcHeight);
  297.  
  298.     /** Draws a filled rounded rectangle. */
  299.     public native void fillRoundRect(int x, int y, int w, int h,
  300.                      int arcWidth, int arcHeight);
  301.  
  302.     /** Draws a polygon defined by an array of x points and y points */
  303.     public native void drawPolygon(int xPoints[], int yPoints[], int nPoints);
  304.  
  305.     /** Fills a polygon with the current fill mask */
  306.     public native void fillPolygon(int xPoints[], int yPoints[], int nPoints);
  307.  
  308.     /** Draws an oval to fit in the given rectangle */
  309.     public native void drawOval(int x, int y, int w, int h);
  310.  
  311.     /** Fills an oval to fit in the given rectangle */
  312.     public native void fillOval(int x, int y, int w, int h);
  313.  
  314.     /**
  315.      * Draws an arc bounded by the given rectangle from startAngle to
  316.      * endAngle. 0 degrees is a vertical line straight up from the
  317.      * center of the rectangle. Positive angles indicate clockwise
  318.      * rotations, negative angle are counter-clockwise.
  319.      */
  320.     public native void drawArc(int x, int y, int w, int h,
  321.                    int startAngle,
  322.                    int endAngle);
  323.  
  324.     /** fills an arc. arguments are the same as drawArc. */
  325.     public native void fillArc(int x, int y, int w, int h,
  326.                    int startAngle,
  327.                    int endAngle);
  328.  
  329.     public String toString() {    
  330.     return getClass().getName() + "[" + originX + "," + originY + "]";
  331.     }
  332.  
  333.     /* Outline the given region. */
  334.     //public native void drawRegion(Region r);
  335.  
  336.     /* Fill the given region. */
  337.     //public native void fillRegion(Region r);
  338.  
  339.     /** Terminates a PrintJob and releases resources */
  340.     public native void close(Win32PrintJob pj);
  341. }
  342.