< prev index next >

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


  59         setBackground(WHITE);
  60     }
  61 
  62     @Override
  63     public void render(int w, int h, Graphics2D g2) {
  64         FontRenderContext frc = g2.getFontRenderContext();
  65         Font font = g2.getFont();
  66         TextLayout tl = new TextLayout("Dashes", font, frc);
  67         float sw = (float) tl.getBounds().getWidth();
  68         float sh = tl.getAscent() + tl.getDescent();
  69         g2.setColor(BLACK);
  70         tl.draw(g2, (w / 2 - sw / 2), sh + 5);
  71 
  72         BasicStroke dotted = new BasicStroke(3, BasicStroke.CAP_ROUND,
  73                 BasicStroke.JOIN_ROUND, 0, new float[] { 0, 6, 0, 6 }, 0);
  74         g2.setStroke(dotted);
  75         g2.drawRect(3, 3, w - 6, h - 6);
  76 
  77         int x = 0;
  78         int y = h - 34;
  79         BasicStroke bs[] = new BasicStroke[6];
  80 
  81         float j = 1.1f;
  82         for (int i = 0; i < bs.length; i++, j += 1.0f) {
  83             float dash[] = { j };
  84             BasicStroke b = new BasicStroke(1.0f, BasicStroke.CAP_BUTT,
  85                     BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
  86             g2.setStroke(b);
  87             g2.drawLine(20, y, w - 20, y);
  88             bs[i] = new BasicStroke(3.0f, BasicStroke.CAP_BUTT,
  89                     BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
  90             y += 5;
  91         }
  92 
  93         Shape shape = null;
  94         y = 0;
  95         for (int i = 0; i < 6; i++) {
  96             x = (i == 0 || i == 3) ? (w / 3 - w / 5) / 2 : x + w / 3;
  97             y = (i <= 2) ? (int) sh + h / 12 : h / 2;
  98 
  99             g2.setStroke(bs[i]);
 100             g2.translate(x, y);
 101             switch (i) {
 102                 case 0:
 103                     shape = new Arc2D.Float(0.0f, 0.0f, w / 5, h / 4, 45, 270,


 111                             10.0f, 10.0f);
 112                     break;
 113                 case 3:
 114                     shape = new Rectangle2D.Float(0.0f, 0.0f, w / 5, h / 4);
 115                     break;
 116                 case 4:
 117                     shape = new QuadCurve2D.Float(0.0f, 0.0f, w / 10, h / 2, w
 118                             / 5, 0.0f);
 119                     break;
 120                 case 5:
 121                     shape = new CubicCurve2D.Float(0.0f, 0.0f, w / 15, h / 2, w
 122                             / 10, h / 4, w / 5, 0.0f);
 123                     break;
 124             }
 125 
 126             g2.draw(shape);
 127             g2.translate(-x, -y);
 128         }
 129     }
 130 
 131     public static void main(String argv[]) {
 132         createDemoFrame(new Dash());
 133     }
 134 }
   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


  59         setBackground(WHITE);
  60     }
  61 
  62     @Override
  63     public void render(int w, int h, Graphics2D g2) {
  64         FontRenderContext frc = g2.getFontRenderContext();
  65         Font font = g2.getFont();
  66         TextLayout tl = new TextLayout("Dashes", font, frc);
  67         float sw = (float) tl.getBounds().getWidth();
  68         float sh = tl.getAscent() + tl.getDescent();
  69         g2.setColor(BLACK);
  70         tl.draw(g2, (w / 2 - sw / 2), sh + 5);
  71 
  72         BasicStroke dotted = new BasicStroke(3, BasicStroke.CAP_ROUND,
  73                 BasicStroke.JOIN_ROUND, 0, new float[] { 0, 6, 0, 6 }, 0);
  74         g2.setStroke(dotted);
  75         g2.drawRect(3, 3, w - 6, h - 6);
  76 
  77         int x = 0;
  78         int y = h - 34;
  79         BasicStroke[] bs = new BasicStroke[6];
  80 
  81         float j = 1.1f;
  82         for (int i = 0; i < bs.length; i++, j += 1.0f) {
  83             float[] dash = { j };
  84             BasicStroke b = new BasicStroke(1.0f, BasicStroke.CAP_BUTT,
  85                     BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
  86             g2.setStroke(b);
  87             g2.drawLine(20, y, w - 20, y);
  88             bs[i] = new BasicStroke(3.0f, BasicStroke.CAP_BUTT,
  89                     BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
  90             y += 5;
  91         }
  92 
  93         Shape shape = null;
  94         y = 0;
  95         for (int i = 0; i < 6; i++) {
  96             x = (i == 0 || i == 3) ? (w / 3 - w / 5) / 2 : x + w / 3;
  97             y = (i <= 2) ? (int) sh + h / 12 : h / 2;
  98 
  99             g2.setStroke(bs[i]);
 100             g2.translate(x, y);
 101             switch (i) {
 102                 case 0:
 103                     shape = new Arc2D.Float(0.0f, 0.0f, w / 5, h / 4, 45, 270,


 111                             10.0f, 10.0f);
 112                     break;
 113                 case 3:
 114                     shape = new Rectangle2D.Float(0.0f, 0.0f, w / 5, h / 4);
 115                     break;
 116                 case 4:
 117                     shape = new QuadCurve2D.Float(0.0f, 0.0f, w / 10, h / 2, w
 118                             / 5, 0.0f);
 119                     break;
 120                 case 5:
 121                     shape = new CubicCurve2D.Float(0.0f, 0.0f, w / 15, h / 2, w
 122                             / 10, h / 4, w / 5, 0.0f);
 123                     break;
 124             }
 125 
 126             g2.draw(shape);
 127             g2.translate(-x, -y);
 128         }
 129     }
 130 
 131     public static void main(String[] argv) {
 132         createDemoFrame(new Dash());
 133     }
 134 }
< prev index next >