home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Lines / Joins.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  7.6 KB  |  211 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)Joins.java    1.23 02/06/13
  38.  */
  39.  
  40. package java2d.demos.Lines;
  41.  
  42. import java.awt.*;
  43. import java.awt.event.*;
  44. import java.awt.geom.AffineTransform;
  45. import java.awt.geom.GeneralPath;
  46. import javax.swing.*;
  47. import javax.swing.border.*;
  48. import javax.swing.event.*;
  49. import java2d.ControlsSurface;
  50. import java2d.CustomControls;
  51.  
  52.  
  53. /**
  54.  * BasicStroke join types and width sizes illustrated.  Control for
  55.  * rendering a shape returned from BasicStroke.createStrokedShape(Shape).
  56.  */
  57. public class Joins extends ControlsSurface implements ChangeListener {
  58.  
  59.     protected int joinType = BasicStroke.JOIN_MITER;
  60.     protected float bswidth = 20.0f;
  61.     protected JSlider slider;
  62.     protected JLabel label;
  63.  
  64.  
  65.     public Joins() {
  66.         setBackground(Color.white);
  67.         slider = new JSlider(JSlider.VERTICAL, 0, 100, (int)(bswidth*2));
  68.         slider.setPreferredSize(new Dimension(15, 100));
  69.         slider.addChangeListener(this);
  70.         setControls(new Component[] { new DemoControls(this), slider });
  71.         setConstraints(new String[] { BorderLayout.NORTH, BorderLayout.WEST});
  72.     }
  73.  
  74.  
  75.     public void stateChanged(ChangeEvent e) {
  76.         // when using these sliders use double buffering, which means
  77.         // ignoring when DemoSurface.imageType = 'On Screen'
  78.         if (getImageType() <= 1) {
  79.             setImageType(2);
  80.         }
  81.         bswidth = (float) slider.getValue() / 2.0f;
  82.         label.setText("width=" + String.valueOf(bswidth));
  83.         label.repaint();
  84.         repaint();
  85.     }    
  86.  
  87.  
  88.     public void render(int w, int h, Graphics2D g2) {
  89.         BasicStroke bs = new BasicStroke(bswidth, 
  90.                                     BasicStroke.CAP_BUTT, joinType);
  91.         GeneralPath p = new GeneralPath();
  92.         p.moveTo(- w / 4.0f, - h / 12.0f);
  93.         p.lineTo(+ w / 4.0f, - h / 12.0f);
  94.         p.lineTo(- w / 6.0f, + h / 4.0f);
  95.         p.lineTo(+     0.0f, - h / 4.0f);
  96.         p.lineTo(+ w / 6.0f, + h / 4.0f);
  97.         p.closePath();
  98.         p.closePath();
  99.         g2.translate(w/2, h/2);
  100.         g2.setColor(Color.black);
  101.         g2.draw(bs.createStrokedShape(p));
  102.     }
  103.  
  104.  
  105.     public static void main(String s[]) {
  106.         createDemoFrame(new Joins());
  107.     }
  108.  
  109.  
  110.     class DemoControls extends CustomControls implements ActionListener {
  111.  
  112.         Joins demo;
  113.         int joinType[] = { BasicStroke.JOIN_MITER, 
  114.                        BasicStroke.JOIN_ROUND, BasicStroke.JOIN_BEVEL };
  115.         String joinName[] = { "Mitered Join", "Rounded Join", "Beveled Join" };
  116.         JMenu menu;
  117.         JMenuItem menuitem[] = new JMenuItem[joinType.length];
  118.         JoinIcon icons[] = new JoinIcon[joinType.length];
  119.         JToolBar toolbar;
  120.  
  121.  
  122.         public DemoControls(Joins demo) {
  123.             super(demo.name);
  124.             this.demo = demo;
  125.             setBackground(Color.gray);
  126.             setLayout(new BorderLayout());
  127.             setBorder(new EmptyBorder(2,2,2,2));
  128.             label = new JLabel("width=" + String.valueOf(demo.bswidth));
  129.             Font font = new Font("serif", Font.BOLD, 14);
  130.             label.setFont(font);
  131.             label.setForeground(Color.lightGray);
  132.             add("West", label);
  133.             JMenuBar menubar = new JMenuBar();
  134.             add("East", menubar);
  135.             menu = (JMenu) menubar.add(new JMenu(joinName[0]));
  136.             menu.setFont(font = new Font("serif", Font.PLAIN, 10));
  137.             for (int i = 0; i < joinType.length; i++) {
  138.                 icons[i]= new JoinIcon(joinType[i]);
  139.                 menuitem[i] = menu.add(new JMenuItem(joinName[i]));
  140.                 menuitem[i].setFont(font);
  141.                 menuitem[i].setIcon(icons[i]);
  142.                 menuitem[i].addActionListener(this);
  143.             } 
  144.             menu.setIcon(icons[0]);
  145.         }
  146.  
  147.  
  148.         public void actionPerformed(ActionEvent e) {
  149.             for (int i = 0; i < joinType.length; i++) {
  150.                 if (e.getSource().equals(menuitem[i])) {
  151.                     demo.joinType = joinType[i];
  152.                     menu.setIcon(icons[i]);
  153.                     menu.setText(joinName[i]);
  154.                     break;
  155.                 } 
  156.             }
  157.             demo.repaint();
  158.         }
  159.  
  160.  
  161.         public Dimension getPreferredSize() {
  162.             return new Dimension(200,28);
  163.         }
  164.  
  165.  
  166.         public void run() {
  167.             try { thread.sleep(999); } catch (Exception e) { return; }
  168.             Thread me = Thread.currentThread();
  169.             while (thread == me) {
  170.                 for (int i = 0; i < menuitem.length; i++) {
  171.                     menuitem[i].doClick();
  172.                     for (int k = 10; k < 60; k+=2) {
  173.                         demo.slider.setValue(k);
  174.                         try {
  175.                             thread.sleep(100);
  176.                         } catch (InterruptedException e) { return; }
  177.                     }
  178.                     try {
  179.                         thread.sleep(999);
  180.                     } catch (InterruptedException e) { return; }
  181.                 }
  182.             }
  183.             thread = null;
  184.         }
  185.  
  186.  
  187.         class JoinIcon implements Icon {
  188.             int joinType;
  189.             public JoinIcon(int joinType) {
  190.                 this.joinType = joinType;
  191.             }
  192.     
  193.             public void paintIcon(Component c, Graphics g, int x, int y) {
  194.                 ((Graphics2D) g).setRenderingHint(
  195.                      RenderingHints.KEY_ANTIALIASING, 
  196.                      RenderingHints.VALUE_ANTIALIAS_ON);
  197.                 BasicStroke bs = new BasicStroke(8.0f, 
  198.                                     BasicStroke.CAP_BUTT, joinType);
  199.                 ((Graphics2D) g).setStroke(bs);
  200.                 GeneralPath p = new GeneralPath();
  201.                 p.moveTo(0, 3);
  202.                 p.lineTo(getIconWidth()-2, getIconHeight()/2);
  203.                 p.lineTo(0,getIconHeight());
  204.                 ((Graphics2D) g).draw(p);
  205.             }
  206.             public int getIconWidth() { return 20; }
  207.             public int getIconHeight() { return 20; }
  208.         } // End JoinIcon class
  209.     } // End DemoControls class
  210. } // End Joins class
  211.