Prev
Next

Easy to create new components

  1 import java.awt. *;
  2 import java.awt.event. *;
  3 import java.applet.Applet;
  4 
  5 /***********************************************************/
  6 
  7 class GroupCard extends Canvas
  8 {
  9   int color, number, shade, shape;
 10 
 11   boolean selected;
 12 
 13   int i, j, k, w, h;
 14 
 15   public GroupCard (int c, int n, int sd, int sp) {
 16     color = c;
 17     number = n;
 18     shade = sd;
 19     shape = sp;
 20     selected = false;
 21   }
 22 
 23   public void setSelected (boolean sel) {
 24 
 25     selected = sel;
 26     repaint ();
 27   }
 28 
 29   public void paint (Graphics g) {
 30     Dimension d;
 31     int xpoints [] = new int [10];
 32     int ypoints [] = new int [10];
 33 
 34     int base_x, base_y;
 35 
 36     Color bordercolor, fillcolor;
 37 
 38     d = getSize ();
 39     w = d.width;
 40     h = d.height;
 41     
 42     switch (color) {
 43     case 0:
 44       bordercolor = new Color (1f, 0, 0);
 45       fillcolor = new Color (1, (float) Math.sqrt (shade / 2f),
 46 			     (float) Math.sqrt (shade / 2f));
 47       break;
 48       case 1:
 49       bordercolor = new Color (0, .75f, 0);
 50       fillcolor = new Color ((float) Math.sqrt (shade / 2f),
 51 			     (float) 1, (float) Math.sqrt (shade / 2f));
 52       break;
 53       default:
 54       bordercolor = new Color (0, 0, 1f);
 55       fillcolor = new Color ((float) Math.sqrt (shade / 2f),
 56 			     (float) Math.sqrt (shade / 2f), 1);
 57       break;
 58     }
 59 
 60     if (selected) {
 61       g.setColor (new Color (192, 192, 192));
 62       g.fillRect (0, 0, w, h);
 63 
 64     } else {
 65 
 66       g.setColor (new Color (255, 255, 255));
 67       g.fillRect (0, 0, w, h);
 68     }
 69 
 70 
 71     g.setColor (new Color (0, 0, 0));
 72     g.drawRect (1, 1, w - 2, h - 2);
 73     g.drawRect (2, 2, w - 4, h - 4);
 74 
 75     /* Begin boring drawing code */
 76 
 77     for (i = 0; i < number + 1; i++) {
 78       switch (shape) {
 79       case 0:
 80 	//diamond
 81 	xpoints[0] = (int) (w * 0.2);
 82 	xpoints[1] = (int) (w * 0.5);
 83 	xpoints[2] = (int) (w * 0.8);
 84 	xpoints[3] = (int) (w * 0.5);
 85 
 86 	ypoints[0] = (int) (h * (i + .5) / (number + 1));
 87 	ypoints[1] = (int) (h * (i + .5) / (number + 1) - h / 10);
 88 	ypoints[2] = (int) (h * (i + .5) / (number + 1));
 89 	ypoints[3] = (int) (h * (i + .5) / (number + 1) + h / 10);
 90 	g.setColor (fillcolor);
 91 	g.fillPolygon (xpoints, ypoints, 4);
 92 	g.setColor (bordercolor);
 93 	for (k = -1; k < 2; k++) {
 94 	  g.drawPolygon (xpoints, ypoints, 4);
 95 	  for (j = 0; j < 3; j++) {
 96 	    ypoints[j] += 1;
 97 	  }
 98 	}
 99 	
100 	break;
101 
102       case 1:
103 	//oval
104 	g.setColor (fillcolor);
105 	g.fillOval ((int) (w * .2), (int) (h * (i + .5) / (number + 1) -
106 					   h / 9),
107 		    (int) (w * .6), (int) (h / 6));
108 	g.setColor (bordercolor);
109 	for (k = -1; k < 2; k++) {
110 	  g.drawOval ((int) (w * .2), (int) (h * (i + .5) / (number +
111 							     1) - h / 9) + k,
112 		      (int) (w * .6), (int) (h / 6));
113 	}
114 	break;
115       case 2:
116 	//squiggle
117 	base_x = (int) (w * .5);
118 	base_y = (int) (h * (i + .45) / (number + 1));
119 
120 	double [] x_coefs = {.3, .1, -.1, -.3, -.3, -.1, .1, .3 };
121 	double [] y_coefs = {.15, 0, .15, 0, -.1, .1, -.1, .1};
122 
123 	for (int p = 0; p < 8; p++) {
124 	    xpoints [p] = (int) (base_x + w * x_coefs [p]);
125 	    ypoints [p] = (int) (base_y + h * y_coefs [p]);
126 	}
127 	
128 	g.setColor (fillcolor);
129 	g.fillPolygon (xpoints, ypoints, 8);
130 	g.setColor (bordercolor);
131 
132 	for (k = -1; k < 2; k++) {
133 	  g.drawPolygon (xpoints, ypoints, 8);
134 	  for (j = 0; j < 3; j++) {
135 	    ypoints[j] += 1;
136 	  }
137 	}
138 	
139 	break;
140       }
141     }
142   }
143 }


Prev
Next