1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.embed.swing;
  27 
  28 import java.awt.Color;
  29 import java.awt.Composite;
  30 import java.awt.Font;
  31 import java.awt.FontMetrics;
  32 import java.awt.Graphics;
  33 import java.awt.Graphics2D;
  34 import java.awt.GraphicsConfiguration;
  35 import java.awt.Image;
  36 import java.awt.Paint;
  37 import java.awt.Polygon;
  38 import java.awt.Rectangle;
  39 import java.awt.RenderingHints;
  40 import java.awt.RenderingHints.Key;
  41 import java.awt.Shape;
  42 import java.awt.Stroke;
  43 import java.awt.font.FontRenderContext;
  44 import java.awt.font.GlyphVector;
  45 import java.awt.geom.AffineTransform;
  46 import java.awt.image.BufferedImage;
  47 import java.awt.image.BufferedImageOp;
  48 import java.awt.image.ImageObserver;
  49 import java.awt.image.RenderedImage;
  50 import java.awt.image.renderable.RenderableImage;
  51 import java.text.AttributedCharacterIterator;
  52 import java.util.Map;
  53 
  54 import sun.awt.ConstrainableGraphics;
  55 
  56 class ProxyGraphics extends Graphics2D implements ConstrainableGraphics {
  57 
  58     private SwingView swingView;
  59 
  60     private Graphics2D proxy;
  61 
  62     ProxyGraphics(SwingView v, Graphics2D p) {
  63         swingView = v;
  64         proxy = p;
  65     }
  66 
  67     @Override
  68     public void draw3DRect(int x, int y, int width, int height, boolean raised) {
  69         proxy.draw3DRect(x, y, width, height, raised);
  70         commit();
  71     }
  72     
  73     @Override
  74     public void drawBytes(byte[] data, int offset, int length, int x, int y) {
  75         proxy.drawBytes(data, offset, length, x, y);
  76         commit();
  77     }
  78     
  79     @Override
  80     public void drawChars(char[] data, int offset, int length, int x, int y) {
  81         proxy.drawChars(data, offset, length, x, y);
  82         commit();
  83     }
  84     
  85     @Override
  86     public Graphics create(int x, int y, int width, int height) {
  87         Graphics2D copy = (Graphics2D) proxy.create(x, y, width, height);
  88         ProxyGraphics graphics = new ProxyGraphics(swingView, copy);
  89         return graphics;
  90     }
  91     
  92     @Override
  93     public void drawPolygon(Polygon p) {
  94         proxy.drawPolygon(p);
  95         commit();
  96     }
  97     
  98     @Override
  99     public void fill3DRect(int x, int y, int width, int height, boolean raised) {
 100         proxy.fill3DRect(x, y, width, height, raised);
 101         commit();
 102     }
 103     
 104     @Override
 105     public void drawRect(int x, int y, int width, int height) {
 106         proxy.drawRect(x, y, width, height);
 107         commit();
 108     }
 109     
 110     @Override
 111     public void fillPolygon(Polygon p) {
 112        proxy.fillPolygon(p);
 113        commit();
 114     }
 115     
 116     @Override
 117     public Rectangle getClipBounds(Rectangle r) {
 118         return proxy.getClipBounds(r);
 119     }
 120     
 121     @SuppressWarnings("deprecation")
 122     @Override
 123     public Rectangle getClipRect() {
 124         return proxy.getClipRect();
 125     }
 126     
 127     @Override
 128     public FontMetrics getFontMetrics() {
 129         return proxy.getFontMetrics();
 130     }
 131     
 132     @Override
 133     public boolean hitClip(int x, int y, int width, int height) {
 134         return proxy.hitClip(x, y, width, height);
 135     }
 136 
 137     @Override
 138     public void draw(Shape s) {
 139         proxy.draw(s);
 140         commit();
 141     }
 142 
 143     @Override
 144     public boolean drawImage(Image img, AffineTransform xform, ImageObserver obs) {
 145         boolean b = proxy.drawImage(img, xform, obs);
 146         commit();
 147         return b;
 148     }
 149 
 150     @Override
 151     public void drawImage(BufferedImage img, BufferedImageOp op, int x, int y) {
 152         proxy.drawImage(img, op, x, y);
 153         commit();
 154     }
 155 
 156     @Override
 157     public void drawRenderedImage(RenderedImage img, AffineTransform xform) {
 158         proxy.drawRenderedImage(img, xform);
 159         commit();
 160     }
 161 
 162     @Override
 163     public void drawRenderableImage(RenderableImage img, AffineTransform xform) {
 164         proxy.drawRenderableImage(img, xform);
 165         commit();
 166     }
 167 
 168     @Override
 169     public void drawString(String str, int x, int y) {
 170         proxy.drawString(str, x, y);
 171         commit();
 172     }
 173 
 174     @Override
 175     public void drawString(String str, float x, float y) {
 176         proxy.drawString(str, x, y);
 177         commit();
 178     }
 179 
 180     @Override
 181     public void drawString(AttributedCharacterIterator iterator, int x, int y) {
 182         proxy.drawString(iterator, x, y);
 183         commit();
 184     }
 185 
 186     @Override
 187     public void drawString(AttributedCharacterIterator iterator, float x,
 188             float y) {
 189         proxy.drawString(iterator, x, y);
 190         commit();
 191     }
 192 
 193     @Override
 194     public void drawGlyphVector(GlyphVector g, float x, float y) {
 195         proxy.drawGlyphVector(g, x, y);
 196         commit();
 197     }
 198 
 199     @Override
 200     public void fill(Shape s) {
 201         proxy.fill(s);
 202         commit();
 203     }
 204 
 205     @Override
 206     public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
 207         return proxy.hit(rect, s, onStroke);
 208     }
 209 
 210     @Override
 211     public GraphicsConfiguration getDeviceConfiguration() {
 212         return proxy.getDeviceConfiguration();
 213     }
 214 
 215     @Override
 216     public void setComposite(Composite comp) {
 217         proxy.setComposite(comp);
 218     }
 219 
 220     @Override
 221     public void setPaint(Paint paint) {
 222         proxy.setPaint(paint);
 223     }
 224 
 225     @Override
 226     public void setStroke(Stroke s) {
 227         proxy.setStroke(s);
 228     }
 229 
 230     @Override
 231     public void setRenderingHint(Key hintKey, Object hintValue) {
 232         proxy.setRenderingHint(hintKey, hintValue);
 233     }
 234 
 235     @Override
 236     public Object getRenderingHint(Key hintKey) {
 237         return proxy.getRenderingHint(hintKey);
 238     }
 239 
 240     @Override
 241     public void setRenderingHints(Map<?, ?> hints) {
 242         proxy.setRenderingHints(hints);
 243     }
 244 
 245     @Override
 246     public void addRenderingHints(Map<?, ?> hints) {
 247         proxy.addRenderingHints(hints);
 248     }
 249 
 250     @Override
 251     public RenderingHints getRenderingHints() {
 252         return proxy.getRenderingHints();
 253     }
 254 
 255     @Override
 256     public void translate(int x, int y) {
 257         proxy.translate(x, y);
 258     }
 259 
 260     @Override
 261     public void translate(double tx, double ty) {
 262         proxy.translate(tx, ty);
 263     }
 264 
 265     @Override
 266     public void rotate(double theta) {
 267         proxy.rotate(theta);
 268     }
 269 
 270     @Override
 271     public void rotate(double theta, double x, double y) {
 272         proxy.rotate(theta, x, y);
 273     }
 274 
 275     @Override
 276     public void scale(double sx, double sy) {
 277         proxy.scale(sx, sy);
 278     }
 279 
 280     @Override
 281     public void shear(double shx, double shy) {
 282         proxy.shear(shx, shy);
 283     }
 284 
 285     @Override
 286     public void transform(AffineTransform Tx) {
 287         proxy.transform(Tx);
 288     }
 289 
 290     @Override
 291     public void setTransform(AffineTransform Tx) {
 292         proxy.setTransform(Tx);
 293     }
 294 
 295     @Override
 296     public AffineTransform getTransform() {
 297         return proxy.getTransform();
 298     }
 299 
 300     @Override
 301     public Paint getPaint() {
 302         return proxy.getPaint();
 303     }
 304 
 305     @Override
 306     public Composite getComposite() {
 307         return proxy.getComposite();
 308     }
 309 
 310     @Override
 311     public void setBackground(Color color) {
 312         proxy.setBackground(color);
 313     }
 314 
 315     @Override
 316     public Color getBackground() {
 317         return proxy.getBackground();
 318     }
 319 
 320     @Override
 321     public Stroke getStroke() {
 322         return proxy.getStroke();
 323     }
 324 
 325     @Override
 326     public void clip(Shape s) {
 327         proxy.clip(s);
 328     }
 329 
 330     @Override
 331     public FontRenderContext getFontRenderContext() {
 332         return proxy.getFontRenderContext();
 333     }
 334 
 335     @Override
 336     public Graphics create() {
 337         Graphics2D copy = (Graphics2D) proxy.create();
 338         ProxyGraphics graphics = new ProxyGraphics(swingView, copy);
 339         return graphics;
 340     }
 341 
 342     @Override
 343     public Color getColor() {
 344         return proxy.getColor();
 345     }
 346 
 347     @Override
 348     public void setColor(Color c) {
 349         proxy.setColor(c);
 350     }
 351 
 352     @Override
 353     public void setPaintMode() {
 354         proxy.setPaintMode();
 355     }
 356 
 357     @Override
 358     public void setXORMode(Color c1) {
 359         proxy.setXORMode(c1);
 360     }
 361 
 362     @Override
 363     public Font getFont() {
 364         return proxy.getFont();
 365     }
 366 
 367     @Override
 368     public void setFont(Font font) {
 369         proxy.setFont(font);
 370     }
 371 
 372     @Override
 373     public FontMetrics getFontMetrics(Font f) {
 374         return proxy.getFontMetrics(f);
 375     }
 376 
 377     @Override
 378     public Rectangle getClipBounds() {
 379         return proxy.getClipBounds();
 380     }
 381 
 382     @Override
 383     public void clipRect(int x, int y, int width, int height) {
 384         proxy.clipRect(x, y, width, height);
 385     }
 386 
 387     @Override
 388     public void setClip(int x, int y, int width, int height) {
 389         proxy.setClip(x, y, width, height);
 390     }
 391 
 392     @Override
 393     public Shape getClip() {
 394         return proxy.getClip();
 395     }
 396 
 397     @Override
 398     public void setClip(Shape clip) {
 399         proxy.setClip(clip);
 400     }
 401 
 402     @Override
 403     public void copyArea(int x, int y, int width, int height, int dx, int dy) {
 404         proxy.copyArea(x, y, width, height, dx, dy);
 405         commit();
 406     }
 407 
 408     @Override
 409     public void drawLine(int x1, int y1, int x2, int y2) {
 410         proxy.drawLine(x1, y1, x2, y2);
 411         commit();
 412     }
 413 
 414     @Override
 415     public void fillRect(int x, int y, int width, int height) {
 416         proxy.fillRect(x, y, width, height);
 417         commit();
 418     }
 419 
 420     @Override
 421     public void clearRect(int x, int y, int width, int height) {
 422         proxy.clearRect(x, y, width, height);
 423         commit();
 424     }
 425 
 426     @Override
 427     public void drawRoundRect(int x, int y, int width, int height,
 428             int arcWidth, int arcHeight) {
 429         proxy.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
 430         commit();
 431     }
 432 
 433     @Override
 434     public void fillRoundRect(int x, int y, int width, int height,
 435             int arcWidth, int arcHeight) {
 436         proxy.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
 437         commit();
 438     }
 439 
 440     @Override
 441     public void drawOval(int x, int y, int width, int height) {
 442         proxy.drawOval(x, y, width, height);
 443         commit();
 444     }
 445 
 446     @Override
 447     public void fillOval(int x, int y, int width, int height) {
 448         proxy.fillOval(x, y, width, height);
 449         commit();
 450     }
 451 
 452     @Override
 453     public void drawArc(int x, int y, int width, int height, int startAngle,
 454             int arcAngle) {
 455         proxy.drawArc(x, y, width, height, startAngle, arcAngle);
 456         commit();
 457     }
 458 
 459     @Override
 460     public void fillArc(int x, int y, int width, int height, int startAngle,
 461             int arcAngle) {
 462         proxy.fillArc(x, y, width, height, startAngle, arcAngle);
 463         commit();
 464     }
 465 
 466     @Override
 467     public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) {
 468         proxy.drawPolyline(xPoints, yPoints, nPoints);
 469         commit();
 470     }
 471 
 472     @Override
 473     public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) {
 474         proxy.drawPolygon(xPoints, yPoints, nPoints);
 475         commit();
 476     }
 477 
 478     @Override
 479     public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) {
 480         proxy.fillPolygon(xPoints, yPoints, nPoints);
 481         commit();
 482     }
 483 
 484     @Override
 485     public boolean drawImage(Image img, int x, int y, ImageObserver observer) {
 486         boolean b = proxy.drawImage(img, x, y, observer);
 487         commit();
 488         return b;
 489     }
 490 
 491     @Override
 492     public boolean drawImage(Image img, int x, int y, int width, int height,
 493             ImageObserver observer) {
 494         boolean b = proxy.drawImage(img, x, y, width, height, observer);
 495         commit();
 496         return b;
 497     }
 498 
 499     @Override
 500     public boolean drawImage(Image img, int x, int y, Color bgcolor,
 501             ImageObserver observer) {
 502         boolean b = proxy.drawImage(img, x, y, bgcolor, observer);
 503         commit();
 504         return b;
 505     }
 506 
 507     @Override
 508     public boolean drawImage(Image img, int x, int y, int width, int height,
 509             Color bgcolor, ImageObserver observer) {
 510         boolean b = proxy.drawImage(img, x, y, width, height, bgcolor, observer);
 511         commit();
 512         return b;
 513     }
 514 
 515     @Override
 516     public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2,
 517             int sx1, int sy1, int sx2, int sy2, ImageObserver observer) {
 518         boolean b = proxy.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer);
 519         commit();
 520         return b;
 521     }
 522 
 523     @Override
 524     public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2,
 525             int sx1, int sy1, int sx2, int sy2, Color bgcolor,
 526             ImageObserver observer) {
 527         boolean b = proxy.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, bgcolor, observer);
 528         commit();
 529         return b;
 530     }
 531 
 532     @Override
 533     public void dispose() {
 534         proxy.dispose();
 535         commit();
 536     }
 537 
 538     private void commit() {
 539         swingView.commit();
 540     }
 541 
 542     @Override
 543     public void constrain(int x, int y, int w, int h) {
 544         if (proxy instanceof ConstrainableGraphics) {
 545             ((ConstrainableGraphics) proxy).constrain(x, y, w, h);
 546         } else {
 547             throw new UnsupportedOperationException();
 548         }
 549     }
 550 }