home *** CD-ROM | disk | FTP | other *** search
Java Source | 1999-04-09 | 4.3 KB | 190 lines |
- import java.awt.*;
- import java.applet.*;
- import java.util.*;
-
-
- /**
- * Class to represent a graphical component.
- */
- public class JDPComponentTemplate extends Panel {
-
- Image offscreen;
- Rectangle offscreensize;
- Graphics gr;
- Rectangle Bounds;
- Font prevFont;
- Color prevForeground;
- Color prevBackground;
- int minimumWidth = 140;
- int minimumHeight = 140;
- /*
- * Used when an event needs to be sent from this component
- */
- Event thisEvent = null;
- /*
- * Used to signify that the last image should simply be redrawn
- */
- boolean nothingChanged = false;
- /*
- * Display a 3D border
- */
- boolean threeDBorder = true;
-
- /**
- * Creates a new graphical component
- */
- public JDPComponentTemplate () {
-
-
- }
-
- /**
- * Redraw the component.
- */
- public void reDraw() {
-
- nothingChanged = false;
- repaint();
-
- }
-
- public void layout() {
-
- repaint();
- }
-
- public void paint(Graphics g) {
-
- update(g);
- }
-
- public void update(Graphics g) {
-
- Dimension newSize = size();
- if ((Bounds == null) || (newSize.width != Bounds.width) ||
- (newSize.height != Bounds.height) || (!getFont().equals(prevFont)) ||
- (!prevForeground.equals(getForeground())) ||
- (!prevBackground.equals(getBackground()))) {
- nothingChanged = false;
- }
- if ((nothingChanged) && (offscreen != null) && (thisEvent == null)) {
- g.drawImage(offscreen, 0, 0, null);
- return;
- }
- nothingChanged = true;
- prevFont = getFont();
- prevForeground = getForeground();
- prevBackground = getBackground();
-
- Rectangle d = bounds();
- if ((offscreen == null) || (d.width != offscreensize.width) || (d.height != offscreensize.height)) {
- offscreen = createImage(d.width, d.height);
- offscreensize = d;
- gr = offscreen.getGraphics();
- gr.setFont(getFont());
- }
-
- Bounds = this.bounds();
- gr.setColor(getBackground());
- gr.fillRect(2,2,Bounds.width,Bounds.height);
-
- drawComponent(gr);
-
- if (threeDBorder) {
- gr.setColor(Color.gray);
- gr.drawLine(0,0,Bounds.width,0);
- gr.drawLine(0,0,0,Bounds.height);
- gr.setColor(Color.black);
- gr.drawLine(1,1,Bounds.width-1,1);
- gr.drawLine(1,1,1,Bounds.height-1);
- gr.setColor(Color.lightGray);
- gr.drawLine(Bounds.width-2,2,Bounds.width-2,Bounds.height-2);
- gr.drawLine(2,Bounds.height-2,Bounds.width-2,Bounds.height-2);
- gr.setColor(Color.white);
- gr.drawLine(Bounds.width-1,1,Bounds.width-1,Bounds.height-1);
- gr.drawLine(1,Bounds.height-1,Bounds.width-1,Bounds.height-1);
- } else {
- gr.setColor(Color.white);
- gr.drawRect(1,1,Bounds.width-2,Bounds.height-2);
- gr.setColor(Color.black);
- gr.drawRect(0,0,Bounds.width-1,Bounds.height-1);
- }
-
- g.drawImage(offscreen, 0, 0, Bounds.width, Bounds.height, null);
-
- if (thisEvent != null) {
- Event newEvent = thisEvent;
- thisEvent = null;
- super.postEvent(newEvent);
- }
-
- }
-
- /**
- * Draw the component
- * @param gr the graphics instance to draw to
- */
- public void drawComponent(Graphics gr) {
-
- gr.setColor(Color.blue);
- gr.drawString("Here is some text",20,20);
-
- }
-
- public boolean handleEvent(Event evt) {
-
- if (!this.isEnabled()) return true;
-
- switch (evt.id) {
- case Event.MOUSE_DOWN:
- Date now = new Date();
- thisEvent = new Event(this,now.getTime(),Event.ACTION_EVENT,evt.x,evt.y,evt.key,evt.modifiers,null);
- reDraw();
- return true;
-
- }
- return false;
- }
-
- /**
- * Set the minimum width of the grid
- * @param the width in pixels.
- */
- public void setMinimumWidth(int width) {
- minimumWidth = width;
- }
- /**
- * Get the minimum width of the grid
- * @return the width in pixels.
- */
- public int getMinimumWidth() {
- return minimumWidth;
- }
- /**
- * Set the minimum height of the grid
- * @param the height in pixels.
- */
- public void setMinimumHeight(int height) {
- minimumHeight = height;
- }
- /**
- * Get the minimum height of the grid
- * @return the height in pixels.
- */
- public int getMinimumHeight() {
- return minimumHeight;
- }
-
- public Dimension preferredSize() {
-
- Dimension d = new Dimension(minimumWidth,minimumHeight);
- return d;
- }
- public Dimension minimumSize() {
-
- Dimension d = new Dimension(minimumWidth,minimumHeight);
- return d;
- }
-
- }
-