home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Java / JDesignerPro / Jdp3_0.exe / data1.cab / Program_Files / Applications / Layouts / JDPComponentTemplate.java < prev    next >
Encoding:
Java Source  |  1999-04-09  |  4.3 KB  |  190 lines

  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.util.*;
  4.  
  5.  
  6. /**
  7.  * Class to represent a graphical component.
  8.  */
  9. public class JDPComponentTemplate extends Panel {
  10.  
  11.     Image offscreen;
  12.     Rectangle offscreensize;
  13.     Graphics gr;
  14.     Rectangle Bounds;
  15.     Font prevFont;
  16.     Color prevForeground;
  17.     Color prevBackground;
  18.     int minimumWidth = 140;
  19.     int minimumHeight = 140;
  20.     /*
  21.      * Used when an event needs to be sent from this component
  22.      */
  23.     Event thisEvent = null;
  24.     /*
  25.      * Used to signify that the last image should simply be redrawn
  26.      */
  27.      boolean nothingChanged = false;
  28.     /*
  29.      * Display a 3D border
  30.      */
  31.     boolean threeDBorder = true;
  32.  
  33.     /**
  34.      * Creates a new graphical component
  35.      */
  36.     public JDPComponentTemplate () {
  37.  
  38.  
  39.     }
  40.  
  41.     /**
  42.      * Redraw the component.
  43.      */
  44.     public void reDraw() {
  45.  
  46.         nothingChanged = false;
  47.         repaint();
  48.  
  49.     }
  50.  
  51.     public void layout() {
  52.  
  53.         repaint();    
  54.     }
  55.  
  56.     public void paint(Graphics g) {
  57.  
  58.         update(g);
  59.     }
  60.  
  61.     public void update(Graphics g) {
  62.  
  63.         Dimension newSize = size();
  64.         if ((Bounds == null) || (newSize.width != Bounds.width) || 
  65.             (newSize.height != Bounds.height) || (!getFont().equals(prevFont)) ||
  66.             (!prevForeground.equals(getForeground())) ||
  67.             (!prevBackground.equals(getBackground()))) {
  68.             nothingChanged = false;
  69.         }
  70.         if ((nothingChanged) && (offscreen != null) && (thisEvent == null)) {
  71.             g.drawImage(offscreen, 0, 0, null);
  72.             return;
  73.         }
  74.         nothingChanged = true;
  75.         prevFont = getFont();
  76.         prevForeground = getForeground();
  77.         prevBackground = getBackground();
  78.  
  79.         Rectangle d = bounds();
  80.         if ((offscreen == null) || (d.width != offscreensize.width) || (d.height != offscreensize.height)) {
  81.             offscreen = createImage(d.width, d.height);
  82.             offscreensize = d;
  83.             gr = offscreen.getGraphics();
  84.             gr.setFont(getFont());
  85.         }
  86.  
  87.         Bounds = this.bounds();
  88.         gr.setColor(getBackground());
  89.         gr.fillRect(2,2,Bounds.width,Bounds.height);
  90.  
  91.         drawComponent(gr);
  92.  
  93.         if (threeDBorder) {
  94.             gr.setColor(Color.gray);
  95.             gr.drawLine(0,0,Bounds.width,0);
  96.             gr.drawLine(0,0,0,Bounds.height);
  97.             gr.setColor(Color.black);
  98.             gr.drawLine(1,1,Bounds.width-1,1);
  99.             gr.drawLine(1,1,1,Bounds.height-1);
  100.             gr.setColor(Color.lightGray);
  101.             gr.drawLine(Bounds.width-2,2,Bounds.width-2,Bounds.height-2);
  102.             gr.drawLine(2,Bounds.height-2,Bounds.width-2,Bounds.height-2);
  103.             gr.setColor(Color.white);
  104.             gr.drawLine(Bounds.width-1,1,Bounds.width-1,Bounds.height-1);
  105.             gr.drawLine(1,Bounds.height-1,Bounds.width-1,Bounds.height-1);
  106.         } else {
  107.             gr.setColor(Color.white);
  108.             gr.drawRect(1,1,Bounds.width-2,Bounds.height-2);
  109.             gr.setColor(Color.black);
  110.             gr.drawRect(0,0,Bounds.width-1,Bounds.height-1);
  111.         }
  112.  
  113.         g.drawImage(offscreen, 0, 0, Bounds.width, Bounds.height, null);
  114.  
  115.         if (thisEvent != null) {
  116.             Event newEvent = thisEvent;
  117.             thisEvent = null;
  118.             super.postEvent(newEvent);
  119.         }
  120.         
  121.     }
  122.  
  123.     /**
  124.      * Draw the component
  125.      * @param gr the graphics instance to draw to
  126.      */
  127.     public void drawComponent(Graphics gr) {
  128.  
  129.         gr.setColor(Color.blue);
  130.         gr.drawString("Here is some text",20,20);
  131.  
  132.     }
  133.  
  134.     public boolean handleEvent(Event evt) {
  135.  
  136.         if (!this.isEnabled()) return true;
  137.  
  138.         switch (evt.id) {
  139.         case Event.MOUSE_DOWN:
  140.             Date now = new Date();
  141.             thisEvent = new Event(this,now.getTime(),Event.ACTION_EVENT,evt.x,evt.y,evt.key,evt.modifiers,null);
  142.             reDraw();
  143.             return true;
  144.  
  145.         }
  146.         return false;
  147.     }
  148.  
  149.         /**
  150.      * Set the minimum width of the grid
  151.      * @param the width in pixels.
  152.      */
  153.     public void setMinimumWidth(int width) {
  154.         minimumWidth = width;
  155.     }
  156.     /**
  157.      * Get the minimum width of the grid
  158.      * @return the width in pixels.
  159.      */
  160.     public int getMinimumWidth() {
  161.         return minimumWidth;
  162.     }
  163.     /**
  164.      * Set the minimum height of the grid
  165.      * @param the height in pixels.
  166.      */
  167.     public void setMinimumHeight(int height) {
  168.         minimumHeight = height;
  169.     }
  170.     /**
  171.      * Get the minimum height of the grid
  172.      * @return the height in pixels.
  173.      */
  174.     public int getMinimumHeight() {
  175.         return minimumHeight;
  176.     }
  177.  
  178.     public Dimension preferredSize() {
  179.  
  180.         Dimension d = new Dimension(minimumWidth,minimumHeight);
  181.         return d;
  182.     }
  183.     public Dimension minimumSize() {
  184.  
  185.         Dimension d = new Dimension(minimumWidth,minimumHeight);
  186.         return d;
  187.     }
  188.  
  189. }
  190.