home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / ed8n1t2i / controlpanel.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  11.4 KB  |  284 lines

  1. //****************************************************************************
  2. // ---- version information ----
  3. //
  4. // ControlPanel.java     v 1.00 b1
  5. // Written by:           I. van Rienen / E-mail ivr@bart.nl
  6. // URL:                  http://www/bart.nl/~ivr
  7. // Initial release:
  8. // Released in public domain:
  9. //
  10. // ---- Description ----
  11. // Java class containing methods for a creating and maintaining the
  12. // Controlpanel which contaings the imagebuttons
  13. //
  14. // This program and the Java source is in the public domain.
  15. // Permission to use, copy, modify, and distribute this software
  16. // and its documentation for NON-COMMERCIAL purposes and
  17. // without fee is hereby granted.
  18. //
  19. //    Copyright 1996
  20. //
  21. //    Iwan van Rienen
  22. //    Joan Maetsuyckerstr. 145
  23. //    2593 ZG  The Hague
  24. //    The Netherlands
  25. //
  26. // I am not responsible for any bugs in this program and
  27. // possible damage to hard- or software when using this program.
  28. //****************************************************************************
  29. import java.awt.*;
  30. import java.awt.image.*;
  31. import java.util.Vector;
  32.  
  33. class ControlPanel extends Canvas {
  34.  
  35.     Vector buttons;
  36.     DigSim applet;
  37.     Image ImageBuffer = null;
  38.     Image CopyImage = null;
  39.     Graphics cig;
  40.     int ButtonOffset = 0;
  41.     ImageButton PressedButton = null;
  42.     boolean ImageButtonsDisabled = false;
  43.  
  44. //----------------------------------------------------------------------------
  45. // The constructor of the control panel.
  46. //----------------------------------------------------------------------------
  47.     public ControlPanel(DigSim app) {
  48.         applet = app;
  49.         buttons = new Vector();
  50.         LoadButtonsImage();
  51.         buttons.addElement (new ImageButton ("New", 0));
  52.         buttons.addElement (new ImageButton ("Open", 24));
  53.         buttons.addElement (new ImageButton ("Save", 48));
  54.         buttons.addElement (new ImageButton ("Cut", 80));
  55.         buttons.addElement (new ImageButton ("Copy", 104));
  56.         buttons.addElement (new ImageButton ("Paste", 128));
  57.         buttons.addElement (new ImageButton ("Pointer", 160));
  58.         buttons.addElement (new ImageButton ("Wire", 184));
  59.         buttons.addElement (new ImageButton ("Junction", 208));
  60.         buttons.addElement (new ImageButton ("Text", 232));
  61.         buttons.addElement (new ImageButton ("Simulate", 264));
  62.         buttons.addElement (new ImageButton ("Help", 296));
  63.         if (ImageButtonsDisabled) {
  64.             resize (size().width, 36);
  65.         } else {
  66.             resize (size().width, 4);
  67.         }
  68.      }
  69.  
  70. //----------------------------------------------------------------------------
  71. // Calculate the preferred size of the control panel
  72. //----------------------------------------------------------------------------
  73.     public Dimension preferredSize() {
  74.         // System.out.println ("ControlPanel preferredSize()");
  75.         int w = applet.size().width;
  76.         if (ImageButtonsDisabled) {
  77.             return new Dimension(w, 4);
  78.         }
  79.         return new Dimension(w, 36);
  80.     }
  81.  
  82. //----------------------------------------------------------------------------
  83. // Get the imagebutton with the specified Name
  84. //----------------------------------------------------------------------------
  85.     public ImageButton getButton(String Name) {
  86.         ImageButton TempButton;
  87.  
  88.         for (int ix = 0; ix < buttons.size(); ix++) {
  89.             TempButton = (ImageButton) buttons.elementAt(ix);
  90.             if (TempButton.getName().equals(Name)) {
  91.                 return TempButton;
  92.             }
  93.         }
  94.         return null;
  95.     }
  96.  
  97. //----------------------------------------------------------------------------
  98. // Enable the button with the specified name
  99. //----------------------------------------------------------------------------
  100.     public void EnableButton(String Name) {
  101.         ImageButton TempButton = getButton (Name);
  102.         if (TempButton != null) TempButton.Enable();
  103.         repaint();
  104.     }
  105.  
  106. //----------------------------------------------------------------------------
  107. // Disable the button with the specified name
  108. //----------------------------------------------------------------------------
  109.     public void DisableButton(String Name) {
  110.         ImageButton TempButton = getButton (Name);
  111.         if (TempButton != null) TempButton.Disable();
  112.         repaint();
  113.     }
  114.  
  115. //----------------------------------------------------------------------------
  116. // Select the button with the specified name
  117. //----------------------------------------------------------------------------
  118.     public void SelectButton(String Name) {
  119.         ImageButton TempButton = getButton (Name);
  120.         if (TempButton != null) TempButton.Select();
  121.         repaint();
  122.     }
  123.  
  124. //----------------------------------------------------------------------------
  125. // Unselect the button with the specified name
  126. //----------------------------------------------------------------------------
  127.     public void UnselectButton(String Name) {
  128.         ImageButton TempButton = getButton (Name);
  129.         if (TempButton != null) TempButton.Unselect();
  130.         repaint();
  131.     }
  132.  
  133. //----------------------------------------------------------------------------
  134. // Enable all buttons in the control panel
  135. //----------------------------------------------------------------------------
  136.      public void EnableAllButtons() {
  137.         EnableButton("Pointer");
  138.         EnableButton("Wire");
  139.         EnableButton("Junction");
  140.         EnableButton("Text");
  141.         EnableButton("Help");
  142.         EnableButton("Simulate");
  143.         EnableButton("New");
  144.         if (applet.EnableFileOperations) {
  145.             EnableButton("Open");
  146.             EnableButton("Save");
  147.         }
  148.         SelectButton("Pointer");
  149.     }
  150.  
  151. //----------------------------------------------------------------------------
  152. // Draw all imagebuttons
  153. //----------------------------------------------------------------------------
  154.     public void DrawButtons(Graphics g) {
  155.         ButtonOffset = (size().width - CopyImage.getWidth(this) ) / 2; // Center buttons
  156.         ImageButton TempButton;
  157.  
  158.         for (int ix = 0; ix < buttons.size(); ix++) {
  159.             TempButton = (ImageButton) buttons.elementAt(ix);
  160.             TempButton.Draw(cig);
  161.         }
  162.  
  163.         Graphics ibg = ImageBuffer.getGraphics();
  164.         ibg.drawImage (CopyImage, 0, 0, this);
  165.         g.drawImage(ImageBuffer, ButtonOffset, 4, this);
  166.     }
  167.  
  168. //----------------------------------------------------------------------------
  169. // The user pressed a mouse button
  170. //----------------------------------------------------------------------------
  171.     public boolean mouseDown(Event event, int x, int y) {
  172.         ImageButton TempButton;
  173.          x -= ButtonOffset;
  174.          y -= 4;
  175.  
  176.         for (int ix = 0; ix < buttons.size(); ix++) {
  177.             TempButton = (ImageButton) buttons.elementAt(ix);
  178.             if (TempButton.CheckIfPressed (x, y)) {
  179.                 PressedButton = TempButton;
  180.                 repaint();
  181.                 return true;
  182.             }
  183.         }
  184.         return true;
  185.     }
  186.  
  187. //----------------------------------------------------------------------------
  188. // The user released a mouse button
  189. //----------------------------------------------------------------------------
  190.     public boolean mouseUp(Event event, int x, int y) {
  191.         if (PressedButton != null) {
  192.             PressedButton.Select();
  193.             repaint();
  194.             ButtonPressed(PressedButton.getName());
  195.             PressedButton = null;
  196.         }
  197.         return true;
  198.     }
  199.  
  200. //----------------------------------------------------------------------------
  201. // Draw a new off-screen image and copy it to the screen
  202. //----------------------------------------------------------------------------
  203.     public synchronized void update (Graphics g) {
  204.         g.setColor (Color.gray);
  205.         g.drawLine (0, 0, size().width, 0);
  206.         if (!ImageButtonsDisabled) g.drawLine (0, 30, size().width, 30);
  207.         g.setColor (Color.white);
  208.         g.drawLine (0, 1, size().width, 1);
  209.         if (!ImageButtonsDisabled) g.drawLine (0, 31, size().width, 31);
  210.  
  211.         if (CopyImage != null && !ImageButtonsDisabled) DrawButtons(g);
  212.     }
  213.  
  214. //----------------------------------------------------------------------------
  215. // Draw a new off-screen image and copy it to the screen
  216. //----------------------------------------------------------------------------
  217.     public synchronized void paint (Graphics g) {
  218.         update (g);
  219.     }
  220.  
  221. //----------------------------------------------------------------------------
  222. // Load the master image containing all image buttons
  223. //----------------------------------------------------------------------------
  224.     public void LoadButtonsImage() {
  225.         Image ButtonsImage;
  226.         MediaTracker tracker = new MediaTracker(this);
  227.         ButtonsImage = applet.getImage(applet.getDocumentBase(), "images/allbuttons.gif");
  228.         tracker.addImage(ButtonsImage, 0);
  229.  
  230.         try {
  231.             Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  232.             tracker.waitForAll();
  233.         } catch(Exception e) {
  234.             String message = e.toString();
  235.             String DlgButtons[] = { "OK" };
  236.             SimpleDialog ExceptionDialog = new SimpleDialog(null, "Reading imagebuttons", message, DlgButtons, 1, 0, 0, SimpleDialog.IMAGE_STOP);
  237.             return;
  238.         }
  239.         if (tracker.isErrorAny()) {
  240.             ImageButtonsDisabled = true;
  241.             String message = "Can't read images/allbuttons.gif ImageButtons will be disabled";
  242.             String DlgButtons[] = { "OK" };
  243.             SimpleDialog ErrorDialog = new SimpleDialog(null, "Error while reading imagebuttons", message, DlgButtons, 1, 0, 0, SimpleDialog.IMAGE_STOP);
  244.             return;
  245.         }
  246.         ImageBuffer = applet.createImage (ButtonsImage.getWidth(this), 24);
  247.         CopyImage = applet.createImage (ButtonsImage.getWidth(this), ButtonsImage.getHeight(this));
  248.         cig = CopyImage.getGraphics();
  249.         cig.drawImage (ButtonsImage, 0, 0, this);
  250.    }
  251.  
  252. //----------------------------------------------------------------------------
  253. // The user pressed a button.
  254. //----------------------------------------------------------------------------
  255.     public void ButtonPressed(String PressedButton) {
  256.         if (ImageButtonsDisabled) return;
  257.  
  258.         if (PressedButton.equals("Pointer")) {
  259.             applet.UserWantsPointer();
  260.         } else if (PressedButton.equals("Wire")) {
  261.             applet.UserWantsWireDrawing();
  262.         } else if (PressedButton.equals("Junction")) {
  263.             applet.UserWantsJunctionDrawing();
  264.         } else if (PressedButton.equals("Text")) {
  265.             applet.UserWantsTextDrawing();
  266.         } else if (PressedButton.equals("Help")) {
  267.             applet.UserWantsHelp();
  268.         } else if (PressedButton.equals("Simulate")) {
  269.             applet.UserWantsSimulate();
  270.         } else if (PressedButton.equals("New")) {
  271.             applet.UserWantsNewSchematic();
  272.         } else if (PressedButton.equals("Open")) {
  273.             applet.UserWantsOpenSchematic();
  274.         } else if (PressedButton.equals("Save")) {
  275.             applet.UserWantsSaveSchematic();
  276.         } else if (PressedButton.equals("Copy")) {
  277.             applet.UserWantsCopySchematic();
  278.         } else if (PressedButton.equals("Paste")) {
  279.             applet.UserWantsPasteSchematic();
  280.         } else if (PressedButton.equals("Cut")) {
  281.             applet.UserWantsCutSchematic();
  282.         }
  283.     }
  284. }