Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/demos/Lines/Dash.java
41175 views
/*1*2* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/31package java2d.demos.Lines;323334import static java.awt.Color.BLACK;35import static java.awt.Color.WHITE;36import java.awt.BasicStroke;37import java.awt.Font;38import java.awt.Graphics2D;39import java.awt.Shape;40import java.awt.font.FontRenderContext;41import java.awt.font.TextLayout;42import java.awt.geom.Arc2D;43import java.awt.geom.CubicCurve2D;44import java.awt.geom.Ellipse2D;45import java.awt.geom.QuadCurve2D;46import java.awt.geom.Rectangle2D;47import java.awt.geom.RoundRectangle2D;48import java2d.Surface;495051/**52* Various shapes stroked with a dashing pattern.53*/54@SuppressWarnings("serial")55public class Dash extends Surface {5657public Dash() {58setBackground(WHITE);59}6061@Override62public void render(int w, int h, Graphics2D g2) {63FontRenderContext frc = g2.getFontRenderContext();64Font font = g2.getFont();65TextLayout tl = new TextLayout("Dashes", font, frc);66float sw = (float) tl.getBounds().getWidth();67float sh = tl.getAscent() + tl.getDescent();68g2.setColor(BLACK);69tl.draw(g2, (w / 2 - sw / 2), sh + 5);7071BasicStroke dotted = new BasicStroke(3, BasicStroke.CAP_ROUND,72BasicStroke.JOIN_ROUND, 0, new float[] { 0, 6, 0, 6 }, 0);73g2.setStroke(dotted);74g2.drawRect(3, 3, w - 6, h - 6);7576int x = 0;77int y = h - 34;78BasicStroke[] bs = new BasicStroke[6];7980float j = 1.1f;81for (int i = 0; i < bs.length; i++, j += 1.0f) {82float[] dash = { j };83BasicStroke b = new BasicStroke(1.0f, BasicStroke.CAP_BUTT,84BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);85g2.setStroke(b);86g2.drawLine(20, y, w - 20, y);87bs[i] = new BasicStroke(3.0f, BasicStroke.CAP_BUTT,88BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);89y += 5;90}9192Shape shape = null;93y = 0;94for (int i = 0; i < 6; i++) {95x = (i == 0 || i == 3) ? (w / 3 - w / 5) / 2 : x + w / 3;96y = (i <= 2) ? (int) sh + h / 12 : h / 2;9798g2.setStroke(bs[i]);99g2.translate(x, y);100switch (i) {101case 0:102shape = new Arc2D.Float(0.0f, 0.0f, w / 5, h / 4, 45, 270,103Arc2D.PIE);104break;105case 1:106shape = new Ellipse2D.Float(0.0f, 0.0f, w / 5, h / 4);107break;108case 2:109shape = new RoundRectangle2D.Float(0.0f, 0.0f, w / 5, h / 4,11010.0f, 10.0f);111break;112case 3:113shape = new Rectangle2D.Float(0.0f, 0.0f, w / 5, h / 4);114break;115case 4:116shape = new QuadCurve2D.Float(0.0f, 0.0f, w / 10, h / 2, w117/ 5, 0.0f);118break;119case 5:120shape = new CubicCurve2D.Float(0.0f, 0.0f, w / 15, h / 2, w121/ 10, h / 4, w / 5, 0.0f);122break;123}124125g2.draw(shape);126g2.translate(-x, -y);127}128}129130public static void main(String[] argv) {131createDemoFrame(new Dash());132}133}134135136