22
33import org .teachingextensions .approvals .lite .util .FrameCloser ;
44import org .teachingextensions .approvals .lite .util .WindowUtils ;
5+ import org .teachingextensions .approvals .lite .writers .ComponentApprovalWriter ;
56import org .teachingextensions .logo .utils .ColorUtils .PenColors ;
67import org .teachingextensions .logo .utils .EventUtils .LeftClickMouseAdapter ;
78import org .teachingextensions .logo .utils .EventUtils .MouseLeftClickListener ;
89import org .teachingextensions .logo .utils .EventUtils .MouseRightClickListener ;
910import org .teachingextensions .logo .utils .EventUtils .RightClickMouseAdapter ;
11+ import org .teachingextensions .logo .utils .InterfaceUtils .CanvasPanel ;
12+ import org .teachingextensions .logo .utils .InterfaceUtils .TurtleFrame ;
1013import org .teachingextensions .logo .utils .LineAndShapeUtils .ImageBackground ;
1114import org .teachingextensions .logo .utils .LineAndShapeUtils .Paintable ;
1215import org .teachingextensions .virtualproctor .VirtualProctorWeb ;
1316
1417import javax .swing .*;
1518import java .awt .*;
16- import java .util . ArrayList ;
19+ import java .awt . image . BufferedImage ;
1720
1821/**
1922 * <img src="http://ftpmirror.your.org/pub/wikimedia/images/wikibooks/de/2/2c/JPanel_Add_JButton_PAGE_END.JPG" style="text-align: left" alt="A window image" height="50" width="75" > Program Window
2023 * allows you to change the color of the background and more...
2124 */
22- @ SuppressWarnings ({"serial" })
23- public class ProgramWindow extends JPanel {
24- protected final ArrayList <Paintable > painters = new ArrayList <Paintable >();
25+ public class ProgramWindow {
26+ private final CanvasPanel panel = new CanvasPanel ();
2527 private JFrame frame ;
2628
2729 public ProgramWindow (String title ) {
2830 this ();
2931 this .frame = new JFrame (title );
30- this .frame .getContentPane ().add (this );
32+ this .frame .getContentPane ().add (this . panel );
3133 ProgramWindow .createStandardFrame (getFrame ());
3234 }
3335
3436 public ProgramWindow () {
35- setPreferredSize (new Dimension (627 , 442 ));
37+ this . panel . setPreferredSize (new Dimension (627 , 442 ));
3638 setColor (PenColors .Whites .White );
3739 }
3840
3941 public static void createStandardFrame (JFrame frame ) {
4042 WindowUtils .testFrame (frame , new VirtualProctorWeb (), new FrameCloser ());
4143 }
4244
43- public static Graphics2D configureGraphics2D (Graphics g ) {
44- Graphics2D g2d = (Graphics2D ) g .create ();
45- g2d .setRenderingHint (RenderingHints .KEY_ANTIALIASING , RenderingHints .VALUE_ANTIALIAS_ON );
46- g2d .setRenderingHint (RenderingHints .KEY_COLOR_RENDERING , RenderingHints .VALUE_COLOR_RENDER_QUALITY );
47- g2d .setRenderingHint (RenderingHints .KEY_STROKE_CONTROL , RenderingHints .VALUE_STROKE_PURE );
48- g2d .setRenderingHint (RenderingHints .KEY_DITHERING , RenderingHints .VALUE_DITHER_ENABLE );
49- return g2d ;
50- }
5145
5246 /**
5347 * Adds a button instance to a window
@@ -57,30 +51,17 @@ public static Graphics2D configureGraphics2D(Graphics g) {
5751 * A button instance
5852 */
5953 public void addButton (JButton button ) {
60- this .add (button );
54+ this .panel . add (button );
6155 }
6256
63- @ Override
64- public void paint (Graphics g ) {
65- super .paint (g );
66- if (this .painters == null ) {
67- return ;
68- }
69-
70- ArrayList <Paintable > toPaint = new ArrayList <>(this .painters );
71-
72- Graphics2D g2d = configureGraphics2D (g );
73- for (Paintable p : toPaint ) {
74- p .paint (g2d , this );
75- }
76- }
7757
7858 public void setColor (Color backgroundColor ) {
79- setBackground (backgroundColor );
59+ this . panel . setBackground (backgroundColor );
8060 }
8161
62+ @ Deprecated
8263 public ProgramWindow clearWindow () {
83- this .painters .clear ();
64+ this .panel .clear ();
8465 return this ;
8566 }
8667
@@ -92,7 +73,7 @@ public ProgramWindow clearWindow() {
9273 * A listener instance
9374 */
9475 public void addMouseRightClickListener (MouseRightClickListener listener ) {
95- addMouseListener (new RightClickMouseAdapter (listener ));
76+ this . panel . addMouseListener (new RightClickMouseAdapter (listener ));
9677 }
9778
9879 /**
@@ -103,7 +84,7 @@ public void addMouseRightClickListener(MouseRightClickListener listener) {
10384 * A listener instance
10485 */
10586 public void addMouseLeftClickListener (MouseLeftClickListener listener ) {
106- addMouseListener (new LeftClickMouseAdapter (listener ));
87+ this . panel . addMouseListener (new LeftClickMouseAdapter (listener ));
10788 }
10889
10990 /**
@@ -123,22 +104,58 @@ public JFrame getFrame() {
123104
124105 public void setWindowVisible (boolean b ) {
125106 this .frame .setVisible (b );
126- this .setVisible (b );
107+ this .panel . setVisible (b );
127108 }
128109
110+ @ Deprecated
129111 public ProgramWindow add (Paintable painter ) {
130- if (!this .painters .contains (painter )) {
131- this .painters .add (painter );
132- }
133-
112+ this .panel .add (painter );
134113 return this ;
135114 }
136115
116+ @ Deprecated
137117 public ProgramWindow remove (Paintable painter ) {
138- if (this .painters .contains (painter )) {
139- this .painters .remove (painter );
140- }
118+ this .panel .remove (painter );
119+ return this ;
120+ }
121+
122+
123+ public final BufferedImage getWindowImage () {
124+ return ComponentApprovalWriter .drawComponent (this .panel );
125+ }
126+
127+ public ProgramWindow addTo (TurtleFrame frame ) {
128+ frame .addContent (this .panel );
129+ return this ;
130+ }
131+
132+ public ProgramWindow repaint () {
133+ this .panel .repaint ();
134+ return this ;
135+ }
136+
137+ public ProgramWindow setVisible (boolean visible ) {
138+ this .panel .setVisible (visible );
139+ return this ;
140+ }
141+
142+ public ProgramWindow setCursor (int cursor ) {
143+ this .setCursor (Cursor .getPredefinedCursor (cursor ));
144+ return this ;
145+ }
141146
147+ public ProgramWindow setCursor (Cursor predefinedCursor ) {
148+ this .panel .setCursor (predefinedCursor );
149+ return this ;
150+ }
151+
152+ public CanvasPanel getCanvas () {
153+ return this .panel ;
154+ }
155+
156+ public ProgramWindow setBackground (Color color ) {
157+ this .panel .setBackground (color );
142158 return this ;
143159 }
144160}
161+
0 commit comments