📄 areas.java
字号:
/* * @(#)Areas.java 1.23 99/04/23 * * Copyright (c) 1998, 1999 by Sun Microsystems, Inc. All Rights Reserved. * * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, * modify and redistribute this software in source and binary code form, * provided that i) this copyright notice and license appear on all copies of * the software; and ii) Licensee does not utilize the software in a manner * which is disparaging to Sun. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * This software is not designed or intended for use in on-line control of * aircraft, air traffic, aircraft navigation or aircraft communications; or in * the design, construction, operation or maintenance of any nuclear * facility. Licensee represents and warrants that it will not use or * redistribute the Software for such purposes. */package demos.Clipping;import java.awt.*;import java.awt.event.*;import java.awt.geom.Area;import java.awt.geom.Ellipse2D;import java.awt.geom.GeneralPath;import javax.swing.*;import DemoSurface;import DemoPanel;import CustomControls;/** * */public class Areas extends DemoSurface implements CustomControls { protected String areaType = "nop"; private DemoControls controls; public Areas() { setBackground(Color.white); controls = new DemoControls(this); } public String[] getCustomControlsConstraints() { return new String[] { BorderLayout.NORTH }; } public Component[] getCustomControls() { return new Component[] { (Component) controls }; } public void customControlsThread(int state) { if (state == CustomControls.START) { controls.start(); } else if (state == CustomControls.STOP) { controls.stop(); } } public void drawDemo(int w, int h, Graphics2D g2) { GeneralPath p1 = new GeneralPath(); p1.moveTo( w * .25f, 0.0f); p1.lineTo( w * .75f, h * .5f); p1.lineTo( w * .25f, (float) h); p1.lineTo( 0.0f, h * .5f); p1.closePath(); GeneralPath p2 = new GeneralPath(); p2.moveTo( w * .75f, 0.0f); p2.lineTo( (float) w, h * .5f); p2.lineTo( w * .75f, (float) h); p2.lineTo( w * .25f, h * .5f); p2.closePath(); Area area = new Area(p1); g2.setColor(Color.yellow); if (areaType.equals("nop")) { g2.fill(p1); g2.fill(p2); g2.setColor(Color.red); g2.draw(p1); g2.draw(p2); return; } else if (areaType.equals("add")) { area.add(new Area(p2)); } else if (areaType.equals("sub")) { area.subtract(new Area(p2)); } else if (areaType.equals("xor")) { area.exclusiveOr(new Area(p2)); } else if (areaType.equals("int")) { area.intersect(new Area(p2)); } else if (areaType.equals("pear")) { double sx = w/100; double sy = h/140; g2.scale(sx, sy); double x = w/sx/2; double y = h/sy/2; // Creates the first leaf by filling the intersection of two Area // objects created from an ellipse. Ellipse2D leaf = new Ellipse2D.Double(x-16, y-29, 15.0, 15.0); Area leaf1 = new Area(leaf); leaf.setFrame(x-14, y-47, 30.0, 30.0); Area leaf2 = new Area(leaf); leaf1.intersect(leaf2); g2.setColor(Color.green); g2.fill(leaf1); // Creates the second leaf. leaf.setFrame(x+1, y-29, 15.0, 15.0); leaf1 = new Area(leaf); leaf2.intersect(leaf1); g2.fill(leaf2); // Creates the stem by filling the Area resulting from the // subtraction of two Area objects created from an ellipse. Ellipse2D stem = new Ellipse2D.Double(x, y-42, 40.0, 40.0); Area st1 = new Area(stem); stem.setFrame(x+3, y-47, 50.0, 50.0); st1.subtract(new Area(stem)); g2.setColor(Color.black); g2.fill(st1); // Creates the pear itself by filling the Area resulting from the // union of two Area objects created by two different ellipses. Ellipse2D circle = new Ellipse2D.Double(x-25, y, 50.0, 50.0); Ellipse2D oval = new Ellipse2D.Double(x-19, y-20, 40.0, 70.0); Area circ = new Area(circle); circ.add(new Area(oval)); g2.setColor(Color.yellow); g2.fill(circ); return; } g2.fill(area); g2.setColor(Color.red); g2.draw(area); } public static void main(String argv[]) { Frame f = new Frame("Java2D Demo - Areas"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); f.add("Center", new DemoPanel(new Areas())); f.pack(); f.setSize(new Dimension(400,300)); f.show(); } static class DemoControls extends JPanel implements ActionListener, Runnable { Areas demo; JToolBar toolbar; JComboBox combo; Thread thread; public DemoControls(Areas demo) { this.demo = demo; setBackground(Color.gray); add(toolbar = new JToolBar()); toolbar.setFloatable(false); addTool("nop", "no area operation", true); addTool("add", "add", false); addTool("sub", "subtract", false); addTool("xor", "exclusiveOr", false); addTool("int", "intersection", false); addTool("pear", "pear", false); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (thread == null) start(); else stop(); } }); } public void addTool(String str, String tooltip, boolean state) { JButton b = (JButton) toolbar.add(new JButton(str)); b.setBackground(state ? Color.green : Color.lightGray); b.setToolTipText(tooltip); b.setSelected(state); b.addActionListener(this); } public void actionPerformed(ActionEvent e) { for (int i = 0; i < toolbar.getComponentCount(); i++) { JButton b = (JButton) toolbar.getComponentAtIndex(i); b.setBackground(Color.lightGray); } JButton b = (JButton) e.getSource(); b.setBackground(Color.green); demo.areaType = b.getText(); demo.repaint(); } public Dimension getPreferredSize() { return new Dimension(200,37); } public void start() { if (thread != null) { return; } thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.setName("Clipping.Areas DemoControls Thread"); thread.start(); } public synchronized void stop() { if (thread != null) { thread.interrupt(); } thread = null; notifyAll(); } public void run() { try { thread.sleep(1111); } catch (Exception e) { return; } Thread me = Thread.currentThread(); while (thread == me) { for (int i = 0; i < toolbar.getComponentCount(); i++) { ((JButton) toolbar.getComponentAtIndex(i)).doClick(); try { thread.sleep(4444); } catch (InterruptedException e) { return; } } } thread = null; } } // End DemoControls} // End Areas
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -