Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.EventQueue;
- import java.awt.Graphics2D;
- import javax.imageio.ImageIO;
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.text.BadLocationException;
- import javax.swing.text.SimpleAttributeSet;
- import javax.swing.text.Style;
- import javax.swing.text.StyleConstants;
- import javax.swing.text.StyleContext;
- import javax.swing.text.StyledDocument;
- import javax.swing.text.html.HTMLEditorKit;
- import java.awt.GridLayout;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import javax.swing.JScrollPane;
- import javax.swing.ScrollPaneConstants;
- import javax.swing.JTextPane;
- public class MinimalExample extends JFrame {
- private JPanel contentPane;
- private JScrollPane scrollPane;
- private JTextPane textPane;
- // Setup some data for an example card:
- String name = "Absorb Vis";
- String set = "CON";
- String manaCost = "{6}{B}";
- String printedType = "Sorcery";
- String artist = "Brandon Kitkouski";
- String rulesText = "Target player loses 4 life and you gain 4 life.\n" +
- "Basic landcycling {1}{B} ({1}{B}, Discard this card: " +
- "Search your library for a basic land card, reveal it, and put " +
- "it into your hand. Then shuffle your library.)";
- HashMap<String, BufferedImage> manaSymbolImages;
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- MinimalExample frame = new MinimalExample();
- frame.setVisible(true);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- public MinimalExample() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 230, 400);
- contentPane = new JPanel();
- contentPane.setBorder(null);
- setContentPane(contentPane);
- contentPane.setLayout(new GridLayout(1, 0, 0, 0));
- scrollPane = new JScrollPane();
- scrollPane.setViewportBorder(new EmptyBorder(0, 3, 0, 3));
- scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
- contentPane.add(scrollPane);
- textPane = new JTextPane();
- textPane.setEditable(false);
- scrollPane.setViewportView(textPane);
- // You can comment this out to see how it looks in non-html mode:
- textPane.setEditorKit(new HTMLEditorKit());
- loadManaCostIcons();
- setPaneText();
- }
- /* This part inserts the text into the document of the text pane. Read below for details. */
- public void setPaneText() {
- String[] contents = new String[] { name, // name
- "ignored" + "\n", // manaCost
- printedType, // printedType
- set + "\n\n", // set
- rulesText + "\n", // rulesText
- "Illus. " + artist }; // artist
- StyledDocument textPaneDoc = textPane.getStyledDocument();
- // I tried it this was first, but that didn't work at all. Maybe you'll have more luck.
- /*
- String[] styles = new String[] { "nameStyle", "manaCostStyle","printedTypeStyle",
- "setStyle", "rulesTextStyle", "artistStyle" };
- addStylesToDocument(textPaneDoc);
- */
- SimpleAttributeSet[] styleAttributes = new SimpleAttributeSet[contents.length];
- getStyleAttributes(styleAttributes);
- for (int i = 0; i < contents.length; i++) {
- try {
- if (i == 4) {
- addTextWithSymbols(contents[i], styleAttributes[i]);
- continue;
- }
- textPaneDoc.insertString(textPaneDoc.getLength(),
- contents[i],
- //textPaneDoc.getStyle(styles[i])); // see above.
- styleAttributes[i]);
- }
- catch (BadLocationException e) {
- e.printStackTrace();
- }
- }
- textPane.revalidate();
- textPane.repaint();
- }
- /* This adds the rest of the text to the pane. The codes for the symbols get replaced by the
- * actual symbols and the text gets inserted piece by piece. */
- public void addTextWithSymbols(String text, SimpleAttributeSet style) {
- StyledDocument textPaneDoc = textPane.getStyledDocument();
- Pattern symbolPattern = Pattern.compile("\\{(.*?)\\}");
- try {
- Matcher symbolMatcher = symbolPattern.matcher(text);
- int previousMatch = 0;
- while (symbolMatcher.find()) {
- int start = symbolMatcher.start();
- int end = symbolMatcher.end();
- String subStringText = text.substring(previousMatch, start);
- String currentMatch = text.substring(start, end);
- if (subStringText.isEmpty() == false) {
- textPaneDoc.insertString(textPaneDoc.getLength(), subStringText, style);
- /* I tried with adding to the text pane's styled document and to the text
- * pane directly, but the second way doesn't seem to work at all. */
- //textPane.replaceSelection(subStringText);
- }
- ImageIcon currentIcon = new ImageIcon(manaSymbolImages.get(currentMatch));
- SimpleAttributeSet iconAtts = new SimpleAttributeSet();
- JLabel iconLabel = new JLabel();
- // None of those seem to change anything visible:
- //iconLabel.setVerticalAlignment(JLabel.CENTER);
- //iconLabel.setHorizontalAlignment(JLabel.RIGHT);
- iconLabel.setIcon(currentIcon);
- StyleConstants.setComponent(iconAtts, iconLabel);
- //StyleConstants.setAlignment(iconAtts, StyleConstants.ALIGN_LEFT);
- textPaneDoc.insertString(textPaneDoc.getLength(), "ignored", iconAtts);
- //textPane.insertIcon(currentIcon);
- previousMatch = end;
- }
- String subStringText = text.substring(previousMatch);
- if (subStringText.isEmpty() == false) {
- textPaneDoc.insertString(textPaneDoc.getLength(), subStringText, style);
- //textPane.replaceSelection(subStringText);
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void getStyleAttributes(SimpleAttributeSet[] styleAttributes) {
- SimpleAttributeSet defaultAtts = new SimpleAttributeSet();
- StyleConstants.setFontFamily(defaultAtts, "SansSerif");
- SimpleAttributeSet nameAtts = new SimpleAttributeSet(defaultAtts);
- StyleConstants.setBold(nameAtts, true);
- StyleConstants.setAlignment(nameAtts, StyleConstants.ALIGN_LEFT);
- SimpleAttributeSet manaCostAtts = new SimpleAttributeSet(defaultAtts);
- ImageIcon manaCostIcon = combineSymbols(manaCost);
- JLabel manaCostLabel = new JLabel(manaCostIcon);
- manaCostLabel.setVerticalAlignment(JLabel.CENTER);
- manaCostLabel.setHorizontalAlignment(JLabel.RIGHT);
- StyleConstants.setComponent(manaCostAtts, manaCostLabel);
- StyleConstants.setAlignment(manaCostAtts, StyleConstants.ALIGN_RIGHT);
- SimpleAttributeSet printedTypeAtts = new SimpleAttributeSet(defaultAtts);
- StyleConstants.setAlignment(printedTypeAtts, StyleConstants.ALIGN_LEFT);
- SimpleAttributeSet setAtts = new SimpleAttributeSet(nameAtts);
- StyleConstants.setAlignment(setAtts, StyleConstants.ALIGN_RIGHT);
- SimpleAttributeSet rulesAtts = new SimpleAttributeSet(defaultAtts);
- StyleConstants.setAlignment(rulesAtts, StyleConstants.ALIGN_LEFT);
- SimpleAttributeSet artistAtts = new SimpleAttributeSet(defaultAtts);
- StyleConstants.setFontSize(artistAtts, 10);
- styleAttributes[0] = nameAtts;
- styleAttributes[1] = manaCostAtts;
- styleAttributes[2] = printedTypeAtts;
- styleAttributes[3] = setAtts;
- styleAttributes[4] = rulesAtts;
- styleAttributes[5] = artistAtts;
- }
- /* This was my first way to try it, see my post. With this, I can't even get the right-
- * alignment to work. */
- public void addStylesToDocument(StyledDocument document) {
- Style defaultStyle = StyleContext.getDefaultStyleContext().
- getStyle(StyleContext.DEFAULT_STYLE);
- Style normalStyle = document.addStyle("normalStyle", defaultStyle);
- StyleConstants.setFontFamily(normalStyle, "SansSerif");
- Style nameStyle = document.addStyle("nameStyle", normalStyle);
- StyleConstants.setBold(nameStyle, true);
- StyleConstants.setAlignment(nameStyle, StyleConstants.ALIGN_LEFT);
- Style manaCostStyle = document.addStyle("manaCostStyle", normalStyle);
- ImageIcon manaCostIcon = combineSymbols(manaCost);
- JLabel manaCostLabel = new JLabel(manaCostIcon);
- manaCostLabel.setVerticalAlignment(JLabel.CENTER);
- manaCostLabel.setHorizontalAlignment(JLabel.RIGHT);
- StyleConstants.setComponent(manaCostStyle, manaCostLabel);
- StyleConstants.setAlignment(manaCostStyle, StyleConstants.ALIGN_RIGHT);
- Style printedTypeStyle = document.addStyle("printedTypeStyle", normalStyle);
- StyleConstants.setAlignment(printedTypeStyle, StyleConstants.ALIGN_LEFT);
- Style setStyle = document.addStyle("setStyle", nameStyle);
- StyleConstants.setAlignment(setStyle, StyleConstants.ALIGN_RIGHT);
- Style rulesTextStyle = document.addStyle("rulesTextStyle", normalStyle);
- Style artistStyle = document.addStyle("artistStyle", normalStyle);
- StyleConstants.setFontSize(artistStyle, 10);
- }
- /* Everything below is more or less irrelevant. However, you might need to adjust the image
- * image file paths. */
- public void loadManaCostIcons() {
- manaSymbolImages = new HashMap<String, BufferedImage>();
- try {
- // Most likely, those paths won't work for you!
- File bFile = new File("resource/B.png");
- File c1File = new File("resource/1.png");
- File c6File = new File("resource/6.png");
- manaSymbolImages.put("{B}", ImageIO.read(bFile));
- manaSymbolImages.put("{1}", ImageIO.read(c1File));
- manaSymbolImages.put("{6}", ImageIO.read(c6File));
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- public ImageIcon combineSymbols(String symbols) {
- String[] manaSymbols = symbols.split("(?<=})");
- int combinedWidth = 0;
- int maxHeight = 0;
- for (int i = 0; i < manaSymbols.length; i++) {
- BufferedImage currentSymbolImage = manaSymbolImages.get(manaSymbols[i]);
- combinedWidth += currentSymbolImage.getWidth();
- if (maxHeight < currentSymbolImage.getWidth()) {
- maxHeight = currentSymbolImage.getWidth();
- }
- }
- BufferedImage combinedManaCostImage = new BufferedImage(combinedWidth,
- maxHeight,
- BufferedImage.TYPE_INT_ARGB);
- Graphics2D graphics = combinedManaCostImage.createGraphics();
- int currentPosition = 0;
- for (int i = 0; i < manaSymbols.length; i++) {
- BufferedImage tempCurrentImage = manaSymbolImages.get(manaSymbols[i].trim());
- graphics.drawImage(tempCurrentImage, null, currentPosition, 0);
- currentPosition += tempCurrentImage.getWidth();
- }
- graphics.dispose();
- return (new ImageIcon(combinedManaCostImage));
- }
- }
Add Comment
Please, Sign In to add comment