Here you can find the source of paintSequenceMatrix(Graphics2D g2, Rectangle2D rect, String sequence, Function
public static void paintSequenceMatrix(Graphics2D g2, Rectangle2D rect, String sequence, Function<Point, Paint> coloring, Function<Point, Paint> border)
//package com.java2s; /**//w ww. j a v a 2s. com * * Copyright 2017 Florian Erhard * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Paint; import java.awt.Point; import java.awt.Shape; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; import java.util.function.Function; public class Main { public static void paintSequenceMatrix(Graphics2D g2, Rectangle2D rect, String sequence, Function<Point, Paint> coloring, Function<Point, Paint> border) { Paint p = g2.getPaint(); Rectangle2D tile = (Rectangle2D) rect.clone(); int h = sequence.length() + 2; int w = sequence.length() + 2; Point point = new Point(); for (int i = 0; i < sequence.length(); i++) { point.y = i; for (int j = 0; j < sequence.length(); j++) { point.x = j; Paint c = coloring.apply(point); if (c != null) { tile.setRect(rect.getMinX() + rect.getWidth() / h * (j + 1), rect.getMinY() + rect.getHeight() / w * (i + 1), rect.getWidth() / h, rect.getHeight() / w); g2.setPaint(c); g2.fill(tile); } } } for (int i = 0; i < sequence.length(); i++) { point.y = i; tile.setRect(rect.getMinX(), rect.getMinY() + rect.getHeight() / w * (i + 1), rect.getWidth() / h, rect.getHeight() / w); g2.setPaint(Color.black); g2.fill(tile); g2.setPaint(Color.white); paintString(sequence.substring(i, i + 1), g2, tile, 0, 0); tile.setRect(rect.getMaxX() - rect.getWidth() / h, rect.getMinY() + rect.getHeight() / w * (i + 1), rect.getWidth() / h, rect.getHeight() / w); g2.setPaint(Color.black); g2.fill(tile); g2.setPaint(Color.white); paintString(sequence.substring(i, i + 1), g2, tile, 0, 0); for (int j = 0; j < sequence.length(); j++) { point.x = j; Paint b = border.apply(point); tile.setRect(rect.getMinX() + rect.getWidth() / h * (j + 1), rect.getMinY() + rect.getHeight() / w * (i + 1), rect.getWidth() / h, rect.getHeight() / w); if (b != null) { g2.setPaint(b); g2.draw(tile); } } } for (int j = 0; j < h - 2; j++) { tile.setRect(rect.getMinX() + rect.getWidth() / h * (j + 1), rect.getMinY(), rect.getWidth() / h, rect.getHeight() / w); g2.setPaint(Color.black); g2.fill(tile); g2.setPaint(Color.white); paintString(sequence.substring(j, j + 1), g2, tile, 0, 0); tile.setRect(rect.getMinX() + rect.getWidth() / h * (j + 1), rect.getMaxY() - rect.getHeight() / w, rect.getWidth() / h, rect.getHeight() / w); g2.setPaint(Color.black); g2.fill(tile); g2.setPaint(Color.white); paintString(sequence.substring(j, j + 1), g2, tile, 0, 0); } g2.setPaint(p); } /** * horizontal is from -1 to 1 where -1 means text is aligned left, 0 centered and 1 right * vertical likewise (-1 is bottom) * * shrink text if necessary! * @param s * @param g2 * @param rect * @param vertical * @param horizontal */ public static Rectangle2D paintString(String s, Graphics2D g2, Rectangle2D rect, float horizontal, float vertical) { return paintString(s, g2, rect, 0, 0, 0, horizontal, vertical, null); } public static Rectangle2D paintString(String s, Graphics2D g2, Rectangle2D rect, double rotation, float horizontal, float vertical) { return paintString(s, g2, rect, rotation, rect.getCenterX(), rect.getCenterY(), horizontal, vertical, null); } public static Rectangle2D paintString(String s, Graphics2D g2, Rectangle2D rect, double rotation, double rotX, double rotY, float horizontal, float vertical, Paint outline) { if (s == null) return new Rectangle2D.Double(rect.getX(), rect.getY(), 0, 0); AffineTransform trans = g2.getTransform(); g2.setTransform(new AffineTransform()); Font original = g2.getFont(); vertical = vertical / 2f + .5f; horizontal = horizontal / 2f + .5f; Rectangle2D des = g2.getFontMetrics().getStringBounds(s, g2); double w = rect.getWidth(); double h = rect.getHeight(); if (rotation != 0) { double w1 = w * Math.cos(rotation) + h * Math.sin(rotation); h = h * Math.cos(rotation) + w * Math.sin(rotation); w = w1; } double xratio = des.getWidth() / w; double yratio = des.getHeight() / h; double maxratio = Math.max(xratio, yratio); if (maxratio > 1) { g2.setFont(g2.getFont().deriveFont(AffineTransform.getScaleInstance(1 / maxratio, 1 / maxratio))); des = g2.getFontMetrics().getStringBounds(s, g2); } double x = rect.getX() + horizontal * (rect.getWidth() - des.getWidth()); double y = rect.getY() + g2.getFontMetrics().getAscent() + vertical * (rect.getHeight() - des.getHeight()); g2.rotate(rotation, rotX, rotY); g2.drawString(s, (float) x, (float) y); if (outline != null) { Shape outl = g2.getFont().createGlyphVector(g2.getFontRenderContext(), s).getOutline(); g2.translate(x, y); g2.setPaint(outline); g2.draw(outl); } g2.setFont(original); des.setRect(x, y - des.getHeight(), des.getWidth(), des.getHeight()); g2.setTransform(trans); return des; } }