项目作者: sshtools

项目描述 :
A simple library for generating Java2D (Swing/AWT) and JavaFX icons in Java, with SWT support in an add-on.
高级语言: Java
项目地址: git://github.com/sshtools/icon-generator.git
创建时间: 2015-11-11T10:36:40Z
项目社区:https://github.com/sshtools/icon-generator

开源协议:Apache License 2.0

下载


icon-generator

A simple library for generating Java2D (Swing/AWT), JavaFX and SWT icons in Java.

icon-generator generates icons images automatically from text or icons with customisable shapes, colour
and styles.

Configuring your project

The library is available in Maven Central, so configure your project according to the
build system you use.

As from version 1.2, icon-generator requires Java 9 or higher (due to modularity requirements). If you
want to use this library with Java 8 or earlier, then you will have to continue to use version
1.0.3. I may consider future 1.0.x releases if anyone needs them.

Choose the appropriate module for the toolkit you wish to use.
You may include multiple toolkits if you wish :-

Swing

  1. <dependency>
  2. <groupId>com.sshtools</groupId>
  3. <artifactId>icon-generator-swing</artifactId>
  4. <version>1.3.0</version>
  5. </dependency>

JavaFX

  1. <dependency>
  2. <groupId>com.sshtools</groupId>
  3. <artifactId>icon-generator-javafx</artifactId>
  4. <version>1.3.0</version>
  5. </dependency>

SWT

  1. <dependency>
  2. <groupId>com.sshtools</groupId>
  3. <artifactId>icon-generator-swt</artifactId>
  4. <version>1.3.0</version>
  5. </dependency>

Configuring the Icon

For this, you use IconBuilder.

Create The Builder

  1. IconBuilder builder = new IconBuilder();

Sizes

  1. builder.width(64);
  2. builder.height(64);

Text Colours

  1. builder.textColor(0xff0000); // red
  2. // or
  3. build.textColor(IconBuilder.AUTO_TEXT_COLOR);
  4. // or
  5. build.textColor(IconBuilder.RANDOM_TEXT_COLOR);
  6. // or
  7. build.textColor(IconBuilder.AUTO_TEXT_COLOR_WHITE);
  8. // or
  9. build.textColor(IconBuilder.AUTO_TEXT_COLOR_BLACK);
  10. // and the background colour
  11. builder.color(0x00ff00); // green
  12. // or
  13. builder.color(IconBuilder.AUTO_COLOR); // based on text / icon
  14. // or
  15. builder.color(IconBuilder.RANDOM_COLOR);

Shape

  1. builder.autoShape();
  2. // or
  3. builder.rect(); // rectangle
  4. // or
  5. builder.round(); // round
  6. // or
  7. builder.rectRound(8); // rounded rectangle with 8px radius

Text And Fonts

  1. builder.text("AB");
  2. // or a full word and a processing option
  3. builder.text("Actually Brilliant");
  4. builder.textCase(TextCase.UPPER);
  5. builder.textContext(TextContext.INITIALS);
  6. // Font
  7. builder.font("Monospaced");
  8. builder.fontSize(12);
  9. builder.bold(true);

Font Awesome Icon

  1. builder.icon(AwesomeIcon.ADDRESS_BOOK);
  2. // or
  3. builder.awesomeIconMode(AwesomeIconMode.AUTO_TEXT);
  4. // or
  5. builder.awesomeIconMode(AwesomeIconMode.AUTO_MATCH);

Generate The Icon

Now you can use the IconBuilder.build() for the toolkit you are using. You must
provide the class of the concrete icon to create, depending on the toolkit. The
following are supported :-

Class Module Description
java.awt.BufferedImage icon-generator-swing A Swing buffered image.
java.awt.Image icon-generator-swing A Swing buffered image.
javax.swing.Icon icon-generator-swing A Swing icon.
org.eclipse.swt.graphics.Image icon-generator-swt An SWT image.
javafx.scene.canvas.Canvas icon-generator-javafx A JavaFX canvas.

Generate an ImageIcon for use in Swing

  1. JLabel l = new JLabel("A label with an icon");
  2. l.setIcon(builder.build(Icon.class));

Generate a Java2D BufferedImage for use in a servlet

Something like this :-

  1. response.setContentType("image/jpeg");
  2. try(OutputStream out = response.getOutputStream()) {
  3. ImageIO.write(builder.build(BufferedImage.class), "jpg", out);
  4. }

Extending

New generators may be added by implementing IconGenerator.
You can then either add the generator to each IconBuilder you create :-

  1. builder.generator(MyIcon.class, new IconGenerator<MyIcon>() {
  2. @Override
  3. public Class<MyIcon> getIconClass() {
  4. return MyIcon.class;
  5. }
  6. @Override
  7. public boolean isValid() {
  8. /* This can be used to have the generator ignored if some pre-condition is not met,
  9. such as a supporting library not being available */
  10. return true;
  11. }
  12. @Override
  13. public MyIcon generate(IconBuilder builder, Object... args) {
  14. MyIcon myIcon = ..
  15. /** Do stuff to generate a MyIcon given the configuration provided by IconBuilder */
  16. return myIcon;
  17. }
  18. });

.. or create a concrete class and add it as a service. Create a classpath resource named META-INF/services/com.sshtools.icongenerator.IconGenerator (e.g. src/main/resources/META-INF/services/com.sshtools.icongenerator.IconGenerator) and place the full class name of your IconGenerator implementation inside it.

  1. com.mystuff.icongen.MyIconGenerator

This will then be automatically loaded whenever you create a new IconBuilder instance, and you can use the build()
method as normal :-

  1. IconBuilder builder = new IconBuilder();
  2. // do stuff to create builder ...
  3. MyIcon myIcon = builder.build(MyIcon.class);

The build() method also can accept additional arguments which are passed on to the generator, if your generator
needs additional configuration.

  1. MyIcon myIcon = builder.build(MyIcon.class, flipX, flipY);

.. and then in the generator access the arguments :-

  1. @Override
  2. public MyIcon generate(IconBuilder builder, Object... args) {
  3. boolean flipX = (Boolean)args[0];
  4. boolean flipY = (Boolean)args[1];
  5. MyIcon myIcon = ..
  6. /** Do stuff to generate a MyIcon given the configuration provided by IconBuilder */
  7. return myIcon;
  8. }