1 package snow.utils.gui; 2 3 import java.awt.*; 4 import java.awt.image.*; 5 import java.awt.geom.*; 6 import javax.swing.*; 7 8 10 public abstract class Icons implements Icon 11 { 12 protected int w, h; 13 14 public Icons(int w, int h) 15 { 16 this.w = w; 17 this.h = h; 18 } 19 20 public int getIconWidth() 21 { 22 return w; 23 } 24 25 public int getIconHeight() 26 { 27 return h; 28 } 29 30 31 public void paintIcon(Component c, Graphics g, int x, int y) 32 { 33 Graphics2D g2 = (Graphics2D) g; 34 g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 35 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 36 37 41 Font oldFont = g2.getFont(); 43 Stroke oldStroke = g2.getStroke(); 44 Color oldColor = g2.getColor(); 45 46 if(c!=null) 47 { 48 g2.setColor(c.getForeground()); 49 } 50 g2.translate(x,y); 51 _paintIcon(c,g2); 52 g2.translate(-x,-y); 53 54 g2.setFont(oldFont); 56 g2.setStroke(oldStroke); 57 g2.setColor(oldColor); 58 } 59 60 public abstract void _paintIcon(Component c, Graphics2D g); 61 62 64 public static Image createImage(Icon icon) 65 { 66 BufferedImage bim = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB); 67 Graphics2D g = bim.createGraphics(); 68 icon.paintIcon(null, g, 0, 0); 69 g.dispose(); 70 return bim; 71 } 72 73 76 public static class StartIcon extends Icons 77 { 78 boolean enabled; 79 Color color = Color.green; 80 81 public final static Icon sharedStart18Icon = new StartIcon(16,16,true); 82 83 public final static Icon shared10 = new StartIcon(10,10,true); 84 85 public StartIcon(int w, int h, boolean enabled) 86 { 87 this(w,h,enabled,Color.green); 88 } 89 90 public StartIcon(int w, int h, boolean enabled, Color c) 91 { 92 super(w,h); 93 this.enabled = enabled; 94 this.color= c; 95 } 96 97 public void _paintIcon(Component c, Graphics2D g2) 98 { 99 GeneralPath gp = new GeneralPath(); 102 gp.moveTo(w/4f, h/8f); 103 gp.lineTo(0.8f*w, .5f*h); 104 gp.lineTo(w/4f, h-h/8f); 105 gp.closePath(); 106 107 if(enabled) 108 { 109 g2.setColor(color); 110 g2.fill(gp); 111 g2.setStroke(new BasicStroke((float) h/15.0f)); 112 g2.setColor(Color.black); 113 g2.draw(gp); 114 } 115 else 116 { 117 g2.setColor(Color.gray); 118 g2.fill(gp); 119 } 120 } 121 } 122 123 public static class WizIcon extends Icons 124 { 125 boolean enabled; 126 final Color col = new Color(120,120,120,130); 127 128 public final static WizIcon shared15 = new WizIcon(13,true); 129 130 public WizIcon(int w, boolean enabled) 131 { 132 super(w,w); 133 this.enabled = enabled; 134 } 135 136 @Override 137 public void _paintIcon(Component c, Graphics2D g2) 138 { 139 Shape line1 = new Line2D.Double(w/8,h-w/4,0.48*w,0.48*w); 140 GeneralPath gp = new GeneralPath(); 141 gp.moveTo(0.1*w, 0.1*h); 142 gp.lineTo(0.6*w, 0.33*h); 143 gp.lineTo(0.4*w, 0.66*h); 144 gp.lineTo(0.9*w, 0.9*h); 145 gp.lineTo(0.9*w, 0.9*h); 146 147 gp.lineTo(0.8*w, 0.7*h); 148 gp.lineTo(0.7*w, 0.9*h); 149 gp.lineTo(0.9*w, 0.9*h); 150 151 if(enabled) 152 { 153 g2.setStroke(new BasicStroke(h/15.0f)); 154 g2.draw(gp); 155 } 156 else 157 { 158 g2.setColor(Color.gray); 159 g2.draw(gp); 160 } 161 } 162 } 163 164 165 public static class LetterIcon extends Icons 166 { 167 boolean enabled; 168 String letter; 169 boolean border; 170 Color letterColor = Color.black; 171 172 public LetterIcon(String letter, boolean border, int w, int h, boolean enabled) 173 { 174 super(w,h); 175 this.enabled = enabled; 176 this.letter = letter; 177 this.border = border; 178 } 179 180 public void _paintIcon(Component c, Graphics2D g2) 181 { 182 g2.setColor(letterColor); 183 g2.setFont(new Font("Lucida Sans Typewriter", Font.BOLD | Font.BOLD, h*6/7)); 184 g2.drawString(letter, w/3, h-w/5); 185 186 if(border) 187 { 188 g2.setColor(Color.black); 189 Shape rect = new Rectangle2D.Double(1,1,w-2,h-2); 190 g2.draw(rect); 191 } 192 } 193 } 194 195 public static class StopIcon extends Icons 196 { 197 boolean enabled; 198 Color color = Color.red; 199 public StopIcon(int w, int h, boolean enabled) 200 { 201 super(w,h); 202 this.enabled = enabled; 203 } 204 205 public void _paintIcon(Component c, Graphics2D g2) 206 { 207 208 Shape rect = new Rectangle2D.Double(w/8,w/8,w-w/4,h-w/4); 209 if(enabled) 210 { 211 g2.setColor(color); 212 g2.fill(rect); 213 g2.setStroke(new BasicStroke((float) h/15.0f)); 214 g2.setColor(Color.black); 215 g2.draw(rect); 216 } 217 else 218 { 219 g2.setColor(Color.gray); 220 g2.fill(rect); 221 } 222 } 223 } 224 225 public static class DiskIcon extends Icons 226 { 227 boolean enabled; 228 final Color col; 229 public DiskIcon(int w, int h, Color c, boolean enabled) 230 { 231 super(w,h); 232 this.enabled = enabled; 233 this.col = c; 234 } 235 236 public void _paintIcon(Component c, Graphics2D g2) 237 { 238 239 Shape disk = new Arc2D.Double(w/8,w/8,w-w/4,h-w/4, 0,360, Arc2D.CHORD); 240 if(enabled) 241 { 242 g2.setColor(col); 243 g2.fill(disk); 244 g2.setStroke(new BasicStroke((float) h/15.0f)); 245 g2.setColor(Color.black); 246 g2.draw(disk); 247 } 248 else 249 { 250 g2.setColor(Color.gray); 251 g2.fill(disk); 252 } 253 } 254 } 255 256 public static class PauseIcon extends Icons 257 { 258 boolean enabled; 259 Color color = Color.blue; 260 public PauseIcon(int w, int h, boolean enabled) 261 { 262 super(w,h); 263 this.enabled = enabled; 264 } 265 266 public void _paintIcon(Component c, Graphics2D g2) 267 { 268 269 Shape rect1 = new Rectangle2D.Double(w/8,w/8,w/2-w/4,h-w/4); 270 Shape rect2 = new Rectangle2D.Double(w/2+w/8,w/8,w/2-w/4,h-w/4); 271 if(enabled) 272 { 273 g2.setColor(color); 274 g2.fill(rect1); 275 g2.fill(rect2); 276 } 280 else 281 { 282 g2.setColor(Color.gray); 283 g2.fill(rect1); 284 g2.fill(rect2); 285 } 286 } 287 } 288 289 290 public static class CrossIcon extends Icons 291 { 292 boolean enabled; 293 Color color = Color.red; 294 final public static CrossIcon shared10 = new Icons.CrossIcon(10,10,true); 295 296 public CrossIcon(int w, int h, boolean enabled) 297 { 298 super(w,h); 299 this.enabled = enabled; 300 } 301 302 public void _paintIcon(Component c, Graphics2D g2) 303 { 304 305 Shape line1 = new Line2D.Double(w/8,w/8,w-w/4,h-w/4); 306 Shape line2 = new Line2D.Double(w/8,h-w/4,w-w/4,w/8); 307 if(enabled) 308 { 309 g2.setColor(color); 310 g2.setStroke(new BasicStroke((float) h/15.0f)); 311 g2.draw(line1); 312 g2.draw(line2); 313 } 314 else 315 { 316 g2.setColor(Color.gray); 317 g2.draw(line1); 318 g2.draw(line2); 319 } 320 } 321 } 322 323 324 public static class OkIcon extends Icons 325 { 326 boolean enabled; 327 Color color = new Color(2,120,2); 328 final public static OkIcon shared10 = new Icons.OkIcon(12,12,true); 329 330 public OkIcon(int w, int h, boolean enabled) 331 { 332 super(w,h); 333 this.enabled = enabled; 334 } 335 336 public void _paintIcon(Component c, Graphics2D g2) 337 { 338 339 Shape line1 = new Line2D.Double(w/3,h/2,w/2,h-w/4); 340 Shape line2 = new Line2D.Double(w/2,h-w/4,w-w/4,w/8); 341 if(enabled) 342 { 343 g2.setColor(color); 344 g2.setStroke(new BasicStroke((float) h/7.0f)); 345 g2.draw(line1); 346 g2.draw(line2); 347 } 348 else 349 { 350 g2.setColor(Color.gray); 351 g2.draw(line1); 352 g2.draw(line2); 353 } 354 } 355 } 356 357 358 359 public static class PlusIcon extends Icons 360 { 361 boolean enabled; 362 Color color = Color.blue; 363 364 public static final PlusIcon shared10 = new PlusIcon(10,10,true); 365 public PlusIcon(int w, int h, boolean enabled) 366 { 367 super(w,h); 368 this.enabled = enabled; 369 } 370 371 public void _paintIcon(Component c, Graphics2D g2) 372 { 373 374 Shape line1 = new Line2D.Double(w/8,h/2,w-w/8,h/2); 375 Shape line2 = new Line2D.Double(w/2,h-w/8,w/2,w/8); 376 if(enabled) 377 { 378 g2.setColor(color); 379 g2.setStroke(new BasicStroke((float) h/15.0f)); 380 g2.draw(line1); 381 g2.draw(line2); 382 } 383 else 384 { 385 g2.setColor(Color.gray); 386 g2.draw(line1); 387 g2.draw(line2); 388 } 389 } 390 } 391 392 393 public static class RightArrow extends Icons 394 { 395 boolean enabled; 396 Color color = Color.blue; 397 public final static Icon sharedJump18Icon = new RightArrow(14,14,true); 398 399 public RightArrow(int w, int h, boolean enabled) 400 { 401 super(w,h); 402 this.enabled = enabled; 403 } 404 405 public void _paintIcon(Component c, Graphics2D g2) 406 { 407 408 Shape line1 = new Line2D.Double(w/8,h/2,w-w/8,h/2); 409 Shape line2 = new Line2D.Double(w-w/8,h/2,3*w/4-w/8,h/2-h/5); 410 Shape line3 = new Line2D.Double(w-w/8,h/2,3*w/4-w/8,h/2+h/5); 411 if(enabled) 412 { 413 g2.setColor(color); 414 g2.setStroke(new BasicStroke((float) h/15.0f)); 415 g2.draw(line1); 416 g2.draw(line2); 417 g2.draw(line3); 418 } 419 else 420 { 421 g2.setColor(Color.gray); 422 g2.draw(line1); 423 g2.draw(line2); 424 g2.draw(line3); 425 } 426 } 427 } 428 429 public static class LeftArrow extends Icons 430 { 431 boolean enabled; 432 Color color = Color.blue; 433 public final static Icon shared14 = new LeftArrow(14,14,true); 434 435 public LeftArrow(int w, int h, boolean enabled) 436 { 437 super(w,h); 438 this.enabled = enabled; 439 } 440 441 public void _paintIcon(Component c, Graphics2D g2) 442 { 443 444 Shape line1 = new Line2D.Double(w/8,h/2,w-w/8,h/2); 445 Shape line2 = new Line2D.Double(w/8,h/2,w/4+w/8,h/2-h/5); 446 Shape line3 = new Line2D.Double(w/8,h/2,w/4+w/8,h/2+h/5); 447 if(enabled) 448 { 449 g2.setColor(color); 450 g2.setStroke(new BasicStroke((float) h/15.0f)); 451 g2.draw(line1); 452 g2.draw(line2); 453 g2.draw(line3); 454 } 455 else 456 { 457 g2.setColor(Color.gray); 458 g2.draw(line1); 459 g2.draw(line2); 460 g2.draw(line3); 461 } 462 } 463 } 464 465 public static class UpIcon extends Icons 466 { 467 boolean enabled; 468 Color color = Color.blue; 469 public UpIcon(int w, int h, boolean enabled) 470 { 471 super(w,h); 472 this.enabled = enabled; 473 } 474 475 public void _paintIcon(Component c, Graphics2D g2) 476 { 477 Shape line1 = new Line2D.Double(w/8,h-w/4,w/2,w/8); 478 Shape line2 = new Line2D.Double(w/2,w/8,w-w/8,h-w/4); 479 480 if(enabled) 481 { 482 g2.setColor(color); 483 g2.setStroke(new BasicStroke((float) h/15.0f)); 484 g2.draw(line1); 485 g2.draw(line2); 486 } 487 else 488 { 489 g2.setColor(Color.gray); 490 g2.draw(line1); 491 g2.draw(line2); 492 } 493 } 494 } 495 496 public static class DownIcon extends Icons 497 { 498 boolean enabled; 499 Color color = Color.blue; 500 public DownIcon(int w, int h, boolean enabled) 501 { 502 super(w,h); 503 this.enabled = enabled; 504 } 505 506 public void _paintIcon(Component c, Graphics2D g2) 507 { 508 Shape line1 = new Line2D.Double(w/8,w/8,w/2,h-w/4); 509 Shape line2 = new Line2D.Double(w/2,h-w/4,w-w/8,w/8); 510 511 if(enabled) 512 { 513 g2.setColor(color); 514 g2.setStroke(new BasicStroke((float) h/15.0f)); 515 g2.draw(line1); 516 g2.draw(line2); 517 } 518 else 519 { 520 g2.setColor(Color.gray); 521 g2.draw(line1); 522 g2.draw(line2); 523 } 524 } 525 } 526 527 public static class SearchIcon extends Icons 528 { 529 boolean enabled; 530 final Color col = new Color(20,20,120,30); 531 532 final public static SearchIcon shared18 = new SearchIcon(15, true); 533 534 public SearchIcon(int w, boolean enabled) 535 { 536 super(w,w); 537 this.enabled = enabled; 538 } 539 540 @Override 541 public void _paintIcon(Component c, Graphics2D g2) 542 { 543 544 Shape disk = new Arc2D.Double(w/2-h/15,0,w/2,w/2, 0,360, Arc2D.CHORD); 545 Shape line1 = new Line2D.Double(0.15*w,0.75*h,0.47*w,0.46*w); 546 if(enabled) 547 { 548 g2.setColor(col); 549 g2.fill(disk); 550 g2.setStroke(new BasicStroke(h/15.0f)); 551 g2.setColor(Color.black); 552 g2.draw(disk); 553 g2.setStroke(new BasicStroke(h/7.0f)); 554 g2.draw(line1); 555 } 556 else 557 { 558 g2.setColor(Color.gray); 559 g2.fill(disk); 560 g2.draw(line1); 561 } 562 } 563 } 564 565 public static Icon createHelpIcon(int size, boolean enabled) 566 { 567 LetterIcon li = new LetterIcon("?", false, size, size, true); 568 li.letterColor = enabled ? new Color(10,10,120) : Color.lightGray ; 569 return li; 570 } 571 572 public static Icon mergeIcons(final int w, final int h, 573 final Icon i1, final int x1, final int y1, 574 final Icon i2, final int x2, final int y2) 575 { 576 return new Icons(w, h) 577 { 578 public void _paintIcon(Component c, Graphics2D g2){ 579 i1.paintIcon(c,g2,x1,y1); 580 i2.paintIcon(c,g2,x2,y2); 581 } 582 }; 583 } 584 585 public static Shape closedDisk(double x, double y, double r) 586 { 587 return new Arc2D.Double(x-r,y-r,2*r,2*r, 0,360,Arc2D.OPEN); 588 } 589 590 591 public static class KeyIcon extends Icons 592 { 593 boolean enabled; 594 final public static KeyIcon shared16 = new KeyIcon(16, 16, true); 595 596 public KeyIcon(int w, int h, boolean enabled) 597 { 598 super(w, h); 599 this.enabled = enabled; 600 } 601 602 @Override 603 public void _paintIcon(Component c, Graphics2D g2) 604 { 605 final GeneralPath gp = new GeneralPath(GeneralPath.WIND_EVEN_ODD); 606 gp.moveTo(w/3, h-h/3+h/16); 609 gp.lineTo(w-w/8, h/8); 610 611 gp.lineTo(w-w/8-w/22, h/8-h/22); 612 gp.lineTo(w-w/3-w/11, h/3-h/5.5); 614 615 gp.lineTo(w-w/3-w/8.0, h/2-h/6); 616 617 gp.lineTo(w-w/3-w/4.5, h/2-h/8.0); 618 619 620 gp.lineTo(w/3-w/22.0, h-h/3); 621 gp.closePath(); 622 623 final GeneralPath gp2 = new GeneralPath(GeneralPath.WIND_EVEN_ODD); 624 gp2.append(closedDisk(w/16+w/6, h-h/6-h/16, w/6),false); 625 gp2.append(closedDisk(w/16+w/6, h-h/6-h/16, w/18), false); 626 gp2.closePath(); 627 628 if(enabled) 629 { 630 g2.setPaint(new GradientPaint(0f,h,Color.orange, w, 0,Color.yellow)); 631 g2.fill(gp); 632 g2.setColor(Color.black); 633 g2.setStroke(new BasicStroke(h/15.0f)); 634 g2.draw(gp); 635 636 g2.setPaint(new GradientPaint(0f,h,Color.orange, w, 0,Color.yellow)); 637 g2.fill(gp2); 638 g2.setColor(Color.black); 639 g2.setStroke(new BasicStroke(h/15.0f)); 640 g2.draw(gp2); 641 642 } 643 else 644 { 645 g2.draw(gp); 647 g2.draw(gp2); 648 649 } 650 } 651 } 652 653 654 public static class WarnIcon extends Icons 655 { 656 boolean enabled; 657 final public static WarnIcon shared16 = new WarnIcon(16, 16, true); 658 659 public WarnIcon(int w, int h, boolean enabled) 660 { 661 super(w,h); 662 this.enabled = enabled; 663 } 664 665 @Override 666 public void _paintIcon(Component c, Graphics2D g2) 667 { 668 final GeneralPath gp = new GeneralPath(); 669 gp.moveTo(w/10, h-h/8); 670 gp.lineTo(w/2, h/9); 671 gp.lineTo(w-w/10, h-h/8); 672 gp.closePath(); 673 674 if(enabled) 675 { 676 g2.setPaint(new GradientPaint(0f,0f,Color.red, w, 0,Color.orange)); 677 678 g2.fill(gp); 679 680 g2.setColor(Color.black); 681 g2.setStroke(new BasicStroke(h/15.0f)); 682 g2.draw(gp); 683 684 g2.setFont(new Font("Lucida Sans Typewriter", Font.BOLD | Font.BOLD, h*5/7)); 685 g2.drawString("!", w/3, h-w/5); 686 } 687 else 688 { 689 g2.draw(gp); 691 } 692 } 693 } 694 695 696 697 public static class LockIcon extends Icons 698 { 699 boolean enabled,open; 700 final public static LockIcon sharedOpen16 = new LockIcon(16, 16, true, true); 701 final public static LockIcon sharedClosed16 = new LockIcon(16, 16, false, true); 702 703 public LockIcon(int w, int h, boolean open, boolean enabled) 704 { 705 super(w,h); 706 this.enabled = enabled; 707 this.open = open; 708 } 709 710 @Override 711 public void _paintIcon(Component c, Graphics2D g2) 712 { 713 final GeneralPath gp = new GeneralPath(); 714 gp.moveTo(w/4, h/3); 715 gp.lineTo(w-w/4, h/3); 716 gp.lineTo(w-w/4, h-h/8); 717 gp.lineTo(w/4, h-h/8); 718 gp.closePath(); 719 720 gp.append(new Arc2D.Double(w/2-w/16,h/2+h/16,w/8,h/8,0,360,Arc2D.OPEN), false); 721 722 final GeneralPath gp2 = new GeneralPath(); 723 724 gp2.moveTo(w/3, h/3); 725 gp2.curveTo(w/3, h/11, w/3, h/11, w/2, h/11); 726 if(!open) 727 gp2.curveTo(2*w/3, h/11, 2*w/3, h/11, 2*w/3, h/3); 728 729 if(enabled) 730 { 731 if(open) 732 { 733 g2.setPaint(new GradientPaint(0f,0f,Color.lightGray, w, 0,Color.darkGray)); 734 } 735 else 736 { 737 g2.setPaint(new GradientPaint(0f,0f,Color.red, w, 0,Color.orange)); 738 } 739 740 g2.fill(gp); 741 742 g2.setColor(Color.black); 743 g2.setStroke(new BasicStroke(h/15.0f)); 744 g2.draw(gp); 745 g2.draw(gp2); 746 } 747 else 748 { 749 g2.setStroke(new BasicStroke(h/15.0f)); 750 g2.setColor(Color.gray); 751 g2.draw(gp); 752 g2.draw(gp2); 753 } 754 } 755 } 756 757 public static void main(String [] a) 759 { 760 KeyIcon li = new KeyIcon(64,64,true); 762 763 final JFrame fr = new JFrame("Test Icon"); 764 fr.setIconImage(Icons.createImage(li)); 765 fr.setContentPane(new JLabel(li)); 766 767 fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 768 769 fr.pack(); 770 fr.setLocationRelativeTo(null); 771 fr.setVisible(true); 772 773 final JPopupMenu pop = new JPopupMenu(); 774 pop.add(new JMenuItem( "Search", new Icons.SearchIcon(15,true))); 775 pop.add(new JMenuItem( "Up", new UpIcon(15, 15, true))); 776 pop.add(new JMenuItem( "Down", new DownIcon(15, 15, true))); 777 pop.add(new JMenuItem( "Compile", new LetterIcon("C", true, 17, 17, true))); 778 pop.add(new JMenuItem( "Jump", new RightArrow(16, 16, true))); 779 pop.add(new JMenuItem( "Back", new LeftArrow(16, 16, true))); 780 781 pop.add(new JMenuItem( "Disk", new DiskIcon(10, 10, Color.red, true))); 782 pop.addSeparator(); 783 pop.add(new JMenuItem( "Start", new StartIcon(12, 12, true))); 784 pop.add(new JMenuItem( "Pause", new PauseIcon(12, 12, true))); 785 pop.add(new JMenuItem( "Stop", new StopIcon(12, 12, true))); 786 pop.addSeparator(); 787 pop.add(new JMenuItem( "Wiz", new WizIcon(12, true))); 788 pop.addSeparator(); 789 pop.add(new JMenuItem( "A12", new LetterIcon("A",true,12, 12, true))); 790 pop.add(new JMenuItem( "A18", new LetterIcon("A",true,18, 18, true))); 791 pop.add(new JMenuItem( "All", new LetterIcon("*",false,18, 18, true))); 792 pop.add(new JMenuItem( "Omega", new LetterIcon("\u03a9",false,17, 17, true))); 793 pop.add(new JMenuItem( "Breakpoint", new DiskIcon(7, 7, Color.red, true))); 794 795 pop.addSeparator(); 796 pop.add(new JMenuItem(createHelpIcon(14, false))); 797 798 pop.add(new JMenuItem( "Ok", new OkIcon(12, 12, true))); 799 pop.add(new JMenuItem( "Warn", new WarnIcon(12, 12, true))); 800 pop.add(new JMenuItem( "Lock", new LockIcon(16, 16, true, true))); 801 pop.add(new JMenuItem( "Lock", new LockIcon(16, 16, false, true))); 802 pop.add(new JMenuItem( "Plus", Icons.PlusIcon.shared10)); 803 804 Icon i1 = mergeIcons(16,16,new DiskIcon(10, 10, Color.red, true),0,0, 805 Icons.PlusIcon.shared10,0,0); 806 pop.add(new JMenuItem( "merged", i1)); 807 808 pop.add(new JMenuItem( "Close", new CrossIcon(12, 12, true))); 809 EventQueue.invokeLater(new Runnable () { public void run() { 810 pop.show(fr,0,0); 811 }}); 812 } 813 814 } | Popular Tags |