Joins.java
Upload User: cdlibang
Upload Date: 2016-07-17
Package Size: 774k
Code Size: 9k
Category:

2D Graphic

Development Platform:

Java

  1. /*
  2.  * @(#)Joins.java 1.18 99/08/16
  3.  *
  4.  * Copyright (c) 1998, 1999 by Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  * 
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  * 
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30. package demos.Lines;
  31. import java.awt.*;
  32. import java.awt.event.*;
  33. import java.awt.geom.AffineTransform;
  34. import java.awt.geom.GeneralPath;
  35. import javax.swing.*;
  36. import javax.swing.border.*;
  37. import javax.swing.event.*;
  38. import DemoSurface;
  39. import DemoPanel;
  40. import CustomControls;
  41. /**
  42.  * BasicStroke join types and width sizes illustrated.  Control for
  43.  * rendering a shape returned from BasicStroke.createStrokedShape(Shape).
  44.  */
  45. public class Joins extends DemoSurface implements CustomControls, ChangeListener {
  46.     protected int joinType = BasicStroke.JOIN_MITER;
  47.     protected float bswidth = 20.0f;
  48.     protected JSlider slider;
  49.     private DemoControls controls;
  50.     public Joins() {
  51.         setBackground(Color.white);
  52.         controls = new DemoControls(this);
  53.     }
  54.     public String[] getCustomControlsConstraints() {
  55.         return new String[] { BorderLayout.NORTH, 
  56.                                 BorderLayout.WEST };
  57.     }
  58.     public Component[] getCustomControls() {
  59.         slider = new JSlider(JSlider.VERTICAL, 0, 100, (int)(bswidth*2));
  60.         slider.setPreferredSize(new Dimension(15, 100));
  61.         slider.addChangeListener(this);
  62.         return new Component[] { (Component) controls, slider };
  63.     }
  64.     public void customControlsThread(int state) {
  65.         if (state == CustomControls.START) {
  66.             controls.start();
  67.         } else if (state == CustomControls.STOP) {
  68.             controls.stop();
  69.         }
  70.     }
  71.     public void stateChanged(ChangeEvent e) {
  72.         // when using these sliders use double buffering, which means
  73.         // ignoring when DemoSurface.imageType = 'On Screen'
  74.         if (getImageType() <= 1) {
  75.             setImageType(2);
  76.         }
  77.         bswidth = (float) slider.getValue() / 2.0f;
  78.         controls.label.setText("width=" + String.valueOf(bswidth));
  79.         controls.label.repaint();
  80.         repaint();
  81.     }    
  82.     public void drawDemo(int w, int h, Graphics2D g2) {
  83.         BasicStroke bs = new BasicStroke(bswidth, 
  84.                                     BasicStroke.CAP_BUTT, joinType);
  85.         GeneralPath p = new GeneralPath();
  86.         p.moveTo(- w / 4.0f, - h / 12.0f);
  87.         p.lineTo(+ w / 4.0f, - h / 12.0f);
  88.         p.lineTo(- w / 6.0f, + h / 4.0f);
  89.         p.lineTo(+     0.0f, - h / 4.0f);
  90.         p.lineTo(+ w / 6.0f, + h / 4.0f);
  91.         p.closePath();
  92.         p.closePath();
  93.         g2.translate(w/2, h/2);
  94.         g2.setColor(Color.black);
  95.         g2.draw(bs.createStrokedShape(p));
  96.     }
  97.     public static void main(String s[]) {
  98.         JFrame f = new JFrame("Java2D Demo - Joins");
  99.         f.addWindowListener(new WindowAdapter() {
  100.             public void windowClosing(WindowEvent e) {System.exit(0);}
  101.         });
  102.         f.getContentPane().add("Center", new DemoPanel(new Joins()));
  103.         f.pack();
  104.         f.setSize(new Dimension(400,300));
  105.         f.show();
  106.     }
  107.     static class DemoControls extends JPanel implements ActionListener, Runnable {
  108.         Joins demo;
  109.         int joinType[] = { BasicStroke.JOIN_MITER, 
  110.                        BasicStroke.JOIN_ROUND, BasicStroke.JOIN_BEVEL };
  111.         String joinName[] = { "Mitered Join", "Rounded Join", "Beveled Join" };
  112.         JMenu menu;
  113.         JMenuItem menuitem[] = new JMenuItem[joinType.length];
  114.         JoinIcon icons[] = new JoinIcon[joinType.length];
  115.         JToolBar toolbar;
  116.         JLabel label;
  117.         Thread thread;
  118.         public DemoControls(Joins demo) {
  119.             this.demo = demo;
  120.             setBackground(Color.gray);
  121.             setLayout(new BorderLayout());
  122.             setBorder(new EmptyBorder(2,2,2,2));
  123.             label = new JLabel("width=" + String.valueOf(demo.bswidth));
  124.             Font font = new Font("serif", Font.BOLD, 14);
  125.             label.setFont(font);
  126.             label.setForeground(Color.lightGray);
  127.             add("West", label);
  128.             JMenuBar menubar = new JMenuBar();
  129.             add("East", menubar);
  130.             menu = (JMenu) menubar.add(new JMenu(joinName[0]));
  131.             menu.setFont(font = new Font("serif", Font.PLAIN, 10));
  132.             for (int i = 0; i < joinType.length; i++) {
  133.                 icons[i]= new JoinIcon(joinType[i]);
  134.                 menuitem[i] = menu.add(new JMenuItem(joinName[i]));
  135.                 menuitem[i].setFont(font);
  136.                 menuitem[i].setIcon(icons[i]);
  137.                 menuitem[i].addActionListener(this);
  138.             } 
  139.             menu.setIcon(icons[0]);
  140.             addMouseListener(new MouseAdapter() {
  141.                 public void mouseClicked(MouseEvent e) {
  142.                     if (thread == null) start(); else stop();
  143.                 }
  144.             });
  145.         }
  146.         public void actionPerformed(ActionEvent e) {
  147.             for (int i = 0; i < joinType.length; i++) {
  148.                 if (e.getSource().equals(menuitem[i])) {
  149.                     demo.joinType = joinType[i];
  150.                     menu.setIcon(icons[i]);
  151.                     menu.setText(joinName[i]);
  152.                     break;
  153.                 } 
  154.             }
  155.             demo.repaint();
  156.         }
  157.         public Dimension getPreferredSize() {
  158.             return new Dimension(200,28);
  159.         }
  160.         public void start() {
  161.             if (thread != null) {
  162.                 return;
  163.             }
  164.             thread = new Thread(this);
  165.             thread.setPriority(Thread.MIN_PRIORITY);
  166.             thread.setName("Lines.Joins DemoControls Thread");
  167.             thread.start();
  168.         }
  169.         public synchronized void stop() {
  170.             if (thread != null) {
  171.                 thread.interrupt();
  172.             }
  173.             thread = null;
  174.             notifyAll();
  175.         }
  176.         public void run() {
  177.             try { thread.sleep(999); } catch (Exception e) { return; }
  178.             Thread me = Thread.currentThread();
  179.             while (thread == me) {
  180.                 for (int i = 0; i < menuitem.length; i++) {
  181.                     menuitem[i].doClick();
  182.                     for (int k = 10; k < 60; k+=2) {
  183.                         demo.slider.setValue(k);
  184.                         try {
  185.                             thread.sleep(100);
  186.                         } catch (InterruptedException e) { return; }
  187.                     }
  188.                     try {
  189.                         thread.sleep(999);
  190.                     } catch (InterruptedException e) { return; }
  191.                 }
  192.             }
  193.             thread = null;
  194.         }
  195.         class JoinIcon implements Icon {
  196.             int joinType;
  197.             public JoinIcon(int joinType) {
  198.                 this.joinType = joinType;
  199.             }
  200.     
  201.             public void paintIcon(Component c, Graphics g, int x, int y) {
  202.                 ((Graphics2D) g).setRenderingHint(
  203.                      RenderingHints.KEY_ANTIALIASING, 
  204.                      RenderingHints.VALUE_ANTIALIAS_ON);
  205.                 BasicStroke bs = new BasicStroke(8.0f, 
  206.                                     BasicStroke.CAP_BUTT, joinType);
  207.                 ((Graphics2D) g).setStroke(bs);
  208.                 GeneralPath p = new GeneralPath();
  209.                 p.moveTo(0, 3);
  210.                 p.lineTo(getIconWidth()-2, getIconHeight()/2);
  211.                 p.lineTo(0,getIconHeight());
  212.                 ((Graphics2D) g).draw(p);
  213.             }
  214.             public int getIconWidth() { return 20; }
  215.             public int getIconHeight() { return 20; }
  216.         } // End JoinIcon class
  217.     } // End DemoControls class
  218. } // End Joins class