001: /*
002: * Copyright (c) 2005, romain guy (romain.guy@jext.org) and craig wickesser (craig@codecraig.com)
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
006: *
007: * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
008: * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
009: * * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
010: *
011: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
012: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
013: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
014: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
015: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
016: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
017: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
018: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
019: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
020: * POSSIBILITY OF SUCH DAMAGE.
021: */
022:
023: package net.java.swingfx.rubberband.demo;
024:
025: import java.awt.Dimension;
026: import java.awt.Graphics;
027: import java.awt.Toolkit;
028: import java.awt.event.ActionEvent;
029: import java.awt.event.ActionListener;
030:
031: import javax.swing.ButtonGroup;
032: import javax.swing.JComponent;
033: import javax.swing.JFrame;
034: import javax.swing.JPanel;
035: import javax.swing.JRadioButton;
036:
037: import net.java.swingfx.rubberband.canvas.RubberBandCanvas;
038: import net.java.swingfx.rubberband.rubberbands.AnimatedRectangularRubberBand;
039: import net.java.swingfx.rubberband.rubberbands.OvalRubberBand;
040: import net.java.swingfx.rubberband.rubberbands.RectangularRubberBand;
041: import net.java.swingfx.rubberband.rubberbands.RubberBand;
042:
043: /**
044: * A simple class with a main method to try out the different rubber band's.
045: * The idea is to make the rubber bands flexible and easy to use.
046: * Currently, in order for it to work you need to do three things:
047: *
048: * 1) Create a JComponent which implements the RubberBandCanvas interface
049: *
050: * 2) Override the canvas' paintComponent(Graphics g) method so that it calls back
051: * to RubberBand.draw(Graphics g)
052: *
053: * 3) Create a {@link net.java.swingfx.rubberband.rubberbands.RubberBand}
054: *
055: * @author rwickesser
056: * $Revision: 1.1 $
057: */
058: public class Main {
059: private static final Dimension SIZE = new Dimension(300, 300);
060:
061: public static void main(String[] args) {
062: // Create RubberBandCanvas
063: RubberBandCanvas canvas = new MyPanel();
064:
065: // Create rubber band
066: RubberBand rb = new RectangularRubberBand(canvas);
067:
068: // Setup demo frame
069: JFrame f = new JFrame("Rubber Band Demo");
070: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
071: f.setSize(SIZE);
072: Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
073: int x = (scrSize.width / 2) - (SIZE.width / 2);
074: int y = (scrSize.height / 2) - (SIZE.height / 2);
075: f.setLocation(x, y);
076: // add canvas to content pane
077: f.getContentPane().add(canvas.getCanvas());
078: f.setVisible(true);
079: }
080:
081: /**
082: * A demonstration on how to create a {@link RubberBandCanvas}. Note
083: * that paintComponent(Graphics g) has been overridden.
084: *
085: * @author rwickesser
086: * $Revision: 1.1 $
087: */
088: private static class MyPanel extends JPanel implements
089: RubberBandCanvas, ActionListener {
090: private static final long serialVersionUID = 3256445806658466864L;
091:
092: private RubberBand rubberband;
093:
094: private JRadioButton radioRect;
095: private JRadioButton radioOval;
096: private JRadioButton radioAnimRect;
097:
098: public MyPanel() {
099: radioRect = new JRadioButton("Rectangle");
100: radioRect.setSelected(true);
101: radioRect.addActionListener(this );
102: radioOval = new JRadioButton("Oval");
103: radioOval.addActionListener(this );
104: radioAnimRect = new JRadioButton("Animated Rectangle");
105: radioAnimRect.addActionListener(this );
106: ButtonGroup bg = new ButtonGroup();
107: bg.add(radioRect);
108: bg.add(radioOval);
109: bg.add(radioAnimRect);
110:
111: add(radioRect);
112: add(radioOval);
113: add(radioAnimRect);
114: }
115:
116: /*************** [ IMPORTANT - override paintComponent ] *******************/
117: public void paintComponent(Graphics g) {
118: super .paintComponent(g);
119: rubberband.draw(g);
120: }
121:
122: /* (non-Javadoc)
123: * @see RubberBandCanvas#setRubberBand(RubberBand)
124: */
125: public void setRubberBand(RubberBand rubberband) {
126: this .rubberband = rubberband;
127: }
128:
129: /* (non-Javadoc)
130: * @see RubberBandCanvas#getCanvas()
131: */
132: public JComponent getCanvas() {
133: return this ;
134: }
135:
136: /* (non-Javadoc)
137: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
138: */
139: public void actionPerformed(ActionEvent e) {
140: if (radioRect.isSelected()) {
141: rubberband = new RectangularRubberBand(this );
142: } else if (radioOval.isSelected()) {
143: rubberband = new OvalRubberBand(this );
144: } else if (radioAnimRect.isSelected()) {
145: rubberband = new AnimatedRectangularRubberBand(this);
146: }
147: }
148: }
149: }
|