Java Reference
In-Depth Information
figure B.8
Basic canvas shown
in top left-hand
corner of Figure B.1
1 class GUICanvas extends JPanel
2 {
3 public void setParams( String aShape, String aColor, int x,
4 int y, int size, boolean fill )
5 {
6 theShape = aShape;
7 theColor = aColor;
8 xcoor = x;
9 ycoor = y;
10 theSize = size;
11 fillOn = fill;
12 repaint( );
13 }
14
15 public void paintComponent( Graphics g )
16 {
17 super.paintComponent( g );
18 if( theColor.equals( "red" ) )
19 g.setColor( Color.red );
20 else if( theColor.equals( "blue" ) )
21 g.setColor( Color.blue );
22
23 theWidth = 25 * ( theSize + 1 );
24
25 if( theShape.equals( "Square" ) )
26 if( fillOn )
27 g.fillRect( xcoor, ycoor, theWidth, theWidth );
28 else
29 g.drawRect( xcoor, ycoor, theWidth, theWidth );
30 else if( theShape.equals( "Circle" ) )
31 if( fillOn )
32 g.fillOval( xcoor, ycoor, theWidth, theWidth );
33 else
34 g.drawOval( xcoor, ycoor, theWidth, theWidth );
35 }
36
37 private String theShape = "";
38 private String theColor = "";
39 private int xcoor;
40 private int ycoor;
41 private int theSize; // 0 = small, 1 = med, 2 = large
42 private boolean fillOn;
43 private int theWidth;
44 }
 
Search WWH ::




Custom Search