Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/demos/Lines/Joins.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.BorderLayout;38import java.awt.Component;39import java.awt.Dimension;40import java.awt.Font;41import java.awt.Graphics;42import java.awt.Graphics2D;43import java.awt.RenderingHints;44import java.awt.event.ActionEvent;45import java.awt.event.ActionListener;46import java.awt.geom.GeneralPath;47import java2d.ControlsSurface;48import java2d.CustomControls;49import javax.swing.Icon;50import javax.swing.JLabel;51import javax.swing.JMenu;52import javax.swing.JMenuBar;53import javax.swing.JMenuItem;54import javax.swing.JSlider;55import javax.swing.JToolBar;56import javax.swing.SwingConstants;57import javax.swing.border.CompoundBorder;58import javax.swing.border.EmptyBorder;59import javax.swing.event.ChangeEvent;60import javax.swing.event.ChangeListener;616263/**64* BasicStroke join types and width sizes illustrated. Control for65* rendering a shape returned from BasicStroke.createStrokedShape(Shape).66*/67@SuppressWarnings("serial")68public class Joins extends ControlsSurface {6970protected int joinType = BasicStroke.JOIN_MITER;71protected float bswidth = 20.0f;72protected JSlider slider;73protected JLabel label;7475public Joins() {76setBackground(WHITE);77slider = new JSlider(SwingConstants.VERTICAL, 0, 100,78(int) (bswidth * 2));79slider.setPreferredSize(new Dimension(15, 100));80slider.addChangeListener(new ChangeListener() {8182public void stateChanged(ChangeEvent e) {83// when using these sliders use double buffering, which means84// ignoring when DemoSurface.imageType = 'On Screen'85if (getImageType() <= 1) {86setImageType(2);87}88bswidth = slider.getValue() / 2.0f;89label.setText(" Width = " + String.valueOf(bswidth));90label.repaint();91repaint();92}93});94setControls(new Component[] { new DemoControls(this), slider });95setConstraints(new String[] { BorderLayout.NORTH, BorderLayout.WEST });96}9798@Override99public void render(int w, int h, Graphics2D g2) {100BasicStroke bs = new BasicStroke(bswidth,101BasicStroke.CAP_BUTT, joinType);102GeneralPath p = new GeneralPath();103p.moveTo(-w / 4.0f, -h / 12.0f);104p.lineTo(+w / 4.0f, -h / 12.0f);105p.lineTo(-w / 6.0f, +h / 4.0f);106p.lineTo(+0.0f, -h / 4.0f);107p.lineTo(+w / 6.0f, +h / 4.0f);108p.closePath();109p.closePath();110g2.translate(w / 2, h / 2);111g2.setColor(BLACK);112g2.draw(bs.createStrokedShape(p));113}114115public static void main(String[] s) {116createDemoFrame(new Joins());117}118119120class DemoControls extends CustomControls implements ActionListener {121122Joins demo;123int[] joinType = { BasicStroke.JOIN_MITER,124BasicStroke.JOIN_ROUND, BasicStroke.JOIN_BEVEL };125String[] joinName = { "Mitered Join", "Rounded Join", "Beveled Join" };126JMenu menu;127JMenuItem[] menuitem = new JMenuItem[joinType.length];128JoinIcon[] icons = new JoinIcon[joinType.length];129JToolBar toolbar;130131@SuppressWarnings("LeakingThisInConstructor")132public DemoControls(Joins demo) {133super(demo.name);134setBorder(new CompoundBorder(getBorder(),135new EmptyBorder(2, 2, 2, 2)));136this.demo = demo;137setLayout(new BorderLayout());138label = new JLabel(" Width = " + String.valueOf(demo.bswidth));139Font font = new Font(Font.SERIF, Font.BOLD, 14);140label.setFont(font);141add("West", label);142JMenuBar menubar = new JMenuBar();143add("East", menubar);144menu = menubar.add(new JMenu(joinName[0]));145menu.setFont(font = new Font(Font.SERIF, Font.PLAIN, 10));146ActionListener actionListener = new ActionListener() {147148public void actionPerformed(ActionEvent e) {149throw new UnsupportedOperationException("Not supported yet.");150}151};152for (int i = 0; i < joinType.length; i++) {153icons[i] = new JoinIcon(joinType[i]);154menuitem[i] = menu.add(new JMenuItem(joinName[i]));155menuitem[i].setFont(font);156menuitem[i].setIcon(icons[i]);157menuitem[i].addActionListener(this);158}159menu.setIcon(icons[0]);160}161162@Override163public void actionPerformed(ActionEvent e) {164for (int i = 0; i < joinType.length; i++) {165if (e.getSource().equals(menuitem[i])) {166demo.joinType = joinType[i];167menu.setIcon(icons[i]);168menu.setText(joinName[i]);169break;170}171}172demo.repaint();173}174175@Override176public Dimension getPreferredSize() {177return new Dimension(200, 37);178}179180@Override181@SuppressWarnings("SleepWhileHoldingLock")182public void run() {183try {184Thread.sleep(999);185} catch (Exception e) {186return;187}188Thread me = Thread.currentThread();189while (thread == me) {190for (int i = 0; i < menuitem.length; i++) {191menuitem[i].doClick();192for (int k = 10; k < 60; k += 2) {193demo.slider.setValue(k);194try {195Thread.sleep(100);196} catch (InterruptedException e) {197return;198}199}200try {201Thread.sleep(999);202} catch (InterruptedException e) {203return;204}205}206}207thread = null;208}209210211class JoinIcon implements Icon {212213int joinType;214215public JoinIcon(int joinType) {216this.joinType = joinType;217}218219@Override220public void paintIcon(Component c, Graphics g, int x, int y) {221((Graphics2D) g).setRenderingHint(222RenderingHints.KEY_ANTIALIASING,223RenderingHints.VALUE_ANTIALIAS_ON);224BasicStroke bs = new BasicStroke(8.0f,225BasicStroke.CAP_BUTT, joinType);226((Graphics2D) g).setStroke(bs);227GeneralPath p = new GeneralPath();228p.moveTo(0, 3);229p.lineTo(getIconWidth() - 2, getIconHeight() / 2);230p.lineTo(0, getIconHeight());231((Graphics2D) g).draw(p);232}233234@Override235public int getIconWidth() {236return 20;237}238239@Override240public int getIconHeight() {241return 20;242}243} // End JoinIcon class244} // End DemoControls class245} // End Joins class246247248249