26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

742 Graphical User Interface Components: Part 2 Chapter 13<br />

java DrawShapes 600 400<br />

contains two command-line arguments—600 and 400—that specify the width and height<br />

of the application window. <strong>Java</strong> passes the command-line arguments <strong>to</strong> main as the array<br />

of Strings called args, which we have declared in the parameter list of every application’s<br />

main method, but not used until this point. The first argument after the application<br />

class name is the first String in the array args, and the length of the array is the <strong>to</strong>tal<br />

number of command-line arguments. Line 60 begins the definition of main and declares<br />

array args as an array of Strings that allows the application <strong>to</strong> access the command-line<br />

arguments. Line 62 defines variables width and height that are used <strong>to</strong> specify the size<br />

of the application window.<br />

1 // Fig. 13.9: DrawShapes\.java<br />

2 // Draw random lines, rectangles and ovals<br />

3<br />

4 // <strong>Java</strong> core packages<br />

5 import java.awt.*;<br />

6 import java.awt.event.*;<br />

7<br />

8 // <strong>Java</strong> extension packages<br />

9 import javax.swing.*;<br />

10<br />

11 public class DrawShapes extends JApplet {<br />

12 private JBut<strong>to</strong>n choices[];<br />

13 private String names[] = { "Line", "Rectangle", "Oval" };<br />

14 private JPanel but<strong>to</strong>nPanel;<br />

15 private DrawPanel drawingPanel;<br />

16 private int width = 300, height = 200;<br />

17<br />

18 // initialize applet; set up GUI<br />

19 public void init()<br />

20 {<br />

21 // set up DrawPanel<br />

22 drawingPanel = new DrawPanel( width, height );<br />

23<br />

24 // create array of but<strong>to</strong>ns<br />

25 choices = new JBut<strong>to</strong>n[ names.length ];<br />

26<br />

27 // set up panel for but<strong>to</strong>ns<br />

28 but<strong>to</strong>nPanel = new JPanel();<br />

29 but<strong>to</strong>nPanel.setLayout(<br />

30 new GridLayout( 1, choices.length ) );<br />

31<br />

32 // set up but<strong>to</strong>ns and register their listeners<br />

33 But<strong>to</strong>nHandler handler = new But<strong>to</strong>nHandler();<br />

34<br />

35 for ( int count = 0; count < choices.length; count++ ) {<br />

36 choices[ count ] = new JBut<strong>to</strong>n( names[ count ] );<br />

37 but<strong>to</strong>nPanel.add( choices[ count ] );<br />

38 choices[ count ].addActionListener( handler );<br />

39 }<br />

Fig. Fig. 13.9 13.9 Creating a GUI-based application from an applet (part 1 of 4).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!