Graphics More AWT Components

J.E.D.I. mechanism has been added to the program yet. Youll learn about event handling in the next module.

7.3.2 Graphics

Several graphics method are found in the Graphics class. Here is the list of some of these methods. drawLine drawPolyline setColor fillRect drawPolygon getFont drawRect fillPolygon setFont clearRect getColor drawString Table 16: Some methods of class Graphics Related to this class is the Color class, which has three constructors. Constructor Format Description Colorint r, int g, int b Integer value is from 0 to 255. Colorfloat r, float g, float b Float value is from 0.0 to 1.0. Colorint rgbValue Value range from 0 to 2 24 -1 black to white. Red: bits 16-23 Green: bits 8-15 Blue: bits 0-7 Table 17: Color constructors Here is a sample program that uses some of the methods in the Graphics class. import java.awt.; public class GraphicsPanel extends Panel { GraphicsPanel { setBackgroundColor.black; constant in Color class } public void paintGraphics g { g.setColornew Color0,255,0; green g.setFontnew FontHelvetica,Font.PLAIN,16; g.drawStringHello GUI World, 30, 100; g.setColornew Color1.0f,0,0; red g.fillRect30, 100, 150, 10; } public static void mainString args[] { Frame f = new FrameTesting Graphics Panel; GraphicsPanel gp = new GraphicsPanel; f.addgp; f.setSize600, 300; f.setVisibletrue; } } Introduction to Programming II Page 90 J.E.D.I. For a panel to become visible, it should be placed inside a visible window like a frame. Running the given code gives the following expected output: Figure 7.2: Running GraphicsPanel Introduction to Programming II Page 91 J.E.D.I.

7.3.3 More AWT Components

Here is a list of AWT controls. Controls are components such as buttons or text fields that allow the user to interact with a GUI application. These are all subclasses of the Component class. Label Button Choice TextField Checkbox List TextArea CheckboxGroup Scrollbar Table 18: AWT Components The following program creates a frame with controls contained in it. import java.awt.; class FrameWControls extends Frame { public static void mainString args[] { FrameWControls fwc = new FrameWControls; fwc.setLayoutnew FlowLayout; more on this later fwc.setSize600, 600; fwc.addnew ButtonTest Me; fwc.addnew LabelLabe; fwc.addnew TextField; CheckboxGroup cbg = new CheckboxGroup; fwc.addnew Checkboxchk1, cbg, true; fwc.addnew Checkboxchk2, cbg, false; fwc.addnew Checkboxchk3, cbg, false; List list = new List3, false; list.addMTV; list.addV; fwc.addlist; Choice chooser = new Choice; chooser.addAvril; chooser.addMonica; chooser.addBritney; fwc.addchooser; fwc.addnew Scrollbar; fwc.setVisibletrue; } } Introduction to Programming II Page 92 J.E.D.I. Here is a sample screen shot of running FrameWControls: Introduction to Programming II Page 93 J.E.D.I. Figure 7.3: Running FrameWControls Introduction to Programming II Page 94 J.E.D.I.

7.4 Layout Managers