Ticket #2163: GlassPaneDemo.java

File GlassPaneDemo.java, 7.4 KB (added by xeen, 14 years ago)
Line 
1/*
2 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of Oracle or the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32
33import javax.swing.*;
34import javax.swing.event.*;
35import java.awt.*;
36import java.awt.event.ItemEvent;
37import java.awt.event.ItemListener;
38import java.awt.event.MouseEvent;
39import javax.swing.event.MouseInputAdapter;
40import java.awt.image.*;
41
42/** An application that requires no other files. */
43public class GlassPaneDemo {
44 static private MyGlassPane myGlassPane;
45
46 private static void createAndShowGUI() {
47 //Create and set up the window.
48 JFrame frame = new MonitoringFrame("GlassPaneDemo");
49 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
50
51 //Set up the content pane, where the "main GUI" lives.
52 Container contentPane = frame.getContentPane();
53 contentPane.setLayout(new FlowLayout());
54 contentPane.add(new JButton("Button 1"));
55 for(int i = 0; i < 10000; i++) {
56 contentPane.add(new JButton("Button 2"));}
57
58 //Set up the glass pane, which appears over both menu bar
59 //and content pane and is an item listener on the change
60 //button.
61 myGlassPane = new MyGlassPane(frame.getContentPane());
62 frame.setGlassPane(myGlassPane);
63 myGlassPane.setVisible(true);
64
65 frame.setCursor(MyGlassPane.blankCursor);
66
67 //Show the window.
68 frame.pack();
69 frame.setVisible(true);
70 }
71
72 public static void main(String[] args) {
73 createAndShowGUI();
74 }
75}
76
77/**
78 * We have to provide our own glass pane so that it can paint.
79 */
80class MyGlassPane extends JComponent implements MouseInputListener {
81 Point point;
82 Container contentPane;
83 double lastPaint = 0;
84
85 final static Image cursorImg = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(0, 0, new int[0], 0, 0));
86 public final static Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor");
87
88
89 protected void paintComponent(Graphics g) {
90 if (point != null) {
91 g.setColor(Color.red);
92 g.fillOval(point.x - 10, point.y - 10, 20, 20);
93 if(lastPaint != 0)
94 System.out.println("@" + (System.currentTimeMillis() - lastPaint));
95 lastPaint = System.currentTimeMillis();
96 }
97 g.dispose();
98 }
99
100 public void setPoint(Point p) {
101
102 point = p;
103 this.repaint();
104
105 }
106
107 public MyGlassPane(Container contentPane) {
108 super();
109 setOpaque(false);
110 this.contentPane = contentPane;
111 addMouseListener(this);
112 addMouseMotionListener(this);
113 }
114
115
116 public void mouseMoved(MouseEvent e) {
117 redispatchMouseEvent(e, false);
118 }
119
120 public void mouseDragged(MouseEvent e) {
121 redispatchMouseEvent(e, false);
122 }
123
124 public void mouseClicked(MouseEvent e) {
125 redispatchMouseEvent(e, false);
126 }
127
128 public void mouseEntered(MouseEvent e) {
129 redispatchMouseEvent(e, false);
130 }
131
132 public void mouseExited(MouseEvent e) {
133 redispatchMouseEvent(e, false);
134 setPoint(null);
135 repaint();
136 }
137
138 public void mousePressed(MouseEvent e) {
139 redispatchMouseEvent(e, false);
140 }
141
142 public void mouseReleased(MouseEvent e) {
143 redispatchMouseEvent(e, true);
144 }
145
146 //A basic implementation of redispatching events.
147 private void redispatchMouseEvent(MouseEvent e,
148 boolean repaint) {
149 Point glassPanePoint = e.getPoint();
150/*
151 Container container = contentPane;
152 Point containerPoint = SwingUtilities.convertPoint(
153 this,
154 glassPanePoint,
155 contentPane);
156
157 contentPane.dispatchEvent(new MouseEvent(contentPane,
158 e.getID(),
159 e.getWhen(),
160 e.getModifiers(),
161 containerPoint.x,
162 containerPoint.y,
163 e.getClickCount(),
164 e.isPopupTrigger()));
165*/
166
167
168 //The mouse event is probably over the content pane.
169 //Find out exactly which component it's over.
170/*
171 Component component =
172 SwingUtilities.getDeepestComponentAt(
173 container,
174 containerPoint.x,
175 containerPoint.y);
176*/
177
178
179/*
180 if (component != null) {
181 //Forward events over the check box.
182 Point componentPoint = SwingUtilities.convertPoint(
183 this,
184 glassPanePoint,
185 component);
186 component.dispatchEvent(new MouseEvent(component,
187 e.getID(),
188 e.getWhen(),
189 e.getModifiers(),
190 componentPoint.x,
191 componentPoint.y,
192 e.getClickCount(),
193 e.isPopupTrigger()));
194 }
195*/
196
197
198 //Update the glass pane if requested.
199 setPoint(glassPanePoint);
200 }
201}
202
203
204
205
206class MonitoringFrame extends JFrame{
207
208 /** Creates a new instance of MyFrame */
209 public MonitoringFrame() {
210 super();
211 }
212 public MonitoringFrame(String title) {
213 super(title);
214 }
215
216 public void paint(Graphics g)
217 {
218 long start = System.currentTimeMillis();
219 super.paint(g);
220 long end = System.currentTimeMillis();
221 System.out.println("Paint time : " + (end-start));
222 }
223 public void repaint()
224 {
225 long start = System.currentTimeMillis();
226 super.repaint();
227 long end = System.currentTimeMillis();
228 System.out.println("Repaint time : " + (end-start));
229 }
230}