< prev index next >

src/demo/share/jfc/J2Ddemo/java2d/demos/Colors/BullsEye.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


  36 import static java.awt.Color.WHITE;
  37 import java.awt.Color;
  38 import java.awt.Graphics2D;
  39 import java.awt.geom.Ellipse2D;
  40 import java2d.Surface;
  41 
  42 
  43 /**
  44  * Creating colors with an alpha value.
  45  */
  46 @SuppressWarnings("serial")
  47 public class BullsEye extends Surface {
  48 
  49     public BullsEye() {
  50         setBackground(WHITE);
  51     }
  52 
  53     @Override
  54     public void render(int w, int h, Graphics2D g2) {
  55 
  56         Color reds[] = { RED.darker(), RED };
  57         for (int N = 0; N < 18; N++) {
  58             float i = (N + 2) / 2.0f;
  59             float x = (5 + i * (w / 2 / 10));
  60             float y = (5 + i * (h / 2 / 10));
  61             float ew = (w - 10) - (i * w / 10);
  62             float eh = (h - 10) - (i * h / 10);
  63             float alpha = (N == 0) ? 0.1f : 1.0f / (19.0f - N);
  64             if (N >= 16) {
  65                 g2.setColor(reds[N - 16]);
  66             } else {
  67                 g2.setColor(new Color(0f, 0f, 0f, alpha));
  68             }
  69             g2.fill(new Ellipse2D.Float(x, y, ew, eh));
  70         }
  71     }
  72 
  73     public static void main(String s[]) {
  74         createDemoFrame(new BullsEye());
  75     }
  76 }
   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


  36 import static java.awt.Color.WHITE;
  37 import java.awt.Color;
  38 import java.awt.Graphics2D;
  39 import java.awt.geom.Ellipse2D;
  40 import java2d.Surface;
  41 
  42 
  43 /**
  44  * Creating colors with an alpha value.
  45  */
  46 @SuppressWarnings("serial")
  47 public class BullsEye extends Surface {
  48 
  49     public BullsEye() {
  50         setBackground(WHITE);
  51     }
  52 
  53     @Override
  54     public void render(int w, int h, Graphics2D g2) {
  55 
  56         Color[] reds = { RED.darker(), RED };
  57         for (int N = 0; N < 18; N++) {
  58             float i = (N + 2) / 2.0f;
  59             float x = (5 + i * (w / 2 / 10));
  60             float y = (5 + i * (h / 2 / 10));
  61             float ew = (w - 10) - (i * w / 10);
  62             float eh = (h - 10) - (i * h / 10);
  63             float alpha = (N == 0) ? 0.1f : 1.0f / (19.0f - N);
  64             if (N >= 16) {
  65                 g2.setColor(reds[N - 16]);
  66             } else {
  67                 g2.setColor(new Color(0f, 0f, 0f, alpha));
  68             }
  69             g2.fill(new Ellipse2D.Float(x, y, ew, eh));
  70         }
  71     }
  72 
  73     public static void main(String[] s) {
  74         createDemoFrame(new BullsEye());
  75     }
  76 }
< prev index next >