KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtriver > open > vectorvisuals > example > fundamental > SwingEmbedding


1 /*
2  * SwingEmbedding.java
3  *
4  * Created on 11 October 2005, 14:48
5  */

6
7 package com.thoughtriver.open.vectorvisuals.example.fundamental;
8
9 import java.awt.*;
10 import java.awt.event.*;
11 import java.awt.geom.*;
12 import java.util.*;
13
14 import javax.swing.*;
15
16 import com.thoughtriver.open.vectorvisuals.*;
17 import com.thoughtriver.open.vectorvisuals.task.*;
18
19 /**
20  * This small application illustrates how to create a Vector Visuals drawing
21  * area (a <CODE>VVDisplay</CODE>) that also displays a Swing component (in
22  * this case, a <CODE>JButton</CODE>) and interacts with it.
23  *
24  * @author Brandon Franklin
25  * @version $Date: 2006/11/25 09:35:06 $
26  */

27 public class SwingEmbedding implements Runnable JavaDoc {
28
29     /** The <CODE>VVDisplay</CODE> used to display the test */
30     private VVDisplay vvDisplay = null;
31
32     /**
33      * Creates a new instance of <CODE>SwingEmbedding</CODE>.
34      */

35     public SwingEmbedding() {
36         VVPanel panel = new VVPanel();
37
38         // The VVPanel extends JPanel, so it can be used in the familiar way
39
panel.setLayout(new FlowLayout());
40         JButton button1 = new JButton(new ButtonAction());
41         panel.add(button1);
42
43         vvDisplay = new VVDisplay(panel);
44     }
45
46     /**
47      * Starts the example.
48      */

49     public void run() {
50         JFrame frame = new JFrame("Vector Visuals Swing Embedding Example");
51         frame.setSize(640, 480);
52         frame.getContentPane().setLayout(new BorderLayout());
53         frame.getContentPane().add(vvDisplay.getViewPane(), BorderLayout.CENTER);
54         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
55
56         frame.setVisible(true);
57
58         // Set up the panel itself
59
vvDisplay.getViewPane().setBackground(Color.DARK_GRAY);
60         vvDisplay.setWorldViewScale(1);
61
62         // Create a text object
63
Brush brush = new Brush(Color.YELLOW, null, null);
64         VisualTextObject textObj = new VisualTextObject("Try resizing the frame.", null, null, brush);
65
66         // Set the location and size of the object
67
AffineTransform transform = textObj.getTransform();
68         transform.scale(3.0, 3.0);
69         transform.translate(40, 40);
70         textObj.setTransform(transform);
71         textObj.setLayer(2); // Keeps the text above the rectangles
72

73         // Display the object on the VVPanel
74
vvDisplay.addObject(textObj);
75     }
76
77     /**
78      * Instantiates a <CODE>SwingEmbedding</CODE> and executes it.
79      *
80      * @param args the command line arguments, ignored here
81      */

82     public static void main(final String JavaDoc[] args) {
83         Runnable JavaDoc example = new SwingEmbedding();
84         example.run();
85     }
86
87     /**
88      * This is the <CODE>Action</CODE> represented by the button in the panel.
89      *
90      * @author Brandon Franklin
91      * @version $Date: 2006/11/25 09:35:06 $
92      */

93     private class ButtonAction extends AbstractAction {
94
95         private static final long serialVersionUID = 2162173921388239990L;
96
97         /** Randomizer used by this action */
98         private final Random random = new Random();
99
100         /**
101          * Creates a new instance of <CODE>ButtonAction</CODE> and prepares it
102          * for use.
103          */

104         public ButtonAction() {
105             super("Press Me if You Dare!");
106         }
107
108         /**
109          * Called when the button is pressed. Generates a randomly placed and
110          * colored <CODE>VisualObject</CODE> rectangle, and starts it
111          * oscillating.
112          *
113          * @param event the event that has occurred
114          */

115         public void actionPerformed(@SuppressWarnings JavaDoc("unused")
116         final ActionEvent event) {
117
118             // Create the rectangle
119
Brush lineBrush = new Brush(new Color(random.nextInt()), null, null);
120             Brush fillBrush = new Brush(new Color(random.nextInt()), null, null);
121             VisualObject rectangle = new VisualObject(new Rectangle2D.Double(0, 0, random.nextInt(200), random.nextInt(200)), lineBrush, fillBrush);
122             rectangle.setTransform(AffineTransform.getTranslateInstance(random.nextInt(500), random.nextInt(300)));
123
124             // Add the rectangle to the view
125
vvDisplay.addObject(rectangle);
126
127             // Animate the rectangle
128
IndefiniteOscillationTask task = new IndefiniteOscillationTask(rectangle, random.nextGaussian());
129             vvDisplay.getTaskManager().addTask(task);
130             task.setRate(random.nextGaussian());
131         }
132
133     }
134 }
135
Popular Tags