Source code: com/arranger/jarl/test/PanelTest.java
1 package com.arranger.jarl.test;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.awt.event.WindowAdapter;
6 import java.awt.event.WindowEvent;
7 import java.awt.geom.Ellipse2D;
8 import java.awt.geom.Rectangle2D;
9
10 /**
11 * PanelTest created on Mar 3, 2003
12 */
13 public class PanelTest {
14
15 public static void main(String[] args) {
16 openInJFrame(new ShapeExample(), 380, 400);
17 }
18
19 public static class ShapeExample extends JPanel {
20
21 private Ellipse2D.Double circle =
22 new Ellipse2D.Double(10, 10, 350, 350);
23 private Rectangle2D.Double square =
24 new Rectangle2D.Double(10, 10, 350, 350);
25
26 public void paintComponent(Graphics g) {
27 clear(g);
28 Graphics2D g2d = (Graphics2D)g;
29 g2d.fill(circle);
30 g2d.draw(square);
31 }
32
33 // super.paintComponent clears offscreen pixmap,
34 // since we're using double buffering by default.
35
36 protected void clear(Graphics g) {
37 super.paintComponent(g);
38 }
39 }
40
41 /** Tell system to use native look and feel, as in previous
42 * releases. Metal (Java) LAF is the default otherwise.
43 */
44
45 public static void setNativeLookAndFeel() {
46 try {
47 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
48 } catch (Exception e) {
49 System.out.println("Error setting native LAF: " + e);
50 }
51 }
52
53 /** A simplified way to see a JPanel or other Container.
54 * Pops up a JFrame with specified Container as the content pane.
55 */
56
57 public static JFrame openInJFrame(Container content,
58 int width,
59 int height,
60 String title,
61 Color bgColor) {
62 JFrame frame = new JFrame(title);
63 frame.setBackground(bgColor);
64 content.setBackground(bgColor);
65 frame.setSize(width, height);
66 frame.setContentPane(content);
67 frame.addWindowListener(new ExitListener());
68 frame.setVisible(true);
69 return frame;
70 }
71
72 /** Uses Color.white as the background color. */
73
74 public static JFrame openInJFrame(Container content,
75 int width,
76 int height,
77 String title) {
78 return (openInJFrame(content, width, height, title, Color.white));
79 }
80
81 /** Uses Color.white as the background color, and the
82 * name of the Container's class as the JFrame title.
83 */
84
85 public static JFrame openInJFrame(Container content,
86 int width,
87 int height) {
88 return (openInJFrame(content, width, height,
89 content.getClass().getName(),
90 Color.white));
91 }
92
93 protected static class ExitListener extends WindowAdapter {
94
95 public void windowClosing(WindowEvent event) {
96 System.exit(0);
97 }
98 }
99 }