1+ package test ;
2+
3+ import java .awt .AlphaComposite ;
4+ import java .awt .BasicStroke ;
5+ import java .awt .Color ;
6+ import java .awt .Font ;
7+ import java .awt .Graphics ;
8+ import java .awt .Graphics2D ;
9+ import java .awt .GridLayout ;
10+ import java .awt .Image ;
11+ import java .awt .event .AdjustmentEvent ;
12+ import java .awt .event .AdjustmentListener ;
13+ import java .awt .event .FocusEvent ;
14+ import java .awt .event .FocusListener ;
15+ import java .awt .event .MouseEvent ;
16+ import java .awt .event .MouseListener ;
17+
18+ import javax .swing .JApplet ;
19+ import javax .swing .JButton ;
20+ import javax .swing .JLabel ;
21+ import javax .swing .JPanel ;
22+ import javax .swing .JScrollBar ;
23+ import javax .swing .JTextArea ;
24+ import javax .swing .JTextField ;
25+
26+ public class TApp2_Swing extends JApplet {
27+
28+ JTextArea ta ;
29+
30+ public void init () {
31+ setSize (400 , 400 );
32+ setFont (new Font (Font .MONOSPACED , Font .PLAIN , 14 ));
33+ setBackground (Color .yellow );
34+ setLayout (null );
35+ JPanel panel = new JPanel ();
36+ panel .setBounds (10 , 10 , 100 , 150 );
37+ add (panel );
38+ panel .setLayout (null );
39+ panel .setBackground (Color .red );
40+ panel .setForeground (Color .green );
41+ JTextField tf = new JTextField ("Text" );
42+ tf .setBounds (5 , 5 , 50 , 24 );
43+ tf .setFont (new Font (Font .SANS_SERIF , Font .PLAIN , 20 ));
44+ tf .addFocusListener (new FocusListener () {
45+
46+ @ Override
47+ public void focusGained (FocusEvent e ) {
48+ System .out .println ("tf " + e .paramString ());
49+ }
50+
51+ @ Override
52+ public void focusLost (FocusEvent e ) {
53+ System .out .println ("tf " + e .paramString ());
54+ }
55+
56+ });
57+ panel .add (tf );
58+ // Label label = new Label("blue", Label.RIGHT);
59+ // label.setBounds(10, 10, 50, 60);
60+ // panel.add(label);
61+ // JPanel panel2 = new JPanel();
62+ // panel2.setLayout(new GridLayout(2, 1));
63+ // Label label2 = new Label("1");
64+ // label2.setAlignment(Label.CENTER);
65+ // panel2.add(label2);
66+ // panel2.add(new JLabel("2"));
67+ // panel2.setBounds(200, 150, 100, 150);
68+ //// add(panel2);
69+ //
70+ // // the scrolling to the bottom is only with TextArea, not JTextArea
71+ // and then only if the append is AFTER the add
72+ ta = new JTextArea ("A text\n with some\n lines and\n no content." );
73+ add (ta );
74+ ta .setBounds (200 , 70 , 200 ,200 );
75+ ta .setFont (new Font (Font .DIALOG , Font .BOLD , 20 ));
76+ ta .append ("A text\n with some\n lines and\n no content." );
77+ ta .addFocusListener (new FocusListener () {
78+
79+ @ Override
80+ public void focusGained (FocusEvent e ) {
81+ System .out .println ("ta " + e .paramString ());
82+ }
83+
84+ @ Override
85+ public void focusLost (FocusEvent e ) {
86+ System .out .println ("ta " + e .paramString ());
87+ }
88+
89+ });
90+
91+
92+ // TextArea ta = new TextArea("A text\nwith some\nlines and\n no content.");
93+ // JScrollPane sp = new JScrollPane(ta);
94+ // sp.setBounds(200, 70, 100, 80);
95+ // add(sp);
96+
97+
98+ // ta.getSelectionEnd();
99+ ta .setBackground (Color .red );
100+ JScrollBar sb = new JScrollBar (0 , 30 , 0 , 0 , 100 );
101+ sb .setBounds (300 , 20 , 100 , 20 );
102+ add (sb );
103+ sb .addAdjustmentListener (new AdjustmentListener () {
104+
105+ @ Override
106+ public void adjustmentValueChanged (AdjustmentEvent e ) {
107+ System .out .println ("TApp2 sb value " + e .getValue ());
108+ }
109+
110+ });
111+
112+ new TestGraphic2 (this ).testGraphic ();
113+ }
114+
115+ static class TestGraphic2 {
116+ private TApp2_Swing tApp2 ;
117+
118+ public TestGraphic2 (TApp2_Swing tApp2 ) {
119+ this .tApp2 = tApp2 ;
120+ }
121+
122+ static {
123+ ClassLoader .getSystemClassLoader ().setDefaultAssertionStatus (true );
124+ }
125+
126+ void testGraphic () {
127+ JButton b = new JButton ("g" );
128+ b .setBounds (300 ,300 ,20 ,20 );
129+ // images are created only when a button is displayable
130+ Image i1 = b .createImage (100 , 100 );
131+ System .out .println ("b.isDisplayable " + b .isDisplayable () + " " + i1 );
132+
133+ tApp2 .add (b );
134+ i1 = b .createImage (100 , 100 );
135+ System .out .println ("b.isDisplayable " + b .isDisplayable () + " " + i1 );
136+ b .setFont (new Font (Font .SERIF , Font .BOLD , 10 ));
137+ Graphics g = i1 .getGraphics ();
138+ Font f = g .getFont ();
139+ System .out .println (f );
140+
141+ // font/background/foreground are set when the graphic is created
142+ Font f0 = new Font (Font .DIALOG , Font .PLAIN , 30 );
143+ b .setFont (f0 );
144+ b .setBackground (Color .red );
145+ b .setForeground (Color .green );
146+ g = i1 .getGraphics ();
147+ b .setFont (new Font (Font .SERIF , Font .BOLD , 10 ));
148+ b .setBackground (Color .white );
149+ b .setForeground (Color .black );
150+ System .out .println ("img font=" + g .getFont ());
151+ System .out .println ("img bg=" + ((Graphics2D ) g ).getBackground ());
152+ System .out .println ("img fg=" + g .getColor ());
153+ assert (g .getFont ().equals (f0 ));
154+ assert (g .getColor () == Color .green );
155+ assert (((Graphics2D ) g ).getBackground () == Color .red );
156+ System .out .println ("Tapp2 OK" );
157+ }
158+ }
159+
160+ public void paint (Graphics g ) {
161+ super .paint (g );
162+ g .fillRect (20 , 330 , 100 , 10 );
163+ ((Graphics2D ) g ).setStroke ((new BasicStroke (0 )));
164+ g .drawRect (130 , 150 , 40 , 60 );
165+ g .fillRect (140 , 5 , 1 , 200 );
166+ g .drawLine (150 , 150 , 150 , 350 );
167+
168+ g .setColor (Color .blue );
169+ g .drawRoundRect (120 , 200 , 80 , 150 , 20 , 20 );
170+
171+ g .fillRoundRect (210 , 200 , 80 , 150 , 20 , 20 );
172+
173+ // test AlphaComposite.Clear
174+
175+ g .setColor (Color .red );
176+ g .fillRect (10 , 200 , 100 , 100 );
177+ ((Graphics2D ) g ).setComposite (AlphaComposite .Clear );
178+ // save the foreground color, but don't use it.
179+ g .setColor (Color .orange );
180+ // paints a black rectangle
181+ g .fillRect (10 , 200 , 100 , 100 );
182+ g .setPaintMode ();
183+ g .fillRect (20 , 220 , 100 , 100 );
184+
185+ // test g.clearRect();
186+
187+ g .clearRect (0 , 240 , 150 , 20 );
188+
189+ //
190+ //
191+ // Polygon poly = new Polygon();
192+ // poly.addPoint(10, 200);
193+ // poly.addPoint(110, 200);
194+ // poly.addPoint(110, 300);
195+ // poly.addPoint(10, 300);
196+ // g.setColor(Color.white);
197+ // g.fillPolygon(poly);
198+ // g.setColor(Color.blue);
199+ // g.fillPolygon(poly);
200+ //
201+
202+ // g.fillRect(10, 200, 100, 100);
203+ //
204+ //
205+ //
206+ //
207+ // g.setColor(Color.red);
208+ // g.fillOval(200, 10, 10, 10);
209+ // g.setColor(Color.white);
210+ // g.fillOval(200, 10, 10, 10);
211+ //
212+ // g.setColor(Color.red);
213+ // g.drawString("SwingJS", 200, 30);
214+ // g.setColor(Color.white);
215+ // g.drawString("SwingJS", 200, 30);
216+
217+
218+ addMouseListener (new MouseListener () {
219+
220+ @ Override
221+ public void mouseClicked (MouseEvent e ) {
222+ System .out .println (">>>>mouseClicked: " + e .getModifiers () + " " + e .getModifiersEx () + " " + e );
223+ }
224+
225+ @ Override
226+ public void mouseEntered (MouseEvent e ) {
227+ // TODO Auto-generated method stub
228+
229+ }
230+
231+ @ Override
232+ public void mouseExited (MouseEvent e ) {
233+ // TODO Auto-generated method stub
234+
235+ }
236+
237+ @ Override
238+ public void mousePressed (MouseEvent e ) {
239+ System .out .println (">>>>mousePressed: " + e .getModifiers () + " " + e .getModifiersEx () + " " + e );
240+ }
241+
242+ @ Override
243+ public void mouseReleased (MouseEvent e ) {
244+ System .out .println (">>>>mouseReleased: " + e .getModifiers () + " " + e .getModifiersEx () + " " + e );
245+ }
246+
247+ });
248+
249+ }
250+ }
0 commit comments