KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtriver > open > vectorvisuals > example > mouse > ObjectDragging


1 /*
2  * ObjectDragging.java
3  *
4  * Created on 10 August 2004, 01:12
5  */

6
7 package com.thoughtriver.open.vectorvisuals.example.mouse;
8
9 import java.awt.*;
10 import java.awt.geom.*;
11
12 import javax.swing.*;
13
14 import com.thoughtriver.open.vectorvisuals.*;
15 import com.thoughtriver.open.vectorvisuals.ui.*;
16
17 /**
18  * This small application illustrates how to create a Vector Visuals drawing
19  * area (a <CODE>VVDisplay</CODE>) and make the objects on it draggable. In
20  * other words, the entire viewing area cannot be moved, but the objects on it
21  * can be shifted around by clicking and dragging the mouse on them.
22  *
23  * @author Brandon Franklin
24  * @version $Date: 2006/11/25 09:21:56 $
25  */

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

34     public ObjectDragging() {
35         vvDisplay = new VVDisplay(new VVPanel());
36     }
37
38     /**
39      * Starts the example.
40      */

41     public void run() {
42         JFrame frame = new JFrame("Vector Visuals Object Dragging Example");
43         frame.setSize(640, 480);
44         frame.getContentPane().setLayout(new BorderLayout());
45         frame.getContentPane().add(vvDisplay.getViewPane(), BorderLayout.CENTER);
46         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
47
48         frame.setVisible(true);
49
50         // Set up the panel itself
51
vvDisplay.getViewPane().setBackground(Color.DARK_GRAY);
52         vvDisplay.setWorldViewScale(1);
53
54         // Create a nice red rectangle
55
Brush rectLineBrush1 = new Brush(Color.WHITE, null, null);
56         Brush rectFillBrush1 = new Brush(Color.RED, null, null);
57         VisualObject rectangle1 = new VisualObject(new Rectangle2D.Double(120, 150, 115, 40), rectLineBrush1, rectFillBrush1);
58
59         // Create a nice yellow rectangle
60
Brush rectLineBrush2 = new Brush(Color.BLACK, null, null);
61         Brush rectFillBrush2 = new Brush(Color.YELLOW, null, null);
62         VisualObject rectangle2 = new VisualObject(new Rectangle2D.Double(240, 75, 40, 90), rectLineBrush2, rectFillBrush2);
63
64         // Create a nice blue rectangle
65
Brush rectLineBrush3 = new Brush(Color.YELLOW, null, null);
66         Brush rectFillBrush3 = new Brush(Color.BLUE, null, null);
67         VisualObject rectangle3 = new VisualObject(new Rectangle2D.Double(380, 250, 60, 60), rectLineBrush3, rectFillBrush3);
68
69         // Create some descriptive text
70
Brush brush = new Brush(Color.YELLOW, null, null);
71         VisualTextObject textObj1 = new VisualTextObject("Click and Drag an object to move it", null, null, brush);
72         VisualTextObject textObj2 = new VisualTextObject("(You can even move this text!)", null, null, brush);
73
74         // Set the location and size of the objects
75
AffineTransform transform = textObj1.getTransform();
76         transform.scale(2.0, 2.0);
77         transform.translate(15, 120);
78         textObj1.setTransform(transform);
79
80         transform = textObj2.getTransform();
81         transform.scale(2.0, 2.0);
82         transform.translate(100, 180);
83         textObj2.setTransform(transform);
84
85         // Display the objects on the VVPanel
86
vvDisplay.addObject(rectangle1);
87         vvDisplay.addObject(rectangle2);
88         vvDisplay.addObject(rectangle3);
89         vvDisplay.addObject(textObj1);
90         vvDisplay.addObject(textObj2);
91
92         // Add a specialized mouse listener
93
VVMouseDragListener listener = new VVMouseDragListener(vvDisplay);
94         vvDisplay.addVVMouseListener(listener);
95
96         // Ensure that the viewport itself is not draggable
97
listener.setViewportDraggable(false);
98
99         // Make the objects draggable
100
listener.setObjectDraggable(rectangle1, true);
101         listener.setObjectDraggable(rectangle2, true);
102         listener.setObjectDraggable(rectangle3, true);
103         listener.setObjectDraggable(textObj2, true);
104
105     }
106
107     /**
108      * Instantiates a <CODE>ObjectDragging</CODE> and executes it.
109      *
110      * @param args the command line arguments, ignored here
111      */

112     public static void main(final String JavaDoc[] args) {
113         Runnable JavaDoc example = new ObjectDragging();
114         example.run();
115     }
116
117 }
118
Popular Tags