home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / netrexx.zip / NetRexx / Buttons.nrx < prev    next >
Text File  |  1998-04-15  |  5KB  |  96 lines

  1. /* A sample graphics stand-alone application for the Java 1.1 platform,
  2.      using dependent and adapter classes.
  3.    This draws two buttons in a frame window on the screen, and sets
  4.      the background colour whenever one is pushed.
  5.    An off-screen image is used to show how one is handled, though it is
  6.      not needed for this simple effect.  The image reverts to default
  7.      background when the window is resized.
  8.    */
  9. options binary                     -- optional, for speed
  10.  
  11. /* ------------------------------------------------------------------ */
  12. /* The main (parent) class                                            */
  13. /* ------------------------------------------------------------------ */
  14. class Buttons adapter extends Frame-
  15.                       implements WindowListener, ComponentListener
  16.   properties shared
  17.     shadow=Image                   -- offscreen image
  18.  
  19.   properties constant
  20.     mywidth=200                    -- our shape
  21.     myheight=300                   -- ..
  22.     glass=Toolkit.getDefaultToolkit.getScreenSize -- screen geometry
  23.  
  24.   /* The 'main' method is called when this class is started as an application */
  25.   method main(s=String[]) static
  26.     frame=Buttons("My Buttons" Rexx(s))           -- make a titled frame
  27.     -- now size and place it mid-screen
  28.     frame.setBounds((glass.width-mywidth)%2,(glass.height-myheight)%2,-
  29.                     mywidth, myheight)
  30.     frame.show                                    -- and make it visible
  31.  
  32.   /* The constructor for Buttons */
  33.   method Buttons(s=String)
  34.     super(s)                       -- pass title to superclass
  35.     setLayout(FlowLayout())        -- set component layout scheme
  36.     add(Buttons.Left())            -- add one button ..
  37.     add(Buttons.Right())           -- .. and the other
  38.     addWindowListener(this)        -- please tell us about Window events ..
  39.     addComponentListener(this)     -- .. and component events
  40.  
  41.   /* newimage -- make a new offscreen image */
  42.   method newimage
  43.     shadow=createImage(getSize.width, getSize.height)
  44.  
  45.   /* update -- called when the window is updated */
  46.   /* paint  -- called when the window needs to be redrawn */
  47.   method update(g=Graphics)        -- we supply this to avoid flicker
  48.     paint(g)
  49.   method paint(g=Graphics)
  50.     if shadow=null then newimage   -- ensure we have an image
  51.     g.drawImage(shadow, 0, 0, this)-- copy the image to screen
  52.  
  53.   /* componentResized -- called after graphics area resized */
  54.   method componentResized(e=ComponentEvent)
  55.     newimage                       -- make new sized image
  56.  
  57.   /* windowClosing -- called when the window is closed */
  58.   -- We need to handle this to end the program
  59.   method windowClosing(e=WindowEvent)
  60.     exit
  61.  
  62. /* ------------------------------------------------------------------ */
  63. /* A dependent class for a button                                     */
  64. /* ------------------------------------------------------------------ */
  65. class Buttons.Left dependent extends Button implements ActionListener
  66.   method Left                 -- construct the button
  67.     super("Green")            -- we choose the label
  68.     addActionListener(this)   -- listen for action events
  69.  
  70.   method actionPerformed(a=ActionEvent) -- Button pressed
  71.     g=parent.shadow.getGraphics         -- get the image
  72.     g.setColor(Color.green)             -- choose a colour
  73.     -- now colour the image
  74.     g.fillRect(0, 0, parent.getSize.width, parent.getSize.height)
  75.     parent.repaint                      -- and request redraw
  76.  
  77. /* ------------------------------------------------------------------ */
  78. /* A dependent class for a button                                     */
  79. /* ------------------------------------------------------------------ */
  80. -- (If many buttons are similar, it could be worth making a shared
  81. -- superclass.  Equally, a method on the parent object could be called
  82. -- to set and fill the new colour, for example, use:
  83. --   parent.newColor(Color.red)
  84. -- to call a newColor method in the Buttons class on the parent object.)
  85. class Buttons.Right dependent extends Button implements ActionListener
  86.   method Right                -- construct the button
  87.     super("Red")              -- we choose the label
  88.     addActionListener(this)   -- listen for action events
  89.  
  90.   method actionPerformed(a=ActionEvent) -- Button pressed
  91.     g=parent.shadow.getGraphics         -- get the image
  92.     g.setColor(Color.red)               -- choose a colour
  93.     -- now colour the image
  94.     g.fillRect(0, 0, parent.getSize.width, parent.getSize.height)
  95.     parent.repaint                      -- and request redraw
  96.