< prev index next >

src/demo/share/jfc/J2Ddemo/java2d/demos/Paint/Gradient.java

Print this page


   1 /*
   2  *
   3  * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  *
   9  *   - Redistributions of source code must retain the above copyright
  10  *     notice, this list of conditions and the following disclaimer.
  11  *
  12  *   - Redistributions in binary form must reproduce the above copyright
  13  *     notice, this list of conditions and the following disclaimer in the
  14  *     documentation and/or other materials provided with the distribution.
  15  *
  16  *   - Neither the name of Oracle nor the names of its
  17  *     contributors may be used to endorse or promote products derived
  18  *     from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR


  76     public void render(int w, int h, Graphics2D g2) {
  77 
  78         int w2 = w / 2;
  79         int h2 = h / 2;
  80         g2.setPaint(new GradientPaint(0, 0, outerC, w * .35f, h * .35f, innerC));
  81         g2.fillRect(0, 0, w2, h2);
  82         g2.setPaint(new GradientPaint(w, 0, outerC, w * .65f, h * .35f, innerC));
  83         g2.fillRect(w2, 0, w2, h2);
  84         g2.setPaint(new GradientPaint(0, h, outerC, w * .35f, h * .65f, innerC));
  85         g2.fillRect(0, h2, w2, h2);
  86         g2.setPaint(new GradientPaint(w, h, outerC, w * .65f, h * .65f, innerC));
  87         g2.fillRect(w2, h2, w2, h2);
  88 
  89         g2.setColor(black);
  90         TextLayout tl = new TextLayout(
  91                 "GradientPaint", g2.getFont(), g2.getFontRenderContext());
  92         tl.draw(g2, (int) (w / 2 - tl.getBounds().getWidth() / 2),
  93                 (int) (h / 2 + tl.getBounds().getHeight() / 2));
  94     }
  95 
  96     public static void main(String s[]) {
  97         createDemoFrame(new Gradient());
  98     }
  99 
 100 
 101     static class DemoControls extends CustomControls implements ActionListener {
 102 
 103         Gradient demo;
 104         Color colors[] = { red, orange, yellow, green, blue, lightGray, cyan,
 105             magenta };
 106         String colorName[] = { "Red", "Orange", "Yellow", "Green",
 107             "Blue", "lightGray", "Cyan", "Magenta" };
 108         JMenuItem innerMI[] = new JMenuItem[colors.length];
 109         JMenuItem outerMI[] = new JMenuItem[colors.length];
 110         ColoredSquare squares[] = new ColoredSquare[colors.length];
 111         JMenu imenu, omenu;
 112 
 113         @SuppressWarnings("LeakingThisInConstructor")
 114         public DemoControls(Gradient demo) {
 115             super(demo.name);
 116             this.demo = demo;
 117             JMenuBar inMenuBar = new JMenuBar();
 118             add(inMenuBar);
 119             JMenuBar outMenuBar = new JMenuBar();
 120             add(outMenuBar);
 121             Font font = new Font(Font.SERIF, Font.PLAIN, 10);
 122 
 123             imenu = inMenuBar.add(new JMenu("Inner Color"));
 124             imenu.setFont(font);
 125             imenu.setIcon(new ColoredSquare(demo.innerC));
 126             omenu = outMenuBar.add(new JMenu("Outer Color"));
 127             omenu.setFont(font);
 128             omenu.setIcon(new ColoredSquare(demo.outerC));
 129             for (int i = 0; i < colors.length; i++) {
 130                 squares[i] = new ColoredSquare(colors[i]);


   1 /*
   2  *
   3  * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  *
   9  *   - Redistributions of source code must retain the above copyright
  10  *     notice, this list of conditions and the following disclaimer.
  11  *
  12  *   - Redistributions in binary form must reproduce the above copyright
  13  *     notice, this list of conditions and the following disclaimer in the
  14  *     documentation and/or other materials provided with the distribution.
  15  *
  16  *   - Neither the name of Oracle nor the names of its
  17  *     contributors may be used to endorse or promote products derived
  18  *     from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR


  76     public void render(int w, int h, Graphics2D g2) {
  77 
  78         int w2 = w / 2;
  79         int h2 = h / 2;
  80         g2.setPaint(new GradientPaint(0, 0, outerC, w * .35f, h * .35f, innerC));
  81         g2.fillRect(0, 0, w2, h2);
  82         g2.setPaint(new GradientPaint(w, 0, outerC, w * .65f, h * .35f, innerC));
  83         g2.fillRect(w2, 0, w2, h2);
  84         g2.setPaint(new GradientPaint(0, h, outerC, w * .35f, h * .65f, innerC));
  85         g2.fillRect(0, h2, w2, h2);
  86         g2.setPaint(new GradientPaint(w, h, outerC, w * .65f, h * .65f, innerC));
  87         g2.fillRect(w2, h2, w2, h2);
  88 
  89         g2.setColor(black);
  90         TextLayout tl = new TextLayout(
  91                 "GradientPaint", g2.getFont(), g2.getFontRenderContext());
  92         tl.draw(g2, (int) (w / 2 - tl.getBounds().getWidth() / 2),
  93                 (int) (h / 2 + tl.getBounds().getHeight() / 2));
  94     }
  95 
  96     public static void main(String[] s) {
  97         createDemoFrame(new Gradient());
  98     }
  99 
 100 
 101     static class DemoControls extends CustomControls implements ActionListener {
 102 
 103         Gradient demo;
 104         Color[] colors = { red, orange, yellow, green, blue, lightGray, cyan,
 105             magenta };
 106         String[] colorName = { "Red", "Orange", "Yellow", "Green",
 107             "Blue", "lightGray", "Cyan", "Magenta" };
 108         JMenuItem[] innerMI = new JMenuItem[colors.length];
 109         JMenuItem[] outerMI = new JMenuItem[colors.length];
 110         ColoredSquare[] squares = new ColoredSquare[colors.length];
 111         JMenu imenu, omenu;
 112 
 113         @SuppressWarnings("LeakingThisInConstructor")
 114         public DemoControls(Gradient demo) {
 115             super(demo.name);
 116             this.demo = demo;
 117             JMenuBar inMenuBar = new JMenuBar();
 118             add(inMenuBar);
 119             JMenuBar outMenuBar = new JMenuBar();
 120             add(outMenuBar);
 121             Font font = new Font(Font.SERIF, Font.PLAIN, 10);
 122 
 123             imenu = inMenuBar.add(new JMenu("Inner Color"));
 124             imenu.setFont(font);
 125             imenu.setIcon(new ColoredSquare(demo.innerC));
 126             omenu = outMenuBar.add(new JMenu("Outer Color"));
 127             omenu.setFont(font);
 128             omenu.setIcon(new ColoredSquare(demo.outerC));
 129             for (int i = 0; i < colors.length; i++) {
 130                 squares[i] = new ColoredSquare(colors[i]);


< prev index next >