/* * @(#)Gradient.java 1.2 98/06/29 * * Copyright 1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */ package demos.Paint; import java.awt.*; import java.awt.font.*; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.awt.event.WindowAdapter; import java.awt.geom.Rectangle2D; import J2DCanvas; public class Gradient extends J2DCanvas { private Font font = new Font("Dialog", Font.PLAIN, 10); public Gradient() { setBackground(Color.white); } public void drawDemo(Graphics2D g2) { Rectangle2D rect1 = new Rectangle2D.Float(0.0f, 0.0f, w/2, h/2); GradientPaint gp = new GradientPaint(0,0,Color.blue,w*.35f,h*.35f,Color.green); g2.setPaint(gp); g2.fill(rect1); rect1 = new Rectangle2D.Float(w/2, 0f, w/2, h/2); gp = new GradientPaint(w,0,Color.blue,w*.65f,h*.35f,Color.green); g2.setPaint(gp); g2.fill(rect1); rect1 = new Rectangle2D.Float(0f, h/2, w/2, h/2); gp = new GradientPaint(0,h,Color.blue,w*.35f,h*.65f,Color.green); g2.setPaint(gp); g2.fill(rect1); rect1 = new Rectangle2D.Float(w/2, h/2, w/2, h/2); gp = new GradientPaint(w,h,Color.blue,w*.65f,h*.65f,Color.green); g2.setPaint(gp); g2.fill(rect1); g2.setColor(Color.black); TextLayout tl = new TextLayout("GradientPaint", font, g2.getFontRenderContext()); tl.draw(g2, (int) (w/2-tl.getBounds().getWidth()/2), (int) (h/2+tl.getBounds().getHeight()/2)); } public static void main(String s[]) { WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }; Frame f = new Frame("Java 2D Demo - gradient"); f.addWindowListener(l); f.add("Center", new Gradient()); f.pack(); f.setSize(new Dimension(400,300)); f.show(); } }